discord-bot/index.ts

57 lines
No EOL
1.5 KiB
TypeScript

import { ActivityType, Client, Events, GatewayIntentBits, REST, Routes } from "discord.js";
import commands from "./cmd";
import { db } from "./db/db";
import { StereoFile } from "./types/files";
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
});
const rest = new REST({ version: "10" })
.setToken(process.env.TOKEN);
bot.once(Events.ClientReady, async (c) => {
await rest.put(
Routes.applicationCommands(process.env.CLIENT_ID),
{ body: commands.map(([data, _]) => data) }
);
const files = (await db.all<StereoFile[]>`SELECT * FROM files`).length
c.user.setPresence({
activities: [{ name: `${files} files uploaded to stereo`, type: ActivityType.Custom }],
status: "dnd"
});
console.log(c.user.tag);
});
bot.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const cmd = commands.find(([data]) => data.name === interaction.commandName);
if (!cmd) return;
try {
await cmd[1](interaction);
} catch (err) {
console.error(err);
await interaction.reply({ content: "there was an error executing this command", ephemeral: true });
}
});
for (const [_, __, on] of commands) {
if (!on) continue;
for (const [event, handler] of Object.entries(on)) {
if (!Object.values(Events).includes(event as Events)) { console.warn(`Unknown event: ${event}`); continue; }
bot.on(event, handler);
}
}
bot.login(process.env.TOKEN).catch((err) => {
console.error("Failed to login:", err)
process.exit(1)
});