Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
},
"engines": {
"node": ">=16.6.0"
}
},
"packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
}
23 changes: 23 additions & 0 deletions playground/src/interactions/commands/utils/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Command } from "djs-core";

export default new Command()
.setName("autocomplete")
.setDescription("This is an autocomplete command")
.addStringOption((option) =>
option
.setName("input")
.setDescription("This is an input")
.setAutocomplete(true)
.setRequired(true),
)
.run((client, interaction) => {
const input = interaction.options.getString("input");
return interaction.reply(`You selected ${input}`);
})
.autoComplete((client, interaction) => {
return interaction.respond([
{ name: "Option 1", value: "option1" },
{ name: "Option 2", value: "option2" },
{ name: "Option 3", value: "option3" },
]);
});
14 changes: 13 additions & 1 deletion playground/src/interactions/commands/utils/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ export default new SubCommandGroup()
.addSubcommand((sub) =>
sub.setName("select").setDescription("Create select menu"),
)
.addSubcommand((sub) => sub.setName("modal").setDescription("Create modal"));
.addSubcommand((sub) => sub.setName("modal").setDescription("Create modal"))
.addSubcommand((sub) =>
sub
.setName("autocomplete")
.setDescription("Create autocomplete")
.addStringOption((option) =>
option
.setName("input")
.setDescription("This is an input")
.setAutocomplete(true)
.setRequired(true),
),
);
18 changes: 18 additions & 0 deletions playground/src/interactions/commands/utils/handler/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import { SubCommand } from "djs-core";

export default new SubCommand()
.run((client, interaction) => {
const input = interaction.options.getString("input");
return interaction.reply({
content: `You have selected: ${input}`,
});
})
.autoComplete((client, interaction) => {
return interaction.respond([
{
name: "I am a fucking ping button",
value: "I am a fucking ping button",
},
]);
});
26 changes: 13 additions & 13 deletions src/class/interactions/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import {
CommandInteraction,
ChatInputCommandInteraction,
SlashCommandBuilder,
SlashCommandUserOption,
Expand All @@ -17,6 +16,7 @@ import {
SlashCommandChannelOption,
SlashCommandRoleOption,
SlashCommandMentionableOption,
AutocompleteInteraction,
} from "discord.js";
import BotClient from "../BotClient";

Expand All @@ -26,12 +26,12 @@ type CommandRunFn = (
) => unknown;
type CommandRunAutoCompleteFn = (
client: BotClient,
interaction: ChatInputCommandInteraction,
interaction: AutocompleteInteraction,
) => unknown;

export default class Command extends SlashCommandBuilder {
private runFn?: CommandRunFn;
private autocomplete?: CommandRunAutoCompleteFn;
private autocompleteFn?: CommandRunAutoCompleteFn;

constructor() {
super();
Expand All @@ -43,7 +43,7 @@ export default class Command extends SlashCommandBuilder {
}

autoComplete(fn: CommandRunAutoCompleteFn) {
this.autocomplete = fn;
this.autocompleteFn = fn;
return this;
}

Expand All @@ -70,16 +70,16 @@ export default class Command extends SlashCommandBuilder {
* DO NOT USE
* Internal method to execute the function
*/
executeAutoComplete(client: BotClient, interaction: CommandInteraction) {
if (
this.autocomplete &&
interaction instanceof ChatInputCommandInteraction
) {
return this.autocomplete(client, interaction);
}
if (interaction instanceof CommandInteraction) {
return interaction.reply("Aucune action définie");
executeAutoComplete(client: BotClient, interaction: AutocompleteInteraction) {
if (!this.autocompleteFn) {
client.logger.error(
`The command ${this.name} has no function to execute!`,
);
return interaction.respond([
{ name: "Autocomplete not found", value: "Autocomplete not found" },
]);
}
return this.autocompleteFn(client, interaction);
}

getDiscordCommand() {
Expand Down
30 changes: 29 additions & 1 deletion src/class/interactions/SubCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,37 @@
* Licence: on the GitHub
*/

import { ChatInputCommandInteraction, MessageFlags } from "discord.js";
import {
AutocompleteInteraction,
ChatInputCommandInteraction,
MessageFlags,
} from "discord.js";
import BotClient from "../BotClient";

type SubCommandRunFn = (
client: BotClient,
interaction: ChatInputCommandInteraction,
) => unknown;

type SubCommandAutoCompleteFn = (
client: BotClient,
interaction: AutocompleteInteraction,
) => unknown;

export default class SubCommand {
private runFn?: SubCommandRunFn;
private autoCompleteFn?: SubCommandAutoCompleteFn;

run(fn: SubCommandRunFn) {
this.runFn = fn;
return this;
}

autoComplete(fn: SubCommandAutoCompleteFn) {
this.autoCompleteFn = fn;
return this;
}

execute(client: BotClient, interaction: ChatInputCommandInteraction) {
if (!this.runFn) {
client.logger.error("The subcommand has no function to execute!");
Expand All @@ -30,4 +45,17 @@ export default class SubCommand {
}
return this.runFn(client, interaction);
}

executeAutoComplete(client: BotClient, interaction: AutocompleteInteraction) {
if (!this.autoCompleteFn) {
client.logger.error("The subcommand has no function to execute!");
return interaction.respond([
{
name: "The subcommand has no function to execute!",
value: "The subcommand has no function to execute!",
},
]);
}
return this.autoCompleteFn(client, interaction);
}
}
10 changes: 10 additions & 0 deletions src/handlers/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ export default class CommandHandler extends Handler {
this.client.on(
Events.InteractionCreate,
async (interaction: Interaction) => {
if (interaction.isAutocomplete()) {
const command: Command = this.collection.get(
interaction.commandName,
) as Command;
if (!command)
return interaction.respond([
{ name: "Command not found", value: "Command not found" },
]);
return command.executeAutoComplete(this.client, interaction);
}
if (!interaction.isCommand()) return;
if (interaction.isContextMenuCommand()) return;
if (interaction.options.getSubcommand(false)) return;
Expand Down
10 changes: 10 additions & 0 deletions src/handlers/SubCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ export default class SubCommandHandler extends Handler {
this.client.on(
Events.InteractionCreate,
async (interaction: Interaction) => {
if (interaction.isAutocomplete()) {
const subCommand: SubCommand | unknown = this.collection.get(
`${interaction.commandName}.${interaction.options.getSubcommand()}`,
);
if (!subCommand || !(subCommand instanceof SubCommand))
return interaction.respond([
{ name: "SubCommand not found", value: "SubCommand not found" },
]);
return subCommand.executeAutoComplete(this.client, interaction);
}
if (!interaction.isCommand()) return;
if (interaction.isContextMenuCommand()) return;
if (!interaction.options.getSubcommand(false)) return;
Expand Down