mirror of
https://github.com/BluemediaDev/muse.git
synced 2025-06-28 01:32:42 +02:00
fix command permission handling and push discord to v10 (#640)
Co-authored-by: Max Isom <hi@maxisom.me>
This commit is contained in:
parent
1ef05aba9d
commit
eb2885b206
38 changed files with 1214 additions and 644 deletions
47
src/bot.ts
47
src/bot.ts
|
@ -1,4 +1,4 @@
|
|||
import {Client, Collection, ExcludeEnum, PresenceStatusData, User} from 'discord.js';
|
||||
import {Client, Collection, PresenceStatusData, User} from 'discord.js';
|
||||
import {inject, injectable} from 'inversify';
|
||||
import ora from 'ora';
|
||||
import {TYPES} from './types.js';
|
||||
|
@ -7,32 +7,27 @@ 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';
|
||||
import {Routes, ActivityType} from 'discord-api-types/v10';
|
||||
import registerCommandsOnGuild from './utils/register-commands-on-guild.js';
|
||||
|
||||
@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,
|
||||
) {
|
||||
@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();
|
||||
|
@ -61,12 +56,13 @@ export default class {
|
|||
}
|
||||
|
||||
// Register event handlers
|
||||
// eslint-disable-next-line complexity
|
||||
this.client.on('interactionCreate', async interaction => {
|
||||
try {
|
||||
if (interaction.isCommand()) {
|
||||
const command = this.commandsByName.get(interaction.commandName);
|
||||
|
||||
if (!command) {
|
||||
if (!command || !interaction.isChatInputCommand()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -76,7 +72,6 @@ export default class {
|
|||
}
|
||||
|
||||
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;
|
||||
|
@ -111,9 +106,9 @@ export default class {
|
|||
|
||||
// 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)) {
|
||||
if ((interaction.isCommand() || interaction.isButton()) && (interaction.replied || interaction.deferred)) {
|
||||
await interaction.editReply(errorMsg(error as Error));
|
||||
} else if (interaction.isApplicationCommand() || interaction.isButton()) {
|
||||
} else if (interaction.isCommand() || interaction.isButton()) {
|
||||
await interaction.reply({content: errorMsg(error as Error), ephemeral: true});
|
||||
}
|
||||
} catch {}
|
||||
|
@ -126,11 +121,9 @@ export default class {
|
|||
debug(generateDependencyReport());
|
||||
|
||||
// Update commands
|
||||
const rest = new REST({version: '9'}).setToken(this.token);
|
||||
|
||||
const rest = new REST({version: '10'}).setToken(this.config.DISCORD_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())},
|
||||
|
@ -140,10 +133,12 @@ export default class {
|
|||
|
||||
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())},
|
||||
);
|
||||
await registerCommandsOnGuild({
|
||||
rest,
|
||||
guildId: guild.id,
|
||||
applicationId: this.client.user!.id,
|
||||
commands: this.commandsByName.map(c => c.slashCommand),
|
||||
});
|
||||
}),
|
||||
// Remove commands registered on bot (if they exist)
|
||||
rest.put(Routes.applicationCommands(this.client.user!.id), {body: []}),
|
||||
|
@ -155,18 +150,14 @@ export default class {
|
|||
activities: [
|
||||
{
|
||||
name: this.config.BOT_ACTIVITY,
|
||||
type: this.config.BOT_ACTIVITY_TYPE as unknown as ExcludeEnum<typeof ActivityTypes, 'CUSTOM'>,
|
||||
type: this.config.BOT_ACTIVITY_TYPE as unknown as Exclude<ActivityType, ActivityType.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`);
|
||||
spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot%20applications.commands&permissions=36700160`);
|
||||
});
|
||||
|
||||
this.client.on('error', console.error);
|
||||
|
@ -174,8 +165,6 @@ export default class {
|
|||
|
||||
this.client.on('guildCreate', handleGuildCreate);
|
||||
this.client.on('voiceStateUpdate', handleVoiceStateUpdate);
|
||||
this.client.on('guildUpdate', handleGuildUpdate);
|
||||
|
||||
await this.client.login(this.token);
|
||||
await this.client.login();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue