mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 14:38:49 +00:00
fix: refactor guilds command
This commit is contained in:
1 parent
56b25f9b97
commit
c68945272e
1 file changed
+75
-41
@@ -1,4 +1,11 @@
|
|||||||
import { ApplicationCommandOption, ApplicationCommandOptionData, EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
import {
|
||||||
|
ApplicationCommandOption,
|
||||||
|
ApplicationCommandOptionData,
|
||||||
|
ChatInputCommandInteraction,
|
||||||
|
EmbedBuilder,
|
||||||
|
SlashCommandBuilder
|
||||||
|
} from 'discord.js';
|
||||||
|
import { Logger } from 'pino';
|
||||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||||
import { ExtendedClient } from '../../../types/clientTypes';
|
import { ExtendedClient } from '../../../types/clientTypes';
|
||||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||||
@@ -20,58 +27,85 @@ class ReloadCommand extends BaseSlashCommandInteraction {
|
|||||||
await this.runValidators({ interaction, executionId }, [checkValidGuildId]);
|
await this.runValidators({ interaction, executionId }, [checkValidGuildId]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.debug('Reloading interaction module across all shards.');
|
await this.reloadInteractionsAcrossShards(logger, executionId, client!);
|
||||||
await client!
|
|
||||||
.shard!.broadcastEval(
|
|
||||||
async (shardClient: ExtendedClient, { executionId }) => {
|
|
||||||
shardClient.registerClientInteractions!({ client: shardClient, executionId });
|
|
||||||
},
|
|
||||||
{ context: { executionId: executionId } }
|
|
||||||
)
|
|
||||||
.then(() => {
|
|
||||||
logger.debug('Successfully reloaded interaction module across all shards.');
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error, 'Failed to reload interaction module across shards.');
|
return await this.handleReloadError(logger, interaction, executionId, error);
|
||||||
|
|
||||||
logger.debug('Responding with error embed.');
|
|
||||||
return await interaction.editReply({
|
|
||||||
embeds: [
|
|
||||||
new EmbedBuilder()
|
|
||||||
|
|
||||||
.setDescription(
|
|
||||||
`**${this.embedOptions.icons.error} Oops!**\n_Hmm.._ It seems I am unable to reload interaction module across shards.`
|
|
||||||
)
|
|
||||||
.setColor(this.embedOptions.colors.error)
|
|
||||||
.setFooter({ text: `Execution ID: ${executionId}` })
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const commands: string[] | undefined = client!.slashCommandInteractions?.map(
|
return await this.respondWithSuccessEmbed(logger, interaction, client!);
|
||||||
(command: BaseSlashCommandInteraction) => {
|
}
|
||||||
let params: string = '';
|
|
||||||
|
|
||||||
if (command.data.options && command.data.options.length > 1) {
|
private async reloadInteractionsAcrossShards(logger: Logger, executionId: string, client: ExtendedClient) {
|
||||||
const options = command.data.options as unknown as ApplicationCommandOptionData[];
|
logger.debug('Reloading interaction module across all shards.');
|
||||||
|
await client!.shard!.broadcastEval(
|
||||||
options.map((option: ApplicationCommandOption) => {
|
async (shardClient: ExtendedClient, { executionId }) => {
|
||||||
params += `**\`${option.name}\`**` + ' ';
|
shardClient.registerClientInteractions!({ client: shardClient, executionId });
|
||||||
});
|
},
|
||||||
}
|
{ context: { executionId: executionId } }
|
||||||
|
|
||||||
return `- **\`/${command.data.name}\`** ${params}- ${command.data.description}`;
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
logger.debug('Successfully reloaded interaction module across all shards.');
|
||||||
|
}
|
||||||
|
|
||||||
const embedDescription: string =
|
private async handleReloadError(
|
||||||
`**${this.embedOptions.icons.bot} Reloaded commands**\n` + commands?.join('\n');
|
logger: Logger,
|
||||||
|
interaction: ChatInputCommandInteraction,
|
||||||
|
executionId: string,
|
||||||
|
error: Error | unknown
|
||||||
|
) {
|
||||||
|
logger.error(error, 'Failed to reload interaction module across shards.');
|
||||||
|
|
||||||
|
logger.debug('Responding with error embed.');
|
||||||
|
return await interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
new EmbedBuilder()
|
||||||
|
.setDescription(
|
||||||
|
`**${this.embedOptions.icons.error} Oops!**\n` +
|
||||||
|
'_Hmm.._ It seems I am unable to reload interaction module across shards.'
|
||||||
|
)
|
||||||
|
.setColor(this.embedOptions.colors.error)
|
||||||
|
.setFooter({ text: `Execution ID: ${executionId}` })
|
||||||
|
]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async respondWithSuccessEmbed(
|
||||||
|
logger: Logger,
|
||||||
|
interaction: ChatInputCommandInteraction,
|
||||||
|
client: ExtendedClient
|
||||||
|
) {
|
||||||
|
const commands = this.getCommands(client);
|
||||||
|
const embedDescription = this.buildEmbedDescription(commands);
|
||||||
|
|
||||||
logger.debug('Responding with success embed.');
|
logger.debug('Responding with success embed.');
|
||||||
return await interaction.editReply({
|
return await interaction.editReply({
|
||||||
embeds: [new EmbedBuilder().setDescription(embedDescription).setColor(this.embedOptions.colors.success)]
|
embeds: [new EmbedBuilder().setDescription(embedDescription).setColor(this.embedOptions.colors.success)]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getCommands(client: ExtendedClient): string[] | undefined {
|
||||||
|
return client!.slashCommandInteractions?.map((command: BaseSlashCommandInteraction) => {
|
||||||
|
const params = this.getCommandParams(command);
|
||||||
|
return `- **\`/${command.data.name}\`** ${params}- ${command.data.description}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCommandParams(command: BaseSlashCommandInteraction): string {
|
||||||
|
let params: string = '';
|
||||||
|
|
||||||
|
if (command.data.options && command.data.options.length > 1) {
|
||||||
|
const options = command.data.options as unknown as ApplicationCommandOptionData[];
|
||||||
|
|
||||||
|
options.map((option: ApplicationCommandOption) => {
|
||||||
|
params += `**\`${option.name}\`**` + ' ';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildEmbedDescription(commands: string[] | undefined): string {
|
||||||
|
return `**${this.embedOptions.icons.bot} Reloaded commands**\n` + commands?.join('\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new ReloadCommand();
|
export default new ReloadCommand();
|
||||||
Reference in new issue
Block a user