Skip to content

Commit 11a5b10

Browse files
committed
Revert "Revert "Revert "feat(MessageCommandHandler): arguments support"""
This reverts commit dd60474.
1 parent 631e4ca commit 11a5b10

12 files changed

+16
-284
lines changed

src/handlers/MessageCommandHandler.ts

+5-57
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,14 @@
1-
import { Client, Collection, CommandInteractionOptionResolver, Guild, Message, MessageActionRow, MessageSelectMenu, SelectMenuInteraction, TextChannel, User } from 'discord.js';
1+
import { Collection, CommandInteractionOptionResolver, Message } from 'discord.js';
22
import type { GClient } from '../lib/GClient';
33
import { CommandContext } from '../lib/structures/contexts/CommandContext';
44
import { CommandType } from '../lib/structures/Command';
5+
import { ArgumentType } from '../lib/structures/Argument';
56
import { Commands } from '../lib/managers/CommandManager';
67
import { Handlers } from '../lib/managers/HandlerManager';
78
import { Logger, Events } from '../lib/util/logger/Logger';
8-
import { Argument, ArgumentType } from '../lib/structures/Argument';
9-
import { MessageArgumentTypeBase, MessageArgumentTypes } from '../lib/structures/arguments/base';
10-
import { Util } from '../lib/util/Util';
119

1210
const cooldowns = new Collection<string, Collection<string, number>>();
1311

14-
const checkValidation = async(arg: MessageArgumentTypes, content: string, client: Client, guild: Guild, argument: Argument, channel: TextChannel, user: User) => {
15-
if (!content) {
16-
const text = `${user.toString()}, please define argument \`${argument.name}\`, type: ${Util.toPascalCase(ArgumentType[argument.type.toString()])}`
17-
if (argument.type === ArgumentType.STRING && argument.choices?.length !== 0) {
18-
const menu = new MessageSelectMenu()
19-
.setCustomId('argument_choices')
20-
.setMaxValues(1)
21-
.setMinValues(0)
22-
.setPlaceholder('Select a choice');
23-
24-
menu.setOptions(
25-
argument.choices.map(
26-
ch => ({
27-
label: ch.name,
28-
value: ch.value
29-
})
30-
)
31-
);
32-
33-
const message = await channel.send({
34-
content: text,
35-
components: [ new MessageActionRow().addComponents(menu) ]
36-
});
37-
38-
const component: SelectMenuInteraction = await channel.awaitMessageComponent({ filter: (m) => m.componentType === 'SELECT_MENU' && m.user.id === user.id && m.channelId === channel.id && m.message.id === message.id && m.customId === 'argument_choices', time: 60000 }) as SelectMenuInteraction;
39-
40-
component.deferUpdate();
41-
content = component.values?.[0];
42-
} else {
43-
channel.send(text);
44-
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });
45-
46-
content = [...message.values()]?.[0]?.content;
47-
}
48-
}
49-
50-
if (!content) return channel.send(`${user.toString()}, Time :(`);
51-
52-
const validate = arg.validate(content);
53-
if (!validate) return checkValidation(arg, null, client, guild, argument, channel, user);
54-
55-
return arg.resolve(argument, client, guild);
56-
};
57-
5812
export async function MessageCommandHandler(
5913
message: Message,
6014
commandName: string,
@@ -81,7 +35,7 @@ export async function MessageCommandHandler(
8135
});
8236
}
8337

84-
/*args = args.map(
38+
args = args.map(
8539
(arg, i) =>
8640
new Object({
8741
name: command.arguments[i].name,
@@ -100,13 +54,7 @@ export async function MessageCommandHandler(
10054
if (args[0]?.type === ArgumentType.SUB_COMMAND_GROUP && args[0]?.options[0]?.type === ArgumentType.SUB_COMMAND)
10155
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
10256
// @ts-ignore
103-
args[0].options[0].options = args[0].options.splice(1);*/
104-
105-
for (const argument in command.arguments) {
106-
const arg = await MessageArgumentTypeBase.createArgument(command.arguments[argument].type);
107-
108-
args[argument] = await checkValidation(arg, args[argument] as string, client, message.guild, command.arguments[argument], message.channel as TextChannel, message.author);
109-
}
57+
args[0].options[0].options = args[0].options.splice(1);
11058

11159
let replied: Message;
11260
const ctx = new CommandContext(client, {
@@ -162,4 +110,4 @@ export async function MessageCommandHandler(
162110
Logger.emit(Events.COMMAND_HANDLER_RUN, ctx);
163111
Logger.debug(`Successfully ran command (${command.name}) for ${message.author.username}`);
164112
});
165-
}
113+
}

src/lib/structures/Provider.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,39 @@ export declare interface Provider {
2121
delete(...args): Promise<any> | any;
2222
}
2323

24+
const throwError = (error, name) => {
25+
const trace = Util.resolveValidationErrorTrace([name]);
26+
27+
Logger.error(error, trace);
28+
};
29+
2430
export class Provider extends EventEmitter {
2531
init() {
26-
Util.throwError('Init method is not implemented!', this.constructor.name);
32+
throwError('Init method is not implemented!', this.constructor.name);
2733
return;
2834
}
2935

3036
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3137
insert(...args) {
32-
Util.throwError('Insert method is not implemented!', this.constructor.name);
38+
throwError('Insert method is not implemented!', this.constructor.name);
3339
return;
3440
}
3541

3642
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3743
get(...args) {
38-
Util.throwError('Get method is not implemented!', this.constructor.name);
44+
throwError('Get method is not implemented!', this.constructor.name);
3945
return;
4046
}
4147

4248
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4349
update(...args) {
44-
Util.throwError('Update method is not implemented!', this.constructor.name);
50+
throwError('Update method is not implemented!', this.constructor.name);
4551
return;
4652
}
4753

4854
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4955
delete(...args) {
50-
Util.throwError('Delete method is not implemented!', this.constructor.name);
56+
throwError('Delete method is not implemented!', this.constructor.name);
5157
return;
5258
}
5359
}

src/lib/structures/arguments/Boolean.ts

-28
This file was deleted.

src/lib/structures/arguments/Channel.ts

-7
This file was deleted.

src/lib/structures/arguments/Integer.ts

-22
This file was deleted.

src/lib/structures/arguments/Mentionable.ts

-7
This file was deleted.

src/lib/structures/arguments/Number.ts

-22
This file was deleted.

src/lib/structures/arguments/Role.ts

-7
This file was deleted.

src/lib/structures/arguments/String.ts

-22
This file was deleted.

src/lib/structures/arguments/User.ts

-26
This file was deleted.

src/lib/structures/arguments/base.ts

-62
This file was deleted.

src/lib/util/Util.ts

-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Logger } from './logger/Logger';
2-
31
export class Util {
42
/**
53
* @deprecated We don't support arguments in object/array
@@ -96,22 +94,5 @@ export class Util {
9694

9795
static pad(number: number): string {
9896
return (number < 10 ? '0' : '') + number;
99-
}
100-
101-
static throwError(error, name): void {
102-
const trace = Util.resolveValidationErrorTrace([name]);
103-
104-
Logger.error(error, trace);
105-
}
106-
107-
static toPascalCase(input: string): string {
108-
return input
109-
.replace(new RegExp(/[-_]+/, 'g'), ' ')
110-
.replace(new RegExp(/[^\w\s]/, 'g'), '')
111-
.replace(
112-
new RegExp(/\s+(.)(\w*)/, 'g'),
113-
($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
114-
)
115-
.replace(new RegExp(/\w/), s => s.toUpperCase());
11697
}
11798
}

0 commit comments

Comments
 (0)