Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: refactor guilds command
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Sep 13, 2023
1 parent 56b25f9 commit c689452
Showing 1 changed file with 74 additions and 40 deletions.
114 changes: 74 additions & 40 deletions src/interactions/commands/system/reload.ts
Original file line number Diff line number Diff line change
@@ -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 { ExtendedClient } from '../../../types/clientTypes';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
Expand All @@ -20,58 +27,85 @@ class ReloadCommand extends BaseSlashCommandInteraction {
await this.runValidators({ interaction, executionId }, [checkValidGuildId]);

try {
logger.debug('Reloading interaction module across all shards.');
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.');
});
await this.reloadInteractionsAcrossShards(logger, executionId, client!);
} catch (error) {
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}` })
]
});
return await this.handleReloadError(logger, interaction, executionId, error);
}

const commands: string[] | undefined = client!.slashCommandInteractions?.map(
(command: BaseSlashCommandInteraction) => {
let params: string = '';
return await this.respondWithSuccessEmbed(logger, interaction, client!);
}

if (command.data.options && command.data.options.length > 1) {
const options = command.data.options as unknown as ApplicationCommandOptionData[];
private async reloadInteractionsAcrossShards(logger: Logger, executionId: string, client: ExtendedClient) {
logger.debug('Reloading interaction module across all shards.');
await client!.shard!.broadcastEval(
async (shardClient: ExtendedClient, { executionId }) => {
shardClient.registerClientInteractions!({ client: shardClient, executionId });
},
{ context: { executionId: executionId } }
);
logger.debug('Successfully reloaded interaction module across all shards.');
}

options.map((option: ApplicationCommandOption) => {
params += `**\`${option.name}\`**` + ' ';
});
}
private async handleReloadError(
logger: Logger,
interaction: ChatInputCommandInteraction,
executionId: string,
error: Error | unknown
) {
logger.error(error, 'Failed to reload interaction module across shards.');

return `- **\`/${command.data.name}\`** ${params}- ${command.data.description}`;
}
);
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 embedDescription: string =
`**${this.embedOptions.icons.bot} Reloaded commands**\n` + commands?.join('\n');
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.');
return await interaction.editReply({
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();

0 comments on commit c689452

Please # to comment.