muse/src/bot.ts
Zagrthos 2f9382f517
Add customizable Bot status (#599)
Co-authored-by: Max Isom <codetheweb@users.noreply.github.com>
Co-authored-by: Max Isom <hi@maxisom.me>
2022-03-24 18:19:48 -05:00

182 lines
6.5 KiB
TypeScript

import {Client, Collection, ExcludeEnum, PresenceStatusData, User} from 'discord.js';
import {inject, injectable} from 'inversify';
import ora from 'ora';
import {TYPES} from './types.js';
import container from './inversify.config.js';
import Command from './commands/index.js';
import debug from './utils/debug.js';
import handleGuildCreate from './events/guild-create.js';
import handleVoiceStateUpdate from './events/voice-state-update.js';
import handleGuildUpdate from './events/guild-update.js';
import errorMsg from './utils/error-msg.js';
import {isUserInVoice} from './utils/channels.js';
import Config from './services/config.js';
import {generateDependencyReport} from '@discordjs/voice';
import {REST} from '@discordjs/rest';
import {Routes} from 'discord-api-types/v9';
import updatePermissionsForGuild from './utils/update-permissions-for-guild.js';
import {ActivityTypes} from 'discord.js/typings/enums';
@injectable()
export default class {
private readonly client: Client;
private readonly config: Config;
private readonly token: string;
private readonly shouldRegisterCommandsOnBot: boolean;
private readonly commandsByName!: Collection<string, Command>;
private readonly commandsByButtonId!: Collection<string, Command>;
constructor(
@inject(TYPES.Client) client: Client,
@inject(TYPES.Config) config: Config,
) {
this.client = client;
this.config = config;
this.token = config.DISCORD_TOKEN;
this.shouldRegisterCommandsOnBot = config.REGISTER_COMMANDS_ON_BOT;
this.commandsByName = new Collection();
this.commandsByButtonId = new Collection();
}
public async register(): Promise<void> {
// Load in commands
for (const command of container.getAll<Command>(TYPES.Command)) {
// Make sure we can serialize to JSON without errors
try {
command.slashCommand.toJSON();
} catch (error) {
console.error(error);
throw new Error(`Could not serialize /${command.slashCommand.name ?? ''} to JSON`);
}
if (command.slashCommand.name) {
this.commandsByName.set(command.slashCommand.name, command);
}
if (command.handledButtonIds) {
for (const buttonId of command.handledButtonIds) {
this.commandsByButtonId.set(buttonId, command);
}
}
}
// Register event handlers
this.client.on('interactionCreate', async interaction => {
try {
if (interaction.isCommand()) {
const command = this.commandsByName.get(interaction.commandName);
if (!command) {
return;
}
if (!interaction.guild) {
await interaction.reply(errorMsg('you can\'t use this bot in a DM'));
return;
}
const requiresVC = command.requiresVC instanceof Function ? command.requiresVC(interaction) : command.requiresVC;
if (requiresVC && interaction.member && !isUserInVoice(interaction.guild, interaction.member.user as User)) {
await interaction.reply({content: errorMsg('gotta be in a voice channel'), ephemeral: true});
return;
}
if (command.execute) {
await command.execute(interaction);
}
} else if (interaction.isButton()) {
const command = this.commandsByButtonId.get(interaction.customId);
if (!command) {
return;
}
if (command.handleButtonInteraction) {
await command.handleButtonInteraction(interaction);
}
} else if (interaction.isAutocomplete()) {
const command = this.commandsByName.get(interaction.commandName);
if (!command) {
return;
}
if (command.handleAutocompleteInteraction) {
await command.handleAutocompleteInteraction(interaction);
}
}
} catch (error: unknown) {
debug(error);
// This can fail if the message was deleted, and we don't want to crash the whole bot
try {
if ((interaction.isApplicationCommand() || interaction.isButton()) && (interaction.replied || interaction.deferred)) {
await interaction.editReply(errorMsg(error as Error));
} else if (interaction.isApplicationCommand() || interaction.isButton()) {
await interaction.reply({content: errorMsg(error as Error), ephemeral: true});
}
} catch {}
}
});
const spinner = ora('📡 connecting to Discord...').start();
this.client.once('ready', async () => {
debug(generateDependencyReport());
// Update commands
const rest = new REST({version: '9'}).setToken(this.token);
if (this.shouldRegisterCommandsOnBot) {
spinner.text = '📡 updating commands on bot...';
await rest.put(
Routes.applicationCommands(this.client.user!.id),
{body: this.commandsByName.map(command => command.slashCommand.toJSON())},
);
} else {
spinner.text = '📡 updating commands in all guilds...';
await Promise.all([
...this.client.guilds.cache.map(async guild => {
await rest.put(
Routes.applicationGuildCommands(this.client.user!.id, guild.id),
{body: this.commandsByName.map(command => command.slashCommand.toJSON())},
);
}),
// Remove commands registered on bot (if they exist)
rest.put(Routes.applicationCommands(this.client.user!.id), {body: []}),
],
);
}
this.client.user!.setPresence({
activities: [
{
name: this.config.BOT_ACTIVITY,
type: this.config.BOT_ACTIVITY_TYPE as unknown as ExcludeEnum<typeof ActivityTypes, 'CUSTOM'>,
url: this.config.BOT_ACTIVITY_URL === '' ? undefined : this.config.BOT_ACTIVITY_URL,
},
],
status: this.config.BOT_STATUS as PresenceStatusData,
});
// Update permissions
spinner.text = '📡 updating permissions...';
await Promise.all(this.client.guilds.cache.map(async guild => updatePermissionsForGuild(guild)));
spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot%20applications.commands&permissions=36700288`);
});
this.client.on('error', console.error);
this.client.on('debug', debug);
this.client.on('guildCreate', handleGuildCreate);
this.client.on('voiceStateUpdate', handleVoiceStateUpdate);
this.client.on('guildUpdate', handleGuildUpdate);
await this.client.login(this.token);
}
}