Files
1800queue/src/index.ts
2022-02-11 13:18:17 -06:00

78 lines
2.1 KiB
TypeScript

import { Client, Intents } from 'discord.js';
import * as fs from 'fs';
import { getPlayerInteraction } from './api';
import { registerCommands } from './discord';
import { discordInit, QueueCommands } from './queue';
import { errorMessage } from './util';
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] });
//init logs with a timestamp
console.log(new Date().toISOString()+'\n\n');
//get token
if (!fs.existsSync('./token.txt')) {
fs.writeFileSync('./token.txt', '');
console.error('Missing Discord Token, please enter the bot token into the token file');
process.exit(1);
}
const TOKEN = fs.readFileSync('./token.txt').toString();
//discord connections
CLIENT.on('ready', client => {
console.log(`Logged in as ${client.user.tag}`);
client.guilds.fetch().then(guilds =>
registerCommands(TOKEN, client.user.id, guilds.map(g => g.id)));
discordInit(client);
});
CLIENT.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
try {
switch (interaction.commandName) {
//mod commands
case 'open':
await QueueCommands.open(interaction);
break;
case 'close':
await QueueCommands.close(interaction);
break;
//general commands
case 'queue':
await QueueCommands.queue(interaction);
break;
case 'join':
await QueueCommands.join(interaction);
break;
case 'leave':
await QueueCommands.leave(interaction);
break;
case 'player':
await getPlayerInteraction(interaction);
break;
}
} catch (e) {
if (e instanceof errorMessage) {
if (interaction.deferred || interaction.replied)
interaction.editReply(e.msg);
else
interaction.reply({
content: e.msg,
ephemeral: e.ephemeral
});
} else console.error(e);
}
});
CLIENT.login(TOKEN);