Skip to content

Commit 12fb09b

Browse files
authored
Merge pull request #4 from Neko-Life/master
Slash command
2 parents a4d6c28 + d7d10a1 commit 12fb09b

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

src/commands/argument.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ class Argument {
216216
);
217217
} else {
218218
// Prompt the user for a new value
219-
prompts.push(await msg.reply(stripIndents`
220-
${empty ? this.prompt : valid ? valid : `You provided an invalid ${this.label}. Please try again.`}
221-
${oneLine`
222-
Respond with \`cancel\` to cancel the command.
223-
${wait ? `The command will automatically be cancelled in ${this.wait} seconds.` : ''}
224-
`}
225-
`));
219+
const mes = await msg.reply({ content: stripIndents`
220+
${empty ? this.prompt : valid ? valid : `You provided an invalid ${this.label}. Please try again.`}
221+
${oneLine`
222+
Respond with \`cancel\` to cancel the command.
223+
${wait ? `The command will automatically be cancelled in ${this.wait} seconds.` : ''}
224+
`}
225+
`, fetchReply: true});
226+
prompts.push(mes);
227+
this.message = mes;
226228
}
227229

228230
// Get the user's response
@@ -241,7 +243,8 @@ class Argument {
241243
value: null,
242244
cancelled: 'time',
243245
prompts,
244-
answers
246+
answers,
247+
message: this.message
245248
};
246249
}
247250

@@ -251,7 +254,8 @@ class Argument {
251254
value: null,
252255
cancelled: 'user',
253256
prompts,
254-
answers
257+
answers,
258+
message: this.message
255259
};
256260
}
257261

@@ -264,7 +268,8 @@ class Argument {
264268
value: await this.parse(val, msg),
265269
cancelled: null,
266270
prompts,
267-
answers
271+
answers,
272+
message: this.message
268273
};
269274
}
270275

src/registry.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,31 @@ class CommandoRegistry {
7070
var commands = [];
7171
for(const command of this.commands.values()) {
7272
for(const interaction of command.interactions) {
73-
commands.push({
73+
const obj = {
7474
name: interaction.name || command.name,
7575
description: interaction.description || command.description,
7676
defaultPermission: true,
7777
type: [undefined, 'slash', 'user', 'message'].indexOf(interaction.type),
7878
options: command.argsCollector ?
7979
command.argsCollector.args.map(arg => Object.assign({
8080
name: arg.key, description: arg.prompt,
81-
required: !arg.default
82-
}, arg.oneOf ? { choices: arg.oneOf.map(choice => ({ name: choice, value: choice })) } : {},
81+
required: [null, undefined].includes(arg.default)
82+
}, arg.oneOf ? { choices: arg.oneOf.map(choice => ({ name: choice, value: choice })) } :
83+
arg.autocomplete ? { autocomplete: true } : {},
8384
arg.slash || {}, (arg.type && arg.type.slash) || {})).map(arg => Object.assign(arg, { type: [
8485
undefined, 'SUB_COMMAND', 'SUB_COMMAND_GROUP', 'STRING',
8586
'INTEGER', 'BOOLEAN', 'USER', 'CHANNEL', 'ROLE',
8687
'MENTIONABLE', 'NUMBER'
87-
].indexOf(arg.type) })) :
88-
[]
89-
});
88+
].indexOf(!arg.type ? "STRING" : arg.type) })) :
89+
command.run.length === 2 ?
90+
[{ name: "args", description: "Arguments", type: 3, required: true }] : []
91+
};
92+
if(obj.type > 1) {
93+
delete obj.description;
94+
delete obj.options;
95+
}
96+
if(obj.options?.length) obj.options.sort((a, b) => b.required - a.required);
97+
commands.push(obj);
9098
}
9199
}
92100
return commands;
@@ -113,7 +121,7 @@ class CommandoRegistry {
113121
async registerSlashGlobally() {
114122
this.client.emit('debug', 'Registering slash commands');
115123
await this.rest.put(
116-
Routes.applicationGuildCommands(this.client.user.id),
124+
Routes.applicationCommands(this.client.user.id),
117125
{ body: this._prepareCommandsForSlash() }
118126
);
119127
this.client.emit('debug', `Registered slash commands (globally)`);
@@ -204,11 +212,11 @@ class CommandoRegistry {
204212
throw new Error(`A command with the name/alias "${alias}" is already registered.`);
205213
}
206214
}
207-
for(const interaction of command.interactions) {
208-
if(this.commands.some(cmd => cmd.name === interaction.name || (cmd.interactions && cmd.interactions.some((int) => int.name === interaction.name)))) {
209-
throw new Error(`An interaction with the name "${interaction.name}" is already registered.`);
210-
}
211-
}
215+
// for(const interaction of command.interactions) {
216+
// if(this.commands.some(cmd => cmd.name === interaction.name || (cmd.interactions && cmd.interactions.some((int) => int.name === interaction.name)))) {
217+
// throw new Error(`An interaction with the name "${interaction.name}" is already registered.`);
218+
// }
219+
// }
212220
const group = this.groups.find(grp => grp.id === command.groupID);
213221
if(!group) throw new Error(`Group "${command.groupID}" is not registered.`);
214222
if(group.commands.some(cmd => cmd.memberName === command.memberName)) {

typings/index.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module '@iceprod/discord.js-commando' {
2-
import { Client, ClientEvents, ClientOptions, Collection, Guild, GuildResolvable, Message, MessageAttachment, MessageEditOptions, MessageEmbed, MessageOptions, MessageAdditions, MessageReaction, PermissionResolvable, PermissionString, StringResolvable, User, UserResolvable } from 'discord.js';
2+
import { Client, ClientEvents, ClientOptions, Collection, Guild, GuildResolvable, Message, MessageAttachment, MessageEditOptions, MessageEmbed, MessageOptions, MessageAdditions, MessageReaction, PermissionResolvable, PermissionString, StringResolvable, User, UserResolvable, Interaction, AutocompleteInteraction } from 'discord.js';
33

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

3132
export abstract class Service {
@@ -100,6 +101,8 @@ declare module '@iceprod/discord.js-commando' {
100101
public throttling: ThrottlingOptions;
101102
public unknown: boolean;
102103
public userPermissions: PermissionResolvable[];
104+
public interactions?: InteractionsInfo[];
105+
public argsCollector?: ArgumentCollector;
103106

104107
public hasPermission(message: CommandoMessage, ownerOverride?: boolean): boolean | string;
105108
public isEnabledIn(guild: GuildResolvable, bypassGroup?: boolean): boolean;
@@ -299,6 +302,11 @@ declare module '@iceprod/discord.js-commando' {
299302
public registerServicesIn(path: string): CommandoRegistry;
300303
public unregisterService(service: Service): CommandoRegistry;
301304
public reregisterService(service: Service, current: Service): CommandoRegistry;
305+
306+
public _prepareCommandsForSlash(): any[];
307+
public registerSlashInGuild(guild: GuildResolvable): Promise<void>;
308+
public registerSlashGlobally(): Promise<void>;
309+
public resolveFromInteraction(interaction: Interaction | Command | string): Command;
302310
}
303311

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

402+
export interface AutocompleteRespond {
403+
name: string;
404+
value: string;
405+
}
406+
394407
export interface ArgumentInfo {
395408
key: string;
396409
label?: string;
@@ -406,6 +419,7 @@ declare module '@iceprod/discord.js-commando' {
406419
parse?: Function;
407420
isEmpty?: Function;
408421
wait?: number;
422+
autocomplete?(interaction: Interaction, focus: AutocompleteInteraction["options"]["data"][0]): Promise<AutocompleteRespond[]>;
409423
}
410424

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

418432
type CommandGroupResolvable = CommandGroup | string;
433+
interface InteractionsInfo {
434+
type: "user" | "message" | "slash",
435+
name?: string,
436+
description?: string
437+
}
419438

420439
export interface CommandInfo {
421440
name: string;
@@ -443,6 +462,7 @@ declare module '@iceprod/discord.js-commando' {
443462
guarded?: boolean;
444463
hidden?: boolean;
445464
unknown?: boolean;
465+
interactions?: InteractionsInfo[];
446466
}
447467

448468
export interface CommandoClientOptions extends ClientOptions {

0 commit comments

Comments
 (0)