Skip to content

Commit

Permalink
Autocomplete 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
Smaug6739 committed Nov 14, 2021
1 parent d11baff commit 66dd982
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 20 deletions.
@@ -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;

Expand All @@ -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
*/
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 10 additions & 11 deletions 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);
}
3 changes: 3 additions & 0 deletions src/structures/Command.ts
Expand Up @@ -17,6 +17,7 @@ import type {
ContextMenuInteraction,
Message,
PermissionString,
AutocompleteInteraction,
} from 'discord.js';
import type { CommandsManager } from '..';

Expand Down Expand Up @@ -149,6 +150,8 @@ export abstract class Command extends BaseStructure {
*/
before?(interaction: CommandInteraction | ContextMenuInteraction | Message): any | Promise<any>;

onAutocomplete?(interaction: AutocompleteInteraction): any | Promise<any>;

/**
* Main function `execute` for the commands
* @param {CommandInteraction | ContextMenuInteraction | Message} interaction Interaction
Expand Down
39 changes: 39 additions & 0 deletions 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);
}
}
2 changes: 0 additions & 2 deletions test/src/events/missingPermissions.ts
Expand Up @@ -10,8 +10,6 @@ export default class Ready extends Event {
});
}
execute(i: CommandInteraction, permission: string) {
console.log(permission);

i.reply('Missing permissions !');
}
}
4 changes: 4 additions & 0 deletions 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<any>;
4 changes: 2 additions & 2 deletions 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;
3 changes: 2 additions & 1 deletion typings/structures/Command.d.ts
Expand Up @@ -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
Expand Down Expand Up @@ -98,6 +98,7 @@ export declare abstract class Command extends BaseStructure {
* @returns {any | Promise<any>}
*/
before?(interaction: CommandInteraction | ContextMenuInteraction | Message): any | Promise<any>;
onAutocomplete?(interaction: AutocompleteInteraction): any | Promise<any>;
/**
* Main function `execute` for the commands
* @param {CommandInteraction | ContextMenuInteraction | Message} interaction Interaction
Expand Down

0 comments on commit 66dd982

Please sign in to comment.