From aa268b584d93666e95dbb05e4417c1a81cba2e90 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Mon, 6 Jun 2022 18:45:45 +0700 Subject: [PATCH] Moved commands to global commands --- src/discord/InteractionManager.ts | 92 ++++++++++++++++++++++- src/utils/DiscordInteraction.ts | 118 ++++++++++++------------------ 2 files changed, 138 insertions(+), 72 deletions(-) diff --git a/src/discord/InteractionManager.ts b/src/discord/InteractionManager.ts index b0a7383..a94ce68 100644 --- a/src/discord/InteractionManager.ts +++ b/src/discord/InteractionManager.ts @@ -12,7 +12,9 @@ import { } from '../utils/DiscordMessage'; import { registerAllGuildsCommands, - unregisterAllGuildsCommands + registerAllGlobalCommands, + unregisterAllGuildsCommands, + unregisterAllGlobalCommands } from '../utils/DiscordInteraction'; const EMBEDS = { @@ -23,7 +25,7 @@ const EMBEDS = { fields: [ { name: 'Available arguments', - value: '``reloadAll``' + value: '``reloadAll`` ``unloadAll`` ``reloadglobal``' } ], user: data instanceof Interaction ? data.user : data.author @@ -43,6 +45,19 @@ const EMBEDS = { user: data instanceof Interaction ? data.user : data.author }); }, + UNLOADALL_SUCCESS: (data: Message | Interaction) => { + return makeSuccessEmbed({ + title: 'Unloaded all Interaction', + user: data instanceof Interaction ? data.user : data.author + }); + }, + UNLOADALL_ERROR: (data: Message | Interaction, err: any) => { + return makeErrorEmbed({ + title: 'An error occurred while trying to unload interaction', + description: '```' + err + '```', + user: data instanceof Interaction ? data.user : data.author + }); + }, RELOADALL_SUCCESS: (data: Message | Interaction) => { return makeSuccessEmbed({ title: 'Reloaded all Interaction', @@ -81,6 +96,75 @@ export default class InteractionManager extends DiscordModule { }); const funct = { + unloadAll: async (data: HybridInteractionMessage) => { + let placeholder: HybridInteractionMessage | undefined; + + let _placeholder = await sendHybridInteractionMessageResponse(data, { + embeds: [EMBEDS.PROCESSING(data.getRaw())] + }); + if (_placeholder) placeholder = new HybridInteractionMessage(_placeholder); + + try { + await unregisterAllGuildsCommands(); + + if (data && data.isMessage() && placeholder && placeholder.isMessage()) + return placeholder + .getMessage() + .edit({ embeds: [EMBEDS.UNLOADALL_SUCCESS(data.getRaw())] }); + else if (data.isSlashCommand()) + return await sendHybridInteractionMessageResponse( + data, + { embeds: [EMBEDS.UNLOADALL_SUCCESS(data.getRaw())] }, + true + ); + } catch (err) { + if (data && data.isMessage() && placeholder && placeholder.isMessage()) + return placeholder + .getMessage() + .edit({ embeds: [EMBEDS.UNLOADALL_ERROR(data.getRaw(), err)] }); + else if (data.isSlashCommand()) + return await sendHybridInteractionMessageResponse( + data, + { embeds: [EMBEDS.UNLOADALL_ERROR(data.getRaw(), err)] }, + true + ); + } + }, + reloadGlobal: async (data: HybridInteractionMessage) => { + let placeholder: HybridInteractionMessage | undefined; + + let _placeholder = await sendHybridInteractionMessageResponse(data, { + embeds: [EMBEDS.PROCESSING(data.getRaw())] + }); + if (_placeholder) placeholder = new HybridInteractionMessage(_placeholder); + + try { + await unregisterAllGlobalCommands(); + await registerAllGlobalCommands(); + + if (data && data.isMessage() && placeholder && placeholder.isMessage()) + return placeholder + .getMessage() + .edit({ embeds: [EMBEDS.RELOADALL_SUCCESS(data.getRaw())] }); + else if (data.isSlashCommand()) + return await sendHybridInteractionMessageResponse( + data, + { embeds: [EMBEDS.RELOADALL_SUCCESS(data.getRaw())] }, + true + ); + } catch (err) { + if (data && data.isMessage() && placeholder && placeholder.isMessage()) + return placeholder + .getMessage() + .edit({ embeds: [EMBEDS.RELOADALL_ERROR(data.getRaw(), err)] }); + else if (data.isSlashCommand()) + return await sendHybridInteractionMessageResponse( + data, + { embeds: [EMBEDS.RELOADALL_ERROR(data.getRaw(), err)] }, + true + ); + } + }, reloadAll: async (data: HybridInteractionMessage) => { let placeholder: HybridInteractionMessage | undefined; @@ -138,6 +222,10 @@ export default class InteractionManager extends DiscordModule { }); case 'reloadall': return await funct.reloadAll(data); + case 'reloadglobal': + return await funct.reloadGlobal(data); + case 'unloadall': + return await funct.unloadAll(data); } } } diff --git a/src/utils/DiscordInteraction.ts b/src/utils/DiscordInteraction.ts index 61eaabb..5d7a49b 100644 --- a/src/utils/DiscordInteraction.ts +++ b/src/utils/DiscordInteraction.ts @@ -3,24 +3,59 @@ import { SlashCommandBuilder } from '@discordjs/builders'; import DiscordProvider from '../providers/Discord'; import Logger from '../libs/Logger'; -export const GLOBAL_COMMANDS: Object[] = []; -export const GUILD_COMMANDS: Object[] = []; - -GUILD_COMMANDS.push(new SlashCommandBuilder().setName('help').setDescription('Show help menu')); -GUILD_COMMANDS.push( - new SlashCommandBuilder().setName('ping').setDescription('Measure network latency') -); -GUILD_COMMANDS.push( - new SlashCommandBuilder().setName('invite').setDescription('Invite me to your server!') -); -GUILD_COMMANDS.push( +export const GLOBAL_COMMANDS: Object[] = [ + new SlashCommandBuilder().setName('help').setDescription('Show help menu'), + new SlashCommandBuilder().setName('ping').setDescription('Measure network latency'), + new SlashCommandBuilder().setName('invite').setDescription('Invite me to your server!'), + new SlashCommandBuilder().setName('stats').setDescription('Show the bot stats'), + new SlashCommandBuilder().setName('skip').setDescription('Skip the current song'), + new SlashCommandBuilder() + .setName('nowplaying') + .setDescription('Show the current song information'), + new SlashCommandBuilder().setName('join').setDescription('Join the voice channel'), + new SlashCommandBuilder().setName('leave').setDescription('Leave the voice channel'), + new SlashCommandBuilder().setName('resume').setDescription('Resume paused music'), + new SlashCommandBuilder().setName('pause').setDescription('Pause the current music'), new SlashCommandBuilder() .setName('say') .setDescription('Make me say something') .addStringOption((option) => option.setName('message').setDescription('Message you want me to say').setRequired(true) + ), + new SlashCommandBuilder() + .setName('osu') + .setDescription('Interact with the game osu!') + .addSubcommand((user) => + user + .setName('user') + .setDescription('Get user information on osu!') + .addStringOption((user) => + user.setName('user').setDescription('Username or User id').setRequired(true) + ) ) -); + .addSubcommand((beatmap) => + beatmap + .setName('beatmap') + .setDescription('Get beatmap information on osu!') + .addStringOption((user) => + user.setName('beatmap').setDescription('Beatmap id').setRequired(true) + ) + ), + new SlashCommandBuilder() + .setName('userinfo') + .setDescription('Lookup discord user information') + .addSubcommand((user) => + user + .setName('user') + .setDescription('Lookup discord user information') + .addUserOption((user) => + user.setName('user').setDescription('Discord user to lookup').setRequired(true) + ) + ) +]; +export const GUILD_COMMANDS: Object[] = []; + +/* GUILD_COMMANDS.push( new SlashCommandBuilder() .setName('interaction') @@ -109,64 +144,7 @@ GUILD_COMMANDS.push( 'Create greeting message for membership screening into the channel. A message for new commers to read' ) ) -); -GUILD_COMMANDS.push( - new SlashCommandBuilder() - .setName('osu') - .setDescription('Interact with the game osu!') - .addSubcommand((user) => - user - .setName('user') - .setDescription('Get user information on osu!') - .addStringOption((user) => - user.setName('user').setDescription('Username or User id').setRequired(true) - ) - ) - .addSubcommand((beatmap) => - beatmap - .setName('beatmap') - .setDescription('Get beatmap information on osu!') - .addStringOption((user) => - user.setName('beatmap').setDescription('Beatmap id').setRequired(true) - ) - ) -); - -GUILD_COMMANDS.push( - new SlashCommandBuilder() - .setName('userinfo') - .setDescription('Lookup discord user information') - .addSubcommand((user) => - user - .setName('user') - .setDescription('Lookup discord user information') - .addUserOption((user) => - user.setName('user').setDescription('Discord user to lookup').setRequired(true) - ) - ) -); - -GUILD_COMMANDS.push( - new SlashCommandBuilder().setName('stats').setDescription('Show the bot stats') -); - -GUILD_COMMANDS.push( - new SlashCommandBuilder().setName('skip').setDescription('Skip the current song') -); - -GUILD_COMMANDS.push( - new SlashCommandBuilder() - .setName('nowplaying') - .setDescription('Show the current song information') -); - -GUILD_COMMANDS.push( - new SlashCommandBuilder().setName('join').setDescription('Join the voice channel') -); - -GUILD_COMMANDS.push( - new SlashCommandBuilder().setName('leave').setDescription('Leave the voice channel') -); +);*/ export const registerAllGlobalCommands = async () => { Logger.log('info', `Registering all global interaction commands`);