Skip to content

Commit

Permalink
feat: mentionable argument
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Feb 12, 2022
1 parent 848c1b6 commit 89df6df
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 20 deletions.
8 changes: 5 additions & 3 deletions src/handlers/MessageCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const checkValidation = async(arg: MessageArgumentTypes, content: string | Messa
channel.send(text);
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });

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

return arg.resolve(argument, client, guild);
return arg.resolve(argument);
};

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

for (const argument in command.arguments) {
const arg = await MessageArgumentTypeBase.createArgument(command.arguments[argument].type);
const arg = await MessageArgumentTypeBase.createArgument(command.arguments[argument].type, message.guild);

args[argument] = await checkValidation(arg, args[argument] as string, client, message.guild, command.arguments[argument], message.channel as TextChannel, message.author);
}
Expand Down
26 changes: 25 additions & 1 deletion src/lib/structures/arguments/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import type { Guild } from 'discord.js';
import { channelRegexp } from '../../util/regexes';
import { Argument, ArgumentType } from '../Argument';
import { MessageArgumentTypeBase } from './base';

export class ChannelType extends MessageArgumentTypeBase {
validate() {
value;
guild: Guild;
constructor(guild: Guild) {
super();

this.guild = guild;
}

validate(content: string): boolean {
const matches = channelRegexp.exec(content);
if (!matches || !this.guild.channels.cache.get(matches?.[1])) return false;

this.value = matches[1];

return true;
}

resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
channel: this.guild.channels.cache.get(this.value)
};
}
}
30 changes: 29 additions & 1 deletion src/lib/structures/arguments/Mentionable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import type { Client, Guild } from 'discord.js';
import { channelRegexp, mentionableRegexp } from '../../util/regexes';
import { Argument, ArgumentType } from '../Argument';
import { MessageArgumentTypeBase } from './base';

export class MentionableType extends MessageArgumentTypeBase {
validate() {
value;
guild: Guild;
client: Client;
constructor(guild: Guild) {
super();

this.client = guild.client;
this.guild = guild;
}

validate(content: string): boolean {
const matches = mentionableRegexp.exec(content);
if (!matches || (this.guild.roles.cache.get(matches?.[1]) || this.client.users.cache.get(matches?.[1]))) return false;

this.value = matches[1];

return true;
}

resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
user: this.client.users.cache.get(this.value),
member: this.guild.members.cache.get(this.value),
roles: this.guild.roles.cache.get(this.value),
};
}
}
26 changes: 25 additions & 1 deletion src/lib/structures/arguments/Role.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import type { Guild } from 'discord.js';
import { roleRegexp } from '../../util/regexes';
import { Argument, ArgumentType } from '../Argument';
import { MessageArgumentTypeBase } from './base';

export class RoleType extends MessageArgumentTypeBase {
validate() {
value;
guild: Guild;
constructor(guild: Guild) {
super();

this.guild = guild;
}

validate(content: string): boolean {
const matches = roleRegexp.exec(content);
if (!matches || !this.guild.roles.cache.get(matches?.[1])) return false;

this.value = matches[1];

return true;
}

resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
channel: this.guild.roles.cache.get(this.value)
};
}
}
24 changes: 16 additions & 8 deletions src/lib/structures/arguments/User.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import type { Client, Guild } from 'discord.js';
import { userRegexp } from '../../util/regexes';
import { Argument, ArgumentType } from '../Argument';
import { MessageArgumentTypeBase } from './base';

export class UserType extends MessageArgumentTypeBase {
matches;
value;
guild: Guild;
client: Client;
constructor(guild: Guild) {
super();

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

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

this.matches = matches;
this.value = matches[1];

return true;
}

resolve(argument: Argument, client: Client, guild: Guild) {
resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
user: client.users.cache.get(this.matches[1]),
member: guild.members?.cache?.get(this.matches[1])
user: this.client.users.cache.get(this.value),
member: this.guild.members.cache.get(this.value)
};
}
}
13 changes: 7 additions & 6 deletions src/lib/structures/arguments/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Guild } from 'discord.js';
import { Util } from '../../util/Util';
import { Argument, ArgumentType } from '../Argument';
import type { AttachmentType } from './Attachment';
Expand All @@ -20,43 +21,43 @@ export class MessageArgumentTypeBase {
}

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

static async createArgument(type: ArgumentType | keyof typeof ArgumentType) {
static async createArgument(type: ArgumentType | keyof typeof ArgumentType, guild: Guild) {
switch(type) {
case ArgumentType.BOOLEAN: {
const { BooleanType } = await import('./Boolean');
return new BooleanType();
}
case ArgumentType.CHANNEL: {
const { ChannelType } = await import('./Channel');
return new ChannelType();
return new ChannelType(guild);
}
case ArgumentType.INTEGER: {
const { IntegerType } = await import('./Integer');
return new IntegerType();
}
case ArgumentType.MENTIONABLE: {
const { MentionableType } = await import('./Mentionable');
return new MentionableType();
return new MentionableType(guild);
}
case ArgumentType.NUMBER: {
const { NumberType } = await import('./Number');
return new NumberType();
}
case ArgumentType.ROLE: {
const { RoleType } = await import('./Role');
return new RoleType();
return new RoleType(guild);
}
case ArgumentType.STRING: {
const { StringType } = await import('./String');
return new StringType();
}
case ArgumentType.USER: {
const { UserType } = await import('./User');
return new UserType();
return new UserType(guild);
}
// @ts-expect-error TODO: Use ArgumentType.ATTACHMENT | Need wait for https://github.com/Garlic-Team/gcommands/pull/314 to be merged (:
case 11: {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/util/regexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const userRegexp = /^(?:<@!?)?([0-9]+)>?$/;
export const roleRegexp = /^(?:<@&)?([0-9]+)>?$/;
export const channelRegexp = /^(?:<#)?([0-9]+)>?$/;
export const mentionableRegexp = /^(?:<@!?)?(?:<@&?)?([0-9]+)>?$/;

0 comments on commit 89df6df

Please sign in to comment.