Skip to content

Commit

Permalink
feat: types
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Feb 3, 2022
1 parent 91ae0ef commit 269c69a
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 28 deletions.
30 changes: 25 additions & 5 deletions src/handlers/MessageCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { Collection, CommandInteractionOptionResolver, Message } from 'discord.js';
import { Client, Collection, CommandInteractionOptionResolver, Guild, Message, TextChannel, User } 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 from 'js-logger';
import { UserType } from '../lib/structures/arguments/User';
import type { Argument } from '../lib/structures/Argument';

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

const checkValidation = async(arg: UserType, content: string, client: Client, guild: Guild, argument: Argument, channel: TextChannel, user: User) => {
if (!content) {
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;
}

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 @@ -35,7 +49,7 @@ export async function MessageCommandHandler(
});
}

args = args.map(
/*args = args.map(
(arg, i) =>
new Object({
name: command.arguments[i].name,
Expand All @@ -54,7 +68,13 @@ 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);
args[0].options[0].options = args[0].options.splice(1);*/

for (const argument in command.arguments) {
const arg = new UserType();

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

let replied: Message;
const ctx = new CommandContext(client, {
Expand Down Expand Up @@ -106,4 +126,4 @@ export async function MessageCommandHandler(
.then(() => {
Logger.debug(`Successfully ran command (${command.name}) for ${message.author.username}`);
});
}
}
2 changes: 1 addition & 1 deletion src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Command {
this.name = options.name || Command.defaults?.name;
this.description = options.description || Command.defaults?.description;
this.type = options.type || Command.defaults?.type;
this.arguments = options.arguments.map((argument) => {
this.arguments = options.arguments?.map((argument) => {
if (argument instanceof Argument) return argument;
else return new Argument(argument);
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/structures/arguments/Boolean.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Boolean {
export class BooleanType {
validate() {

}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/structures/arguments/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Channel {
export class ChannelType {
validate() {

}
Expand Down
22 changes: 19 additions & 3 deletions src/lib/structures/arguments/Integer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
export class Integer {
validate() {

import { Argument, ArgumentType } from '../Argument';

export class IntegerType {
value;

validate(content: string) {
if (Number.isInteger(Number(content))) {
this.value = content;
return true
}
else return false;
}

resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
value: this.value
}
}
}
2 changes: 1 addition & 1 deletion src/lib/structures/arguments/Mentionable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Mentionable {
export class MentionableType {
validate() {

}
Expand Down
22 changes: 19 additions & 3 deletions src/lib/structures/arguments/Number.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
export class Number {
validate() {

import { Argument, ArgumentType } from '../Argument';

export class NumberType {
value;

validate(content: string) {
if (!isNaN(Number(content))) {
this.value = content;
return true
}
else return false;
}

resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
value: this.value
}
}
}
2 changes: 1 addition & 1 deletion src/lib/structures/arguments/Role.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class Role {
export class RoleType {
validate() {

}
Expand Down
22 changes: 19 additions & 3 deletions src/lib/structures/arguments/String.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
export class String {
validate() {

import { Argument, ArgumentType } from '../Argument';

export class StringType {
value;

validate(content: string) {
if (typeof content === 'string') {
this.value = content;
return true
}
else return false;
}

resolve(argument: Argument) {
return {
...argument.toJSON(),
type: ArgumentType[argument.type],
value: this.value
}
}
}
26 changes: 17 additions & 9 deletions src/lib/structures/arguments/User.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import type { Message } from 'discord.js';
import type { Client, Guild } from 'discord.js';
import { Argument, ArgumentType } from '../Argument';

export class User {
validate(message: Message) {
const content = message.content;
export class UserType {
matches;

validate(content: string) {
const matches = content.match(/([0-9]+)/);

if (!matches) return {
success: false
}
if (!matches) return false;

this.matches = matches;

return true;
}

resolve(argument: Argument, client: Client, guild: Guild) {
return {
success: true,
value: message.client.users.cache.get(matches[0])
...argument.toJSON(),
type: ArgumentType[argument.type],
user: client.users.cache.get(this.matches[1]),
member: guild.members.cache.get(this.matches[1])
}
}
}

0 comments on commit 269c69a

Please sign in to comment.