Skip to content

Commit d82ee79

Browse files
committed
feat: argumented message commands
1 parent 5a13881 commit d82ee79

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed
File renamed without changes.

apps/test-bot/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const commandkit = new CommandKit({
1414
validationsPath: `${__dirname}/validations`,
1515
devGuildIds: process.env.DEV_GUILD_ID?.split(',') ?? [],
1616
devUserIds: process.env.DEV_USER_ID?.split(',') ?? [],
17-
bulkRegister: false,
17+
bulkRegister: true,
1818
});
1919

2020
await client.login(process.env.TOKEN);

packages/commandkit/src/app/commands/Context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ export class Context<
335335
public isMiddleware(): this is MiddlewareContext<ExecutionMode> {
336336
return this instanceof MiddlewareContext;
337337
}
338+
339+
public args(): string[] {
340+
if (this.isMessage()) {
341+
return this.config.messageCommandParser!.getArgs();
342+
}
343+
344+
return [];
345+
}
338346
}
339347

340348
export class MiddlewareContext<

packages/commandkit/src/app/commands/MessageCommandParser.ts

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ export type MessageCommandOptionsSchema = Record<
2828
export class MessageCommandParser {
2929
#parsed: ParsedMessageCommand | null = null;
3030
#options: MessageCommandOptions | null = null;
31+
#args: string[] = [];
3132

3233
public constructor(
3334
public message: Message,
3435
private prefix: string[],
3536
private schema: (command: string) => MessageCommandOptionsSchema,
3637
) {}
3738

39+
public getArgs() {
40+
void this.parse();
41+
return this.#args;
42+
}
43+
3844
public get options() {
3945
if (!this.#options) {
4046
this.#options = new MessageCommandOptions(this);
@@ -89,6 +95,8 @@ export class MessageCommandParser {
8995
const parts = content.slice(prefix.length).trim().split(' ');
9096
const command = parts.shift();
9197

98+
this.#args = parts;
99+
92100
let subcommandGroup: string | undefined;
93101
let subcommand: string | undefined;
94102

@@ -109,49 +117,54 @@ export class MessageCommandParser {
109117

110118
const options = parts
111119
.map((part) => {
112-
const [name, value] = part.split(':');
113-
114-
if (!(name in schema)) return null;
115-
116-
switch (schema[name]) {
117-
case ApplicationCommandOptionType.Boolean:
118-
return { name, value: value === 'true' };
119-
case ApplicationCommandOptionType.Integer:
120-
return { name, value: parseInt(value, 10) };
121-
case ApplicationCommandOptionType.Number:
122-
return { name, value: parseFloat(value) };
123-
case ApplicationCommandOptionType.String:
124-
return { name, value };
125-
case ApplicationCommandOptionType.User:
126-
return {
127-
name,
128-
value: this.message.mentions.users.find((u) => {
129-
return u.id === value.replace(/[<@!>]/g, '');
130-
}),
131-
};
132-
case ApplicationCommandOptionType.Channel:
133-
return {
134-
name,
135-
value: this.message.mentions.channels.find((c) => {
136-
return c.id === value.replace(/[<#>]/g, '');
137-
}),
138-
};
139-
case ApplicationCommandOptionType.Role:
140-
return {
141-
name,
142-
value: this.message.mentions.roles.find((r) => {
143-
return r.id === value.replace(/[<@&>]/g, '');
144-
}),
145-
};
146-
case ApplicationCommandOptionType.Attachment:
147-
return {
148-
name,
149-
value: this.message.attachments.find((a) => {
150-
return a.name === value;
151-
}),
152-
};
153-
default:
154-
return null;
120+
try {
121+
const [name, value] = part.split(':');
122+
123+
if (!(name in schema)) return null;
124+
125+
switch (schema[name]) {
126+
case ApplicationCommandOptionType.Boolean:
127+
return { name, value: value === 'true' };
128+
case ApplicationCommandOptionType.Integer:
129+
return { name, value: parseInt(value, 10) };
130+
case ApplicationCommandOptionType.Number:
131+
return { name, value: parseFloat(value) };
132+
case ApplicationCommandOptionType.String:
133+
return { name, value };
134+
case ApplicationCommandOptionType.User:
135+
return {
136+
name,
137+
value: this.message.mentions.users.find((u) => {
138+
return u.id === value.replace(/[<@!>]/g, '');
139+
}),
140+
};
141+
case ApplicationCommandOptionType.Channel:
142+
return {
143+
name,
144+
value: this.message.mentions.channels.find((c) => {
145+
return c.id === value.replace(/[<#>]/g, '');
146+
}),
147+
};
148+
case ApplicationCommandOptionType.Role:
149+
return {
150+
name,
151+
value: this.message.mentions.roles.find((r) => {
152+
return r.id === value.replace(/[<@&>]/g, '');
153+
}),
154+
};
155+
case ApplicationCommandOptionType.Attachment:
156+
return {
157+
name,
158+
value: this.message.attachments.find((a) => {
159+
return a.name === value;
160+
}),
161+
};
162+
default:
163+
return null;
164+
}
165+
} catch {
166+
// Invalid option
167+
return null;
155168
}
156169
})
157170
.filter((v) => v !== null);

0 commit comments

Comments
 (0)