64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
|
|
import { Client, Intents } from 'discord.js';
|
|
import * as fs from 'fs';
|
|
import { getPlayerInteraction } from './api';
|
|
import { registerCommands } from './discord';
|
|
import { cancelQueue, createQueue, joinQueue, leaveQueue, readyQueue } 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')) {
|
|
fs.writeFileSync('./token', '');
|
|
console.error('Missing Discord Token, please enter the bot token into the token file');
|
|
process.exit(1);
|
|
}
|
|
const TOKEN = fs.readFileSync('./token').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)));
|
|
});
|
|
|
|
CLIENT.on('interactionCreate', async interaction => {
|
|
if (!interaction.isCommand()) return;
|
|
|
|
try {
|
|
|
|
if (interaction.commandName === 'queue')
|
|
await createQueue(interaction);
|
|
else if (interaction.commandName === 'join')
|
|
await joinQueue(interaction);
|
|
else if (interaction.commandName === 'leave')
|
|
await leaveQueue(interaction);
|
|
else if (interaction.commandName === 'ready')
|
|
await readyQueue(interaction);
|
|
else if (interaction.commandName === 'cancel')
|
|
await cancelQueue(interaction);
|
|
else if (interaction.commandName === 'player')
|
|
await getPlayerInteraction(interaction);
|
|
|
|
} 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);
|