jillo-bot/src/index.ts

63 lines
2.0 KiB
TypeScript

import { Client, GatewayIntentBits, Events, Collection, TextChannel } from 'discord.js';
import * as fs from 'fs';
const { token } = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
import * as path from 'path';
import { initializeCounter } from './lib/counter';
import { initializeAnnouncements } from './lib/subscriptions';
import { initTables } from './lib/db';
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages
],
});
bot.on(Events.ClientReady, async () => {
initializeCounter(false);
initializeCounter(true);
initializeAnnouncements(bot);
bot.commands = new Collection();
const cmdFiles = fs.readdirSync(path.join(__dirname, './commands')).filter((file) => file.endsWith('.js'));
for (const file of cmdFiles) {
const cmd = (await import(`./commands/${file}`));
bot.commands.set(cmd.data.name, cmd);
if (cmd.onClientReady) cmd.onClientReady(bot);
}
console.log('jillo online');
});
bot.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, interaction.member);
} catch (error) {
if (interaction.isRepliable() && !interaction.replied && !interaction.deferred) interaction.reply({ content: '`ERROR`', ephemeral: true });
if (interaction.deferred) interaction.followUp('`ERROR`');
console.error(error);
}
});
bot.on(Events.MessageDelete, (msg) => {
console.log(`${msg.author?.username}#${msg.author?.discriminator} in #${msg.channel instanceof TextChannel ? msg.channel.name : '?'} at ${msg.createdAt.toISOString()}`);
console.log(`${msg.content}`);
});
process.on('uncaughtException', err => {
console.error(err);
});
initTables().then(() => bot.login(token));