Skip to content

Commit

Permalink
Revert "Revert "Revert "feat(MessageCommandHandler): arguments suppor…
Browse files Browse the repository at this point in the history
…t"""

This reverts commit dd60474.
  • Loading branch information
xhyrom committed Feb 9, 2022
1 parent 631e4ca commit 11a5b10
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 284 deletions.
62 changes: 5 additions & 57 deletions src/handlers/MessageCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,14 @@
import { Client, Collection, CommandInteractionOptionResolver, Guild, Message, MessageActionRow, MessageSelectMenu, SelectMenuInteraction, TextChannel, User } from 'discord.js';
import { Collection, CommandInteractionOptionResolver, Message } from 'discord.js';
import type { GClient } from '../lib/GClient';
import { CommandContext } from '../lib/structures/contexts/CommandContext';
import { CommandType } from '../lib/structures/Command';
import { ArgumentType } from '../lib/structures/Argument';
import { Commands } from '../lib/managers/CommandManager';
import { Handlers } from '../lib/managers/HandlerManager';
import { Logger, Events } from '../lib/util/logger/Logger';
import { Argument, ArgumentType } from '../lib/structures/Argument';
import { MessageArgumentTypeBase, MessageArgumentTypes } from '../lib/structures/arguments/base';
import { Util } from '../lib/util/Util';

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

const checkValidation = async(arg: MessageArgumentTypes, content: string, client: Client, guild: Guild, argument: Argument, channel: TextChannel, user: User) => {
if (!content) {
const text = `${user.toString()}, please define argument \`${argument.name}\`, type: ${Util.toPascalCase(ArgumentType[argument.type.toString()])}`
if (argument.type === ArgumentType.STRING && argument.choices?.length !== 0) {
const menu = new MessageSelectMenu()
.setCustomId('argument_choices')
.setMaxValues(1)
.setMinValues(0)
.setPlaceholder('Select a choice');

menu.setOptions(
argument.choices.map(
ch => ({
label: ch.name,
value: ch.value
})
)
);

const message = await channel.send({
content: text,
components: [ new MessageActionRow().addComponents(menu) ]
});

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;

component.deferUpdate();
content = component.values?.[0];
} else {
channel.send(text);
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });

content = [...message.values()]?.[0]?.content;
}
}

if (!content) return channel.send(`${user.toString()}, Time :(`);

const validate = arg.validate(content);
if (!validate) return checkValidation(arg, null, client, guild, argument, channel, user);

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

export async function MessageCommandHandler(
message: Message,
commandName: string,
Expand All @@ -81,7 +35,7 @@ export async function MessageCommandHandler(
});
}

/*args = args.map(
args = args.map(
(arg, i) =>
new Object({
name: command.arguments[i].name,
Expand All @@ -100,13 +54,7 @@ export async function MessageCommandHandler(
if (args[0]?.type === ArgumentType.SUB_COMMAND_GROUP && args[0]?.options[0]?.type === ArgumentType.SUB_COMMAND)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
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);

args[argument] = await checkValidation(arg, args[argument] as string, client, message.guild, command.arguments[argument], message.channel as TextChannel, message.author);
}
args[0].options[0].options = args[0].options.splice(1);

let replied: Message;
const ctx = new CommandContext(client, {
Expand Down Expand Up @@ -162,4 +110,4 @@ export async function MessageCommandHandler(
Logger.emit(Events.COMMAND_HANDLER_RUN, ctx);
Logger.debug(`Successfully ran command (${command.name}) for ${message.author.username}`);
});
}
}
16 changes: 11 additions & 5 deletions src/lib/structures/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,39 @@ export declare interface Provider {
delete(...args): Promise<any> | any;
}

const throwError = (error, name) => {
const trace = Util.resolveValidationErrorTrace([name]);

Logger.error(error, trace);
};

export class Provider extends EventEmitter {
init() {
Util.throwError('Init method is not implemented!', this.constructor.name);
throwError('Init method is not implemented!', this.constructor.name);
return;
}

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

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

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

// eslint-disable-next-line @typescript-eslint/no-unused-vars
delete(...args) {
Util.throwError('Delete method is not implemented!', this.constructor.name);
throwError('Delete method is not implemented!', this.constructor.name);
return;
}
}
28 changes: 0 additions & 28 deletions src/lib/structures/arguments/Boolean.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/lib/structures/arguments/Channel.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/lib/structures/arguments/Integer.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/lib/structures/arguments/Mentionable.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/lib/structures/arguments/Number.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/lib/structures/arguments/Role.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/lib/structures/arguments/String.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/lib/structures/arguments/User.ts

This file was deleted.

62 changes: 0 additions & 62 deletions src/lib/structures/arguments/base.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/lib/util/Util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Logger } from './logger/Logger';

export class Util {
/**
* @deprecated We don't support arguments in object/array
Expand Down Expand Up @@ -96,22 +94,5 @@ export class Util {

static pad(number: number): string {
return (number < 10 ? '0' : '') + number;
}

static throwError(error, name): void {
const trace = Util.resolveValidationErrorTrace([name]);

Logger.error(error, trace);
}

static toPascalCase(input: string): string {
return input
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w*)/, 'g'),
($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
)
.replace(new RegExp(/\w/), s => s.toUpperCase());
}
}

0 comments on commit 11a5b10

Please sign in to comment.