Skip to content

Commit 269c69a

Browse files
committed
feat: types
1 parent 91ae0ef commit 269c69a

File tree

10 files changed

+104
-28
lines changed

10 files changed

+104
-28
lines changed

src/handlers/MessageCommandHandler.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
import { Collection, CommandInteractionOptionResolver, Message } from 'discord.js';
1+
import { Client, Collection, CommandInteractionOptionResolver, Guild, Message, TextChannel, User } 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';
65
import { Commands } from '../lib/managers/CommandManager';
76
import { Handlers } from '../lib/managers/HandlerManager';
87
import Logger from 'js-logger';
8+
import { UserType } from '../lib/structures/arguments/User';
9+
import type { Argument } from '../lib/structures/Argument';
910

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

13+
const checkValidation = async(arg: UserType, content: string, client: Client, guild: Guild, argument: Argument, channel: TextChannel, user: User) => {
14+
if (!content) {
15+
const message = await channel.awaitMessages({ filter: (m) => m.author.id === user.id && m.channelId === channel.id, time: 60000, max: 1 });
16+
17+
content = [...message.values()]?.[0]?.content;
18+
}
19+
20+
const validate = arg.validate(content);
21+
if (!validate) return checkValidation(arg, null, client, guild, argument, channel, user);
22+
23+
return arg.resolve(argument, client, guild);
24+
}
25+
1226
export async function MessageCommandHandler(
1327
message: Message,
1428
commandName: string,
@@ -35,7 +49,7 @@ export async function MessageCommandHandler(
3549
});
3650
}
3751

38-
args = args.map(
52+
/*args = args.map(
3953
(arg, i) =>
4054
new Object({
4155
name: command.arguments[i].name,
@@ -54,7 +68,13 @@ export async function MessageCommandHandler(
5468
if (args[0]?.type === ArgumentType.SUB_COMMAND_GROUP && args[0]?.options[0]?.type === ArgumentType.SUB_COMMAND)
5569
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5670
// @ts-ignore
57-
args[0].options[0].options = args[0].options.splice(1);
71+
args[0].options[0].options = args[0].options.splice(1);*/
72+
73+
for (const argument in command.arguments) {
74+
const arg = new UserType();
75+
76+
args[argument] = await checkValidation(arg, args[argument] as string, client, message.guild, command.arguments[argument], message.channel as TextChannel, message.author);
77+
}
5878

5979
let replied: Message;
6080
const ctx = new CommandContext(client, {
@@ -106,4 +126,4 @@ export async function MessageCommandHandler(
106126
.then(() => {
107127
Logger.debug(`Successfully ran command (${command.name}) for ${message.author.username}`);
108128
});
109-
}
129+
}

src/lib/structures/Command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class Command {
9191
this.name = options.name || Command.defaults?.name;
9292
this.description = options.description || Command.defaults?.description;
9393
this.type = options.type || Command.defaults?.type;
94-
this.arguments = options.arguments.map((argument) => {
94+
this.arguments = options.arguments?.map((argument) => {
9595
if (argument instanceof Argument) return argument;
9696
else return new Argument(argument);
9797
});

src/lib/structures/arguments/Boolean.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Boolean {
1+
export class BooleanType {
22
validate() {
33

44
}

src/lib/structures/arguments/Channel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Channel {
1+
export class ChannelType {
22
validate() {
33

44
}
+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
export class Integer {
2-
validate() {
3-
1+
import { Argument, ArgumentType } from '../Argument';
2+
3+
export class IntegerType {
4+
value;
5+
6+
validate(content: string) {
7+
if (Number.isInteger(Number(content))) {
8+
this.value = content;
9+
return true
10+
}
11+
else return false;
12+
}
13+
14+
resolve(argument: Argument) {
15+
return {
16+
...argument.toJSON(),
17+
type: ArgumentType[argument.type],
18+
value: this.value
19+
}
420
}
521
}

src/lib/structures/arguments/Mentionable.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Mentionable {
1+
export class MentionableType {
22
validate() {
33

44
}
+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
export class Number {
2-
validate() {
3-
1+
import { Argument, ArgumentType } from '../Argument';
2+
3+
export class NumberType {
4+
value;
5+
6+
validate(content: string) {
7+
if (!isNaN(Number(content))) {
8+
this.value = content;
9+
return true
10+
}
11+
else return false;
12+
}
13+
14+
resolve(argument: Argument) {
15+
return {
16+
...argument.toJSON(),
17+
type: ArgumentType[argument.type],
18+
value: this.value
19+
}
420
}
521
}

src/lib/structures/arguments/Role.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class Role {
1+
export class RoleType {
22
validate() {
33

44
}
+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
export class String {
2-
validate() {
3-
1+
import { Argument, ArgumentType } from '../Argument';
2+
3+
export class StringType {
4+
value;
5+
6+
validate(content: string) {
7+
if (typeof content === 'string') {
8+
this.value = content;
9+
return true
10+
}
11+
else return false;
12+
}
13+
14+
resolve(argument: Argument) {
15+
return {
16+
...argument.toJSON(),
17+
type: ArgumentType[argument.type],
18+
value: this.value
19+
}
420
}
521
}

src/lib/structures/arguments/User.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import type { Message } from 'discord.js';
1+
import type { Client, Guild } from 'discord.js';
2+
import { Argument, ArgumentType } from '../Argument';
23

3-
export class User {
4-
validate(message: Message) {
5-
const content = message.content;
4+
export class UserType {
5+
matches;
6+
7+
validate(content: string) {
68
const matches = content.match(/([0-9]+)/);
79

8-
if (!matches) return {
9-
success: false
10-
}
10+
if (!matches) return false;
11+
12+
this.matches = matches;
13+
14+
return true;
15+
}
1116

17+
resolve(argument: Argument, client: Client, guild: Guild) {
1218
return {
13-
success: true,
14-
value: message.client.users.cache.get(matches[0])
19+
...argument.toJSON(),
20+
type: ArgumentType[argument.type],
21+
user: client.users.cache.get(this.matches[1]),
22+
member: guild.members.cache.get(this.matches[1])
1523
}
1624
}
1725
}

0 commit comments

Comments
 (0)