Skip to content

Commit

Permalink
feat(command/help): convert to paginated message
Browse files Browse the repository at this point in the history
  • Loading branch information
fieztazica committed May 11, 2024
1 parent 50bcdbb commit e603eef
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
83 changes: 41 additions & 42 deletions src/commands/General/help.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { ApplyOptions } from '@sapphire/decorators';
import { PaginatedMessage } from '@sapphire/discord.js-utilities';
import { Command } from '@sapphire/framework';
import { APIApplicationCommandOptionChoice, ApplicationCommandType, EmbedBuilder, chatInputApplicationCommandMention } from 'discord.js';
import { ApplicationCommandType, EmbedBuilder, chatInputApplicationCommandMention } from 'discord.js';
import { getRandomHoot, replyLoadingChatInputInteraction } from '../../lib/utils';

const dev = process.env.NODE_ENV !== 'production';

const choices: APIApplicationCommandOptionChoice<string>[] = [
{ name: 'Get command list', value: 'commands' },
{ name: 'Support server', value: 'support' }
];

@ApplyOptions<Command.Options>({
description: 'This command will help you out'
})
Expand All @@ -18,27 +15,30 @@ export class UserCommand extends Command {
builder //
.setName(this.name)
.setDescription(this.description)
.addStringOption((option) =>
option
.setName('topic')
.setDescription('Select a topic')
.setRequired(true)
.addChoices(...choices)
)
);
}

public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const topic = interaction.options.getString('topic');
const response = await replyLoadingChatInputInteraction(interaction);

const paginatedMessage = new PaginatedMessage({
template: new EmbedBuilder()
.setColor('Random')
// Be sure to add a space so this is offset from the page numbers!
.setFooter({ text: ` ${getRandomHoot()}`, iconURL: `${interaction.user.displayAvatarURL()}` })
.setThumbnail(this.container.client.user?.displayAvatarURL() || null)
});

paginatedMessage.addPageEmbed(this.getWelcomeEmbed());
const commandEmbeds = await this.getCommandsEmbeds(interaction);
commandEmbeds.forEach((embed) => paginatedMessage.addPageEmbed(embed));

if (topic === 'commands') {
return this.commandsOptionRun(interaction);
}
await paginatedMessage.run(response, interaction.user);

return this.supportOptionRun(interaction);
return response;
}

private async commandsOptionRun(interaction: Command.ChatInputCommandInteraction) {
private async getCommandsEmbeds(interaction: Command.ChatInputCommandInteraction) {
const { guild } = interaction;
const applicationCommands = (dev ? await guild!.commands.fetch() : await this.container.client.application!.commands.fetch())
.filter((c) => c.type === ApplicationCommandType.ChatInput)
Expand All @@ -57,32 +57,31 @@ export class UserCommand extends Command {
.map((command) => applicationCommands.find((ac) => ac.name === command.name))
}));

const embed = new EmbedBuilder()
.setColor('Random')
.setTitle(`COMMAND LIST`)
.setThumbnail(this.container.client.user?.displayAvatarURL() || null)
.setDescription(
`We use \`/command\` (Slash command) only! For further reason click [here](https://support-dev.discord.com/hc/en-us/articles/4404772028055).`
)
.addFields(
...categorizedCommands.map((category) => ({
name: category.categoryName,
value: category.commands
const embeds = categorizedCommands.map((category) =>
new EmbedBuilder()
.setTitle(`${category.categoryName} Commands`)
.setDescription(
category.commands
.map((command) => chatInputApplicationCommandMention(command?.name || 'unknownName', command?.id || 'unknownId'))
.join(' • ')
}))
);

return interaction.reply({
embeds: [embed]
});
)
);
return embeds;
}

private async supportOptionRun(interaction: Command.ChatInputCommandInteraction) {
const supportGuild = await this.container.client.guilds.fetch(process.env.SUPPORT_SERVER_ID);

return interaction.reply({
content: `${await supportGuild.invites.create(supportGuild.rulesChannelId!)}`
});
private getWelcomeEmbed() {
const linkMardown = (name: string, url: string) => `[${name}](${url})`;
const hootSlashReasonUrl = linkMardown('here', 'https://support-dev.discord.com/hc/en-us/articles/4404772028055');
const hootSupportServerUrl = linkMardown('Support Server', process.env.SUPPORT_SERVER_INVITE_LINK);
const hootFeedbackUrl = linkMardown('Feedback', process.env.SUPPORT_URL);
const embed = new EmbedBuilder()
.setTitle('HELP PANEL')
.setDescription(
[
`Hoot uses \`/command\` (Slash command) only! For further reason click ${hootSlashReasonUrl}.`,
`${hootFeedbackUrl}${hootSupportServerUrl}`
].join('\n\n')
);
return embed;
}
}
2 changes: 2 additions & 0 deletions src/lib/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ declare module '@skyra/env-utilities' {
OWNERS: ArrayString;
DEV_GUILD_ID: string;
SUPPORT_SERVER_ID: string;
SUPPORT_SERVER_INVITE_LINK: string;
SUPPORT_URL: string;
}
}
13 changes: 13 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,16 @@ export function setupStatusChanger() {
client.user?.setActivity(status);
});
}

export function getRandomInt(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

export function getRandomHoot(oLength: number = 0, upperH: boolean = false, upperCase: boolean = false) {
const min = 1,
max = oLength <= 0 ? 10 : oLength > 10 ? 10 : oLength;
const randomLength = getRandomInt(min, max);
const oString = new Array(randomLength).fill('o').join('');
const content = `${upperH ? 'H' : 'h'}${oString}t`;
return upperCase ? content.toUpperCase() : content;
}

0 comments on commit e603eef

Please sign in to comment.