Skip to content

Commit 89df6df

Browse files
committed
feat: mentionable argument
1 parent 848c1b6 commit 89df6df

File tree

7 files changed

+111
-20
lines changed

7 files changed

+111
-20
lines changed

src/handlers/MessageCommandHandler.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ const checkValidation = async(arg: MessageArgumentTypes, content: string | Messa
4848
channel.send(text);
4949
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });
5050

51-
// @ts-expect-error TODO: Use ArgumentType.ATTACHMENT | Need wait for https://github.com/Garlic-Team/gcommands/pull/314 to be merged (:
51+
/**
52+
* TODO: Use ArgumentType.ATTACHMENT | Need wait for https://github.com/Garlic-Team/gcommands/pull/314 to be merged (:
53+
* @ts-expect-error */
5254
if (argument.type == 11) {
5355
const attachments = [...message.values()]?.[0]?.attachments;
5456
content = attachments ? [...attachments.values()][0] : null;
@@ -60,7 +62,7 @@ const checkValidation = async(arg: MessageArgumentTypes, content: string | Messa
6062
const validate = arg instanceof AttachmentType ? arg.validate(content) : arg.validate(content as string);
6163
if (!validate) return checkValidation(arg, null, client, guild, argument, channel, user);
6264

63-
return arg.resolve(argument, client, guild);
65+
return arg.resolve(argument);
6466
};
6567

6668
export async function MessageCommandHandler(
@@ -111,7 +113,7 @@ export async function MessageCommandHandler(
111113
args[0].options[0].options = args[0].options.splice(1);*/
112114

113115
for (const argument in command.arguments) {
114-
const arg = await MessageArgumentTypeBase.createArgument(command.arguments[argument].type);
116+
const arg = await MessageArgumentTypeBase.createArgument(command.arguments[argument].type, message.guild);
115117

116118
args[argument] = await checkValidation(arg, args[argument] as string, client, message.guild, command.arguments[argument], message.channel as TextChannel, message.author);
117119
}
+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
import type { Guild } from 'discord.js';
2+
import { channelRegexp } from '../../util/regexes';
3+
import { Argument, ArgumentType } from '../Argument';
14
import { MessageArgumentTypeBase } from './base';
25

36
export class ChannelType extends MessageArgumentTypeBase {
4-
validate() {
7+
value;
8+
guild: Guild;
9+
constructor(guild: Guild) {
10+
super();
11+
12+
this.guild = guild;
13+
}
14+
15+
validate(content: string): boolean {
16+
const matches = channelRegexp.exec(content);
17+
if (!matches || !this.guild.channels.cache.get(matches?.[1])) return false;
18+
19+
this.value = matches[1];
20+
521
return true;
622
}
23+
24+
resolve(argument: Argument) {
25+
return {
26+
...argument.toJSON(),
27+
type: ArgumentType[argument.type],
28+
channel: this.guild.channels.cache.get(this.value)
29+
};
30+
}
731
}
+29-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
import type { Client, Guild } from 'discord.js';
2+
import { channelRegexp, mentionableRegexp } from '../../util/regexes';
3+
import { Argument, ArgumentType } from '../Argument';
14
import { MessageArgumentTypeBase } from './base';
25

36
export class MentionableType extends MessageArgumentTypeBase {
4-
validate() {
7+
value;
8+
guild: Guild;
9+
client: Client;
10+
constructor(guild: Guild) {
11+
super();
12+
13+
this.client = guild.client;
14+
this.guild = guild;
15+
}
16+
17+
validate(content: string): boolean {
18+
const matches = mentionableRegexp.exec(content);
19+
if (!matches || (this.guild.roles.cache.get(matches?.[1]) || this.client.users.cache.get(matches?.[1]))) return false;
20+
21+
this.value = matches[1];
22+
523
return true;
624
}
25+
26+
resolve(argument: Argument) {
27+
return {
28+
...argument.toJSON(),
29+
type: ArgumentType[argument.type],
30+
user: this.client.users.cache.get(this.value),
31+
member: this.guild.members.cache.get(this.value),
32+
roles: this.guild.roles.cache.get(this.value),
33+
};
34+
}
735
}

src/lib/structures/arguments/Role.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
import type { Guild } from 'discord.js';
2+
import { roleRegexp } from '../../util/regexes';
3+
import { Argument, ArgumentType } from '../Argument';
14
import { MessageArgumentTypeBase } from './base';
25

36
export class RoleType extends MessageArgumentTypeBase {
4-
validate() {
7+
value;
8+
guild: Guild;
9+
constructor(guild: Guild) {
10+
super();
11+
12+
this.guild = guild;
13+
}
14+
15+
validate(content: string): boolean {
16+
const matches = roleRegexp.exec(content);
17+
if (!matches || !this.guild.roles.cache.get(matches?.[1])) return false;
18+
19+
this.value = matches[1];
20+
521
return true;
622
}
23+
24+
resolve(argument: Argument) {
25+
return {
26+
...argument.toJSON(),
27+
type: ArgumentType[argument.type],
28+
channel: this.guild.roles.cache.get(this.value)
29+
};
30+
}
731
}

src/lib/structures/arguments/User.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
import type { Client, Guild } from 'discord.js';
2+
import { userRegexp } from '../../util/regexes';
23
import { Argument, ArgumentType } from '../Argument';
34
import { MessageArgumentTypeBase } from './base';
45

56
export class UserType extends MessageArgumentTypeBase {
6-
matches;
7+
value;
8+
guild: Guild;
9+
client: Client;
10+
constructor(guild: Guild) {
11+
super();
712

8-
validate(content: string): boolean {
9-
const matches = content?.match(/([0-9]+)/);
13+
this.client = guild.client;
14+
this.guild = guild;
15+
}
1016

11-
if (!matches) return false;
17+
validate(content: string): boolean {
18+
const matches = userRegexp.exec(content);
19+
if (!matches || !this.client.users.cache.get(matches?.[1])) return false;
1220

13-
this.matches = matches;
21+
this.value = matches[1];
1422

1523
return true;
1624
}
1725

18-
resolve(argument: Argument, client: Client, guild: Guild) {
26+
resolve(argument: Argument) {
1927
return {
2028
...argument.toJSON(),
2129
type: ArgumentType[argument.type],
22-
user: client.users.cache.get(this.matches[1]),
23-
member: guild.members?.cache?.get(this.matches[1])
30+
user: this.client.users.cache.get(this.value),
31+
member: this.guild.members.cache.get(this.value)
2432
};
2533
}
2634
}

src/lib/structures/arguments/base.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Guild } from 'discord.js';
12
import { Util } from '../../util/Util';
23
import { Argument, ArgumentType } from '../Argument';
34
import type { AttachmentType } from './Attachment';
@@ -20,43 +21,43 @@ export class MessageArgumentTypeBase {
2021
}
2122

2223
// eslint-disable-next-line @typescript-eslint/no-unused-vars
23-
resolve(argument: Argument, ...args) {
24+
resolve(argument: Argument) {
2425
Util.throwError('Resolve method is not implemented!', this.constructor.name);
2526
}
2627

27-
static async createArgument(type: ArgumentType | keyof typeof ArgumentType) {
28+
static async createArgument(type: ArgumentType | keyof typeof ArgumentType, guild: Guild) {
2829
switch(type) {
2930
case ArgumentType.BOOLEAN: {
3031
const { BooleanType } = await import('./Boolean');
3132
return new BooleanType();
3233
}
3334
case ArgumentType.CHANNEL: {
3435
const { ChannelType } = await import('./Channel');
35-
return new ChannelType();
36+
return new ChannelType(guild);
3637
}
3738
case ArgumentType.INTEGER: {
3839
const { IntegerType } = await import('./Integer');
3940
return new IntegerType();
4041
}
4142
case ArgumentType.MENTIONABLE: {
4243
const { MentionableType } = await import('./Mentionable');
43-
return new MentionableType();
44+
return new MentionableType(guild);
4445
}
4546
case ArgumentType.NUMBER: {
4647
const { NumberType } = await import('./Number');
4748
return new NumberType();
4849
}
4950
case ArgumentType.ROLE: {
5051
const { RoleType } = await import('./Role');
51-
return new RoleType();
52+
return new RoleType(guild);
5253
}
5354
case ArgumentType.STRING: {
5455
const { StringType } = await import('./String');
5556
return new StringType();
5657
}
5758
case ArgumentType.USER: {
5859
const { UserType } = await import('./User');
59-
return new UserType();
60+
return new UserType(guild);
6061
}
6162
// @ts-expect-error TODO: Use ArgumentType.ATTACHMENT | Need wait for https://github.com/Garlic-Team/gcommands/pull/314 to be merged (:
6263
case 11: {

src/lib/util/regexes.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const userRegexp = /^(?:<@!?)?([0-9]+)>?$/;
2+
export const roleRegexp = /^(?:<@&)?([0-9]+)>?$/;
3+
export const channelRegexp = /^(?:<#)?([0-9]+)>?$/;
4+
export const mentionableRegexp = /^(?:<@!?)?(?:<@&?)?([0-9]+)>?$/;

0 commit comments

Comments
 (0)