jillo-bot/src/index.ts

86 lines
2.7 KiB
TypeScript

import { Client, GatewayIntentBits, Events, Collection } from 'discord.js';
import * as fs from 'fs';
const { token } = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
import * as path from 'path';
import { initializeAnnouncements } from './lib/subscriptions';
import * as log from './lib/log';
import chalk from 'chalk';
import prettyBytes from 'pretty-bytes';
const bot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages
],
});
async function init() {
log.nonsense('booting chip...');
log.nonsense('setting up connection...');
try {
await bot.login(token);
} catch (err) {
log.error('error: network hardware broken?', err);
log.error(`${chalk.bold('emergency mode could not be established.')} shutting down.`);
}
}
bot.on(Events.ClientReady, async () => {
log.info('jillo online');
log.nonsense('finishing launch');
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);
}
log.info('jillo firmware up and running');
log.nonsense(`| running on ${process.platform} ${process.config.variables.host_arch}`);
log.nonsense(`| node ${process.version} V8 v${process.versions.v8}`);
const memory = process.memoryUsage();
log.nonsense(`| ${prettyBytes(memory.rss)} memory usage, ${prettyBytes(memory.heapUsed)} / ${prettyBytes(memory.heapTotal)} heap usage`);
});
bot.on(Events.InteractionCreate, async (interaction) => {
if (interaction.isCommand()) {
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`');
log.error(error);
}
} else if (interaction.isAutocomplete()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.autocomplete(interaction);
} catch (error) {
log.error(error);
}
}
});
process.on('uncaughtException', err => {
log.error(err);
});
init();