Skip to content

Commit

Permalink
Merge pull request #4 from Neko-Life/master
Browse files Browse the repository at this point in the history
Slash command
  • Loading branch information
danbulant committed Mar 26, 2022
2 parents a4d6c28 + d7d10a1 commit 12fb09b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
25 changes: 15 additions & 10 deletions src/commands/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@ class Argument {
);
} else {
// Prompt the user for a new value
prompts.push(await msg.reply(stripIndents`
${empty ? this.prompt : valid ? valid : `You provided an invalid ${this.label}. Please try again.`}
${oneLine`
Respond with \`cancel\` to cancel the command.
${wait ? `The command will automatically be cancelled in ${this.wait} seconds.` : ''}
`}
`));
const mes = await msg.reply({ content: stripIndents`
${empty ? this.prompt : valid ? valid : `You provided an invalid ${this.label}. Please try again.`}
${oneLine`
Respond with \`cancel\` to cancel the command.
${wait ? `The command will automatically be cancelled in ${this.wait} seconds.` : ''}
`}
`, fetchReply: true});
prompts.push(mes);
this.message = mes;
}

// Get the user's response
Expand All @@ -241,7 +243,8 @@ class Argument {
value: null,
cancelled: 'time',
prompts,
answers
answers,
message: this.message
};
}

Expand All @@ -251,7 +254,8 @@ class Argument {
value: null,
cancelled: 'user',
prompts,
answers
answers,
message: this.message
};
}

Expand All @@ -264,7 +268,8 @@ class Argument {
value: await this.parse(val, msg),
cancelled: null,
prompts,
answers
answers,
message: this.message
};
}

Expand Down
32 changes: 20 additions & 12 deletions src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,31 @@ class CommandoRegistry {
var commands = [];
for(const command of this.commands.values()) {
for(const interaction of command.interactions) {
commands.push({
const obj = {
name: interaction.name || command.name,
description: interaction.description || command.description,
defaultPermission: true,
type: [undefined, 'slash', 'user', 'message'].indexOf(interaction.type),
options: command.argsCollector ?
command.argsCollector.args.map(arg => Object.assign({
name: arg.key, description: arg.prompt,
required: !arg.default
}, arg.oneOf ? { choices: arg.oneOf.map(choice => ({ name: choice, value: choice })) } : {},
required: [null, undefined].includes(arg.default)
}, arg.oneOf ? { choices: arg.oneOf.map(choice => ({ name: choice, value: choice })) } :
arg.autocomplete ? { autocomplete: true } : {},
arg.slash || {}, (arg.type && arg.type.slash) || {})).map(arg => Object.assign(arg, { type: [
undefined, 'SUB_COMMAND', 'SUB_COMMAND_GROUP', 'STRING',
'INTEGER', 'BOOLEAN', 'USER', 'CHANNEL', 'ROLE',
'MENTIONABLE', 'NUMBER'
].indexOf(arg.type) })) :
[]
});
].indexOf(!arg.type ? "STRING" : arg.type) })) :
command.run.length === 2 ?
[{ name: "args", description: "Arguments", type: 3, required: true }] : []
};
if(obj.type > 1) {
delete obj.description;
delete obj.options;
}
if(obj.options?.length) obj.options.sort((a, b) => b.required - a.required);
commands.push(obj);
}
}
return commands;
Expand All @@ -113,7 +121,7 @@ class CommandoRegistry {
async registerSlashGlobally() {
this.client.emit('debug', 'Registering slash commands');
await this.rest.put(
Routes.applicationGuildCommands(this.client.user.id),
Routes.applicationCommands(this.client.user.id),
{ body: this._prepareCommandsForSlash() }
);
this.client.emit('debug', `Registered slash commands (globally)`);
Expand Down Expand Up @@ -204,11 +212,11 @@ class CommandoRegistry {
throw new Error(`A command with the name/alias "${alias}" is already registered.`);
}
}
for(const interaction of command.interactions) {
if(this.commands.some(cmd => cmd.name === interaction.name || (cmd.interactions && cmd.interactions.some((int) => int.name === interaction.name)))) {
throw new Error(`An interaction with the name "${interaction.name}" is already registered.`);
}
}
// for(const interaction of command.interactions) {
// if(this.commands.some(cmd => cmd.name === interaction.name || (cmd.interactions && cmd.interactions.some((int) => int.name === interaction.name)))) {
// throw new Error(`An interaction with the name "${interaction.name}" is already registered.`);
// }
// }
const group = this.groups.find(grp => grp.id === command.groupID);
if(!group) throw new Error(`Group "${command.groupID}" is not registered.`);
if(group.commands.some(cmd => cmd.memberName === command.memberName)) {
Expand Down
22 changes: 21 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module '@iceprod/discord.js-commando' {
import { Client, ClientEvents, ClientOptions, Collection, Guild, GuildResolvable, Message, MessageAttachment, MessageEditOptions, MessageEmbed, MessageOptions, MessageAdditions, MessageReaction, PermissionResolvable, PermissionString, StringResolvable, User, UserResolvable } from 'discord.js';
import { Client, ClientEvents, ClientOptions, Collection, Guild, GuildResolvable, Message, MessageAttachment, MessageEditOptions, MessageEmbed, MessageOptions, MessageAdditions, MessageReaction, PermissionResolvable, PermissionString, StringResolvable, User, UserResolvable, Interaction, AutocompleteInteraction } from 'discord.js';

export class Argument {
private constructor(client: CommandoClient, info: ArgumentInfo);
Expand All @@ -26,6 +26,7 @@ declare module '@iceprod/discord.js-commando' {
public obtain(msg: CommandoMessage, val?: string, promptLimit?: number): Promise<ArgumentResult>;
public parse(val: string, msg: CommandoMessage): any | Promise<any>;
public validate(val: string, msg: CommandoMessage): boolean | string | Promise<boolean | string>;
public autocomplete?(interaction: Interaction, focus: AutocompleteInteraction["options"]["data"][0]): Promise<AutocompleteRespond[]>;
}

export abstract class Service {
Expand Down Expand Up @@ -100,6 +101,8 @@ declare module '@iceprod/discord.js-commando' {
public throttling: ThrottlingOptions;
public unknown: boolean;
public userPermissions: PermissionResolvable[];
public interactions?: InteractionsInfo[];
public argsCollector?: ArgumentCollector;

public hasPermission(message: CommandoMessage, ownerOverride?: boolean): boolean | string;
public isEnabledIn(guild: GuildResolvable, bypassGroup?: boolean): boolean;
Expand Down Expand Up @@ -299,6 +302,11 @@ declare module '@iceprod/discord.js-commando' {
public registerServicesIn(path: string): CommandoRegistry;
public unregisterService(service: Service): CommandoRegistry;
public reregisterService(service: Service, current: Service): CommandoRegistry;

public _prepareCommandsForSlash(): any[];
public registerSlashInGuild(guild: GuildResolvable): Promise<void>;
public registerSlashGlobally(): Promise<void>;
public resolveFromInteraction(interaction: Interaction | Command | string): Command;
}

export class FriendlyError extends Error {
Expand Down Expand Up @@ -391,6 +399,11 @@ declare module '@iceprod/discord.js-commando' {
answers: Message[];
}

export interface AutocompleteRespond {
name: string;
value: string;
}

export interface ArgumentInfo {
key: string;
label?: string;
Expand All @@ -406,6 +419,7 @@ declare module '@iceprod/discord.js-commando' {
parse?: Function;
isEmpty?: Function;
wait?: number;
autocomplete?(interaction: Interaction, focus: AutocompleteInteraction["options"]["data"][0]): Promise<AutocompleteRespond[]>;
}

export interface ArgumentResult {
Expand All @@ -416,6 +430,11 @@ declare module '@iceprod/discord.js-commando' {
}

type CommandGroupResolvable = CommandGroup | string;
interface InteractionsInfo {
type: "user" | "message" | "slash",
name?: string,
description?: string
}

export interface CommandInfo {
name: string;
Expand Down Expand Up @@ -443,6 +462,7 @@ declare module '@iceprod/discord.js-commando' {
guarded?: boolean;
hidden?: boolean;
unknown?: boolean;
interactions?: InteractionsInfo[];
}

export interface CommandoClientOptions extends ClientOptions {
Expand Down

0 comments on commit 12fb09b

Please sign in to comment.