From 66dd982d7d8003679e1f0930f5f835478cc8c647 Mon Sep 17 00:00:00 2001 From: Smaug6739 Date: Sun, 14 Nov 2021 17:39:17 +0100 Subject: [PATCH] Autocomplete :tada: --- ...interactionCommandOrAutocompleteCreate.ts} | 12 ++++-- src/events/interactionCreate.ts | 21 +++++----- src/structures/Command.ts | 3 ++ .../src/commands/autocomplete/autocomplete.ts | 39 +++++++++++++++++++ test/src/events/missingPermissions.ts | 2 - ...nteractionCommandOrAutocompleteCreate.d.ts | 4 ++ typings/events/interactionCreate.d.ts | 4 +- typings/structures/Command.d.ts | 3 +- 8 files changed, 68 insertions(+), 20 deletions(-) rename src/events/{interactionCommandCreate.ts => interactionCommandOrAutocompleteCreate.ts} (86%) create mode 100644 test/src/commands/autocomplete/autocomplete.ts create mode 100644 typings/events/interactionCommandOrAutocompleteCreate.d.ts diff --git a/src/events/interactionCommandCreate.ts b/src/events/interactionCommandOrAutocompleteCreate.ts similarity index 86% rename from src/events/interactionCommandCreate.ts rename to src/events/interactionCommandOrAutocompleteCreate.ts index 39480de4..7e281cb9 100644 --- a/src/events/interactionCommandCreate.ts +++ b/src/events/interactionCommandOrAutocompleteCreate.ts @@ -1,10 +1,13 @@ -import { Collection } from 'discord.js'; +import { AutocompleteInteraction, Collection } from 'discord.js'; import { COMMAND_TYPE, INHIBITOR_TYPE, COMMAND_CHANNEL, COMMAND_PERMISSIONS, COMMAND_EVENTS } from '../constants/constants'; import { ShewenyError } from '../helpers'; import type { ShewenyClient } from '..'; import type { Inhibitor } from '../structures'; import type { CommandInteraction, ContextMenuInteraction } from 'discord.js'; -export default async function run(client: ShewenyClient, interaction: CommandInteraction | ContextMenuInteraction) { +export default async function run( + client: ShewenyClient, + interaction: CommandInteraction | ContextMenuInteraction | AutocompleteInteraction +) { try { if (!client.managers.commands) return; @@ -13,7 +16,8 @@ export default async function run(client: ShewenyClient, interaction: CommandInt //@ts-ignore if (!command || (command && ![COMMAND_TYPE.cmdSlash, COMMAND_TYPE.ctxUser, COMMAND_TYPE.ctxMsg].includes(command.type))) return; - if (command.before) await command.before(interaction); + if (interaction.isAutocomplete() && command.onAutocomplete) return await command.onAutocomplete(interaction); + if (command.before) await command.before(interaction as CommandInteraction | ContextMenuInteraction); /** * Handle inhibitors */ @@ -82,7 +86,7 @@ export default async function run(client: ShewenyClient, interaction: CommandInt /* ---------------COMMAND--------------- */ - await command.execute(interaction); + await command.execute(interaction as CommandInteraction | ContextMenuInteraction); } catch (e: any) { new ShewenyError(client, e); } diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index d806019a..16d3b45c 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -1,18 +1,17 @@ -import type { Interaction } from "discord.js"; -import type { ShewenyClient } from "../client/Client"; +import type { Interaction } from 'discord.js'; +import type { ShewenyClient } from '../client/Client'; export default function run(client: ShewenyClient, interaction: Interaction) { - if (interaction.isButton()) return client.emit("interactionButtonCreate", interaction); + if (interaction.isButton()) return client.emit('interactionButtonCreate', interaction); - if (interaction.isCommand() || interaction.isContextMenu()) - return client.emit("interactionCommandCreate", interaction); + if (interaction.isCommand() || interaction.isContextMenu()) return client.emit('interactionCommandCreate', interaction); - if (interaction.isContextMenu()) - return client.emit("interactionContextMenuCreate", interaction); + if (interaction.isCommand() || interaction.isContextMenu() || interaction.isAutocomplete()) + return client.emit('interactionCommandOrAutocompleteCreate', interaction); - if (interaction.isSelectMenu()) - return client.emit("interactionSelectMenuCreate", interaction); + if (interaction.isContextMenu()) return client.emit('interactionContextMenuCreate', interaction); - if (interaction.isMessageComponent()) - return client.emit("interactionMessageComponentCreate", interaction); + if (interaction.isSelectMenu()) return client.emit('interactionSelectMenuCreate', interaction); + + if (interaction.isMessageComponent()) return client.emit('interactionMessageComponentCreate', interaction); } diff --git a/src/structures/Command.ts b/src/structures/Command.ts index 09b208c4..a3864774 100644 --- a/src/structures/Command.ts +++ b/src/structures/Command.ts @@ -17,6 +17,7 @@ import type { ContextMenuInteraction, Message, PermissionString, + AutocompleteInteraction, } from 'discord.js'; import type { CommandsManager } from '..'; @@ -149,6 +150,8 @@ export abstract class Command extends BaseStructure { */ before?(interaction: CommandInteraction | ContextMenuInteraction | Message): any | Promise; + onAutocomplete?(interaction: AutocompleteInteraction): any | Promise; + /** * Main function `execute` for the commands * @param {CommandInteraction | ContextMenuInteraction | Message} interaction Interaction diff --git a/test/src/commands/autocomplete/autocomplete.ts b/test/src/commands/autocomplete/autocomplete.ts new file mode 100644 index 00000000..21becab1 --- /dev/null +++ b/test/src/commands/autocomplete/autocomplete.ts @@ -0,0 +1,39 @@ +import { Command, ShewenyClient } from '../../../../'; +import type { AutocompleteInteraction, CommandInteraction } from 'discord.js'; + +export class PingCommand extends Command { + constructor(client: ShewenyClient) { + super(client, { + name: 'autocomplete', + description: 'Test the autocomplete', + type: 'SLASH_COMMAND', + category: 'Misc', + options: [ + { name: 'name', description: 'description', type: 'STRING', autocomplete: true }, + { name: 'theme', description: 'description', type: 'STRING', autocomplete: true }, + ], + }); + } + execute(interaction: CommandInteraction) { + interaction.reply('Autocomplete work !'); + } + onAutocomplete(interaction: AutocompleteInteraction) { + const focusedOption = interaction.options.getFocused(true); + + let choices: any; + + if (focusedOption.name === 'name') { + choices = ['faq', 'install', 'collection', 'promise', 'debug']; + } + + if (focusedOption.name === 'theme') { + choices = ['halloween', 'christmas', 'summer']; + } + + const filtered = choices!.filter((choice: any) => choice.startsWith(focusedOption.value)); + interaction + .respond(filtered.map((choice: any) => ({ name: choice, value: choice }))) + .then(console.log) + .catch(console.error); + } +} diff --git a/test/src/events/missingPermissions.ts b/test/src/events/missingPermissions.ts index ade43094..ed9bc765 100644 --- a/test/src/events/missingPermissions.ts +++ b/test/src/events/missingPermissions.ts @@ -10,8 +10,6 @@ export default class Ready extends Event { }); } execute(i: CommandInteraction, permission: string) { - console.log(permission); - i.reply('Missing permissions !'); } } diff --git a/typings/events/interactionCommandOrAutocompleteCreate.d.ts b/typings/events/interactionCommandOrAutocompleteCreate.d.ts new file mode 100644 index 00000000..8f1679d4 --- /dev/null +++ b/typings/events/interactionCommandOrAutocompleteCreate.d.ts @@ -0,0 +1,4 @@ +import { AutocompleteInteraction } from 'discord.js'; +import type { ShewenyClient } from '..'; +import type { CommandInteraction, ContextMenuInteraction } from 'discord.js'; +export default function run(client: ShewenyClient, interaction: CommandInteraction | ContextMenuInteraction | AutocompleteInteraction): Promise; diff --git a/typings/events/interactionCreate.d.ts b/typings/events/interactionCreate.d.ts index 45b1a127..b4f0cba1 100644 --- a/typings/events/interactionCreate.d.ts +++ b/typings/events/interactionCreate.d.ts @@ -1,3 +1,3 @@ -import type { Interaction } from "discord.js"; -import type { ShewenyClient } from "../client/Client"; +import type { Interaction } from 'discord.js'; +import type { ShewenyClient } from '../client/Client'; export default function run(client: ShewenyClient, interaction: Interaction): boolean; diff --git a/typings/structures/Command.d.ts b/typings/structures/Command.d.ts index 2dc4d1d2..65b743fd 100644 --- a/typings/structures/Command.d.ts +++ b/typings/structures/Command.d.ts @@ -4,7 +4,7 @@ import { COMMAND_CHANNEL } from '../constants/constants'; import type { ShewenyClient } from '../client/Client'; import type { MessageCommandOptionData, CommandMessageArgsResolved } from '../typescript/interfaces'; import type { CommandData, CommandType } from '../typescript/types'; -import type { ApplicationCommandOptionData, CommandInteraction, ContextMenuInteraction, Message, PermissionString } from 'discord.js'; +import type { ApplicationCommandOptionData, CommandInteraction, ContextMenuInteraction, Message, PermissionString, AutocompleteInteraction } from 'discord.js'; import type { CommandsManager } from '..'; /** * Represents an Command structure @@ -98,6 +98,7 @@ export declare abstract class Command extends BaseStructure { * @returns {any | Promise} */ before?(interaction: CommandInteraction | ContextMenuInteraction | Message): any | Promise; + onAutocomplete?(interaction: AutocompleteInteraction): any | Promise; /** * Main function `execute` for the commands * @param {CommandInteraction | ContextMenuInteraction | Message} interaction Interaction