diff --git a/src/commands/cite.ts b/src/commands/cite.ts index c4c03bb..fe1d1cf 100644 --- a/src/commands/cite.ts +++ b/src/commands/cite.ts @@ -1,48 +1,63 @@ -import { ApplicationIntegrationType, AutocompleteInteraction, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; +// External Imports +import { AutocompleteInteraction, Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from 'athena-prime'; + +// Internal Imports import { Database } from '../shared/Database'; -import { KnowledgebaseItem } from '../types'; +import { CreateDefaultEmbed, SetSeverity } from '../utils'; -export default { - name: 'cite', - data: new SlashCommandBuilder() - .setName('cite') - .setDescription('Cite content from the knowledgebase.') - .setIntegrationTypes(ApplicationIntegrationType.GuildInstall) - .setContexts(InteractionContextType.Guild) - .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages) - .addIntegerOption(option => - option - .setName('name') - .setDescription('The name of the entry.') - .setRequired(true) - .setAutocomplete(true) - ), - handler: async function (client: Client, interaction: ChatInputCommandInteraction) { - if (!interaction.guild) return interaction.reply({ flags: [MessageFlags.Ephemeral], content: 'Must be ran in guild.' }); +// --------------- - await interaction.deferReply(); +@SlashCommand( + new CommandBuilder('cite', 'Cite a knowledgebase article.') + .setIntegrationTypes(Constants.ApplicationIntegrationType.GuildInstall) + .setContexts(Constants.InteractionContextType.Guild) + .setCommandType(Constants.ApplicationCommandType.ChatInput) + .addIntegerOption({ name: 'article', description: 'The article to cite.', required: true, autocomplete: true }) +) +class CiteCommand extends Command { + cooldown = 5; // 5s. - const id = interaction.options.getInteger('name', true); + async handleCommand(context: CommandClient, interaction: CommandInteraction, data: any) { + if (!interaction.inGuild()) return; + await interaction.defer(); - const item = await Database.getFromKnowledgebase(id); + const article = interaction.getRequiredInteger('article'); + const item = await Database.getFromKnowledgebase(article); + + if (!item) { + await interaction.createMessage({ + embeds: [{ + ...CreateDefaultEmbed(context), + ...SetSeverity('error'), - if (!item) return interaction.editReply('Knowledgebase item not found.'); + description: 'There was an issue locating the requested knowledgebase article.' + }] + }); - return interaction.editReply(item.content); - }, - autoComplete: async function (client: Client, interaction: AutocompleteInteraction) { - if (!interaction.guild) return interaction.respond([]); + return; + } - const items: KnowledgebaseItem[] = await Database.getAllKnowledgebaseItems(interaction.guild.id); + await interaction.createMessage({ + embeds: [{ + ...CreateDefaultEmbed(context), - const value = interaction.options.getFocused(); - - const toRespond = items.filter(i => !value ? true : i.content.includes(value)); - if (toRespond.length > 25) toRespond.length = 25; - - interaction.respond(toRespond.map(p => ({ - name: p.name, - value: p.id - }))); + description: item.content + }] + }); } -}; \ No newline at end of file + + async handleAutocomplete(context: CommandClient, interaction: AutocompleteInteraction, data: any) { + if (!interaction.guild) return await interaction.acknowledge([]); + const items = await Database.getAllKnowledgebaseItems(interaction.guild.id); + + const value = interaction.focused(); + if(!value) return await interaction.acknowledge([]); + + const response = items.filter(item => item.content.includes(value.value as string)); + if (response.length > 25) response.length = 25; + + return await interaction.acknowledge(response.map(res => ({ name: res.name, value: res.id }))); + } +} + +export default new CiteCommand('cite'); \ No newline at end of file diff --git a/src/commands/utilities/about.ts b/src/commands/utilities/about.ts index d04f0a5..6947a9d 100644 --- a/src/commands/utilities/about.ts +++ b/src/commands/utilities/about.ts @@ -13,7 +13,7 @@ import { CreateDefaultEmbed } from '../../utils'; .setCommandType(Constants.ApplicationCommandType.ChatInput) ) class AboutCommand extends Command { - cooldown = 60; + cooldown = 5; // 5s. async handleCommand(context: CommandClient, interaction: CommandInteraction, data: any) { if (!interaction.inGuild()) return; diff --git a/src/commands/utilities/name-sync.ts b/src/commands/utilities/name-sync.ts index e7531a9..76b4498 100644 --- a/src/commands/utilities/name-sync.ts +++ b/src/commands/utilities/name-sync.ts @@ -25,7 +25,7 @@ async function fetchUser(member: Member, id: number | null): Promise { - cooldown = 60; + cooldown = 360; // 5m. async handleCommand(context: CommandClient, interaction: CommandInteraction, data: any) { if (!interaction.inGuild()) return;