Archived
reworked fetching procedure
This commit is contained in:
1 parent
8ff79484c5
commit
8bd3dbd8d2
3 files changed
+27
-47
No files matched your search
+2
-2
@@ -2,8 +2,8 @@ export type ArgumentType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT";
|
|||||||
export type ArgumentResolvable = string | boolean | number;
|
export type ArgumentResolvable = string | boolean | number;
|
||||||
export interface Argument {
|
export interface Argument {
|
||||||
name: string;
|
name: string;
|
||||||
isOptional?: boolean;
|
optional: boolean;
|
||||||
type?: ArgumentType;
|
type: ArgumentType;
|
||||||
}
|
}
|
||||||
export const ProcessArgument = (
|
export const ProcessArgument = (
|
||||||
a: string,
|
a: string,
|
||||||
|
|||||||
+4
-4
@@ -112,9 +112,8 @@ export class Bot extends EventEmitter {
|
|||||||
this.emit("READY");
|
this.emit("READY");
|
||||||
});
|
});
|
||||||
this.client.on("message", async (m) => {
|
this.client.on("message", async (m) => {
|
||||||
const cmdMsg: CommandMessageStructure | null =
|
const cmdMsg = this.commands.fetch(m);
|
||||||
this.commands.fetch(m);
|
if (cmdMsg) {
|
||||||
if (cmdMsg?.command) {
|
|
||||||
this.emit("COMMAND", m, cmdMsg);
|
this.emit("COMMAND", m, cmdMsg);
|
||||||
try {
|
try {
|
||||||
await cmdMsg.command.start(m, cmdMsg.arguments);
|
await cmdMsg.command.start(m, cmdMsg.arguments);
|
||||||
@@ -143,7 +142,8 @@ export class Bot extends EventEmitter {
|
|||||||
this.emit("ERROR", e);
|
this.emit("ERROR", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (cmdMsg) {
|
} else if (m.content.startsWith(this.commands.prefix)) {
|
||||||
|
this.emit("MESSAGE", m);
|
||||||
this.messages.system.send(
|
this.messages.system.send(
|
||||||
"NOT_FOUND",
|
"NOT_FOUND",
|
||||||
{ phrase: m.content, user: m.member || undefined },
|
{ phrase: m.content, user: m.member || undefined },
|
||||||
|
|||||||
+21
-41
@@ -6,15 +6,14 @@ import {
|
|||||||
GuildMember,
|
GuildMember,
|
||||||
MessageEmbed,
|
MessageEmbed,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { Argument, ArgumentResolvable } from "./Arguments.js";
|
import { Argument, ArgumentResolvable, ProcessArgument } from "./Arguments.js";
|
||||||
|
|
||||||
//TYPE DEFINITIONS
|
//TYPE DEFINITIONS
|
||||||
type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
|
type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
|
||||||
type PermissionCheckTypes = "ALL" | "ANY";
|
type PermissionCheckTypes = "ALL" | "ANY";
|
||||||
export interface CommandMessageStructure {
|
export interface CommandMessageStructure {
|
||||||
name: string;
|
command: Command;
|
||||||
arguments: ArgumentResolvable[];
|
arguments: ArgumentResolvable[];
|
||||||
command: Command | null;
|
|
||||||
}
|
}
|
||||||
interface CommandBuilder {
|
interface CommandBuilder {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -145,7 +144,7 @@ export class Command {
|
|||||||
let usageTemplate: string = "";
|
let usageTemplate: string = "";
|
||||||
this.arguments.map((e) => {
|
this.arguments.map((e) => {
|
||||||
usageTemplate += `[${e.name} (${e.type}${
|
usageTemplate += `[${e.name} (${e.type}${
|
||||||
e.isOptional ? ", optional" : ""
|
e.optional ? ", optional" : ""
|
||||||
})] `;
|
})] `;
|
||||||
});
|
});
|
||||||
return usageTemplate;
|
return usageTemplate;
|
||||||
@@ -261,47 +260,28 @@ export class CommandManager {
|
|||||||
* @returns *CommandMessagesStructure* | *null*
|
* @returns *CommandMessagesStructure* | *null*
|
||||||
*/
|
*/
|
||||||
fetch(message: Message): CommandMessageStructure | null {
|
fetch(message: Message): CommandMessageStructure | null {
|
||||||
if (!message.author.bot && message.content.startsWith(this.prefix)) {
|
if (!message.author.bot) {
|
||||||
let msgContent = message.content.replace(this.prefix, "");
|
const content = message.content.startsWith(this.prefix)
|
||||||
const cmdName = msgContent.split(" ")[0];
|
? message.content.replace(this.prefix, "")
|
||||||
const cmd: Command | null = this.get(cmdName, "PREFIX");
|
: message.content;
|
||||||
msgContent = msgContent.replace(cmdName, "");
|
const name = content.split(" ")[0];
|
||||||
let cmdArguments: string[] = msgContent.split(
|
const command = this.get(name, "ALL");
|
||||||
this.argumentSeparator
|
if (command) {
|
||||||
);
|
const argumentsText = content.replace(name, "");
|
||||||
cmdArguments = cmdArguments.map((a) => {
|
const argumentsList = argumentsText
|
||||||
return a.replace(" ", "");
|
.split(this.argumentSeparator)
|
||||||
});
|
.map((a) => {
|
||||||
if (
|
return a.replace(" ", "");
|
||||||
(cmdArguments[0] == "" || cmdArguments[0] == " ") &&
|
});
|
||||||
cmdArguments.length == 1
|
|
||||||
) {
|
|
||||||
cmdArguments = [];
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
name: cmdName,
|
|
||||||
arguments: cmdArguments,
|
|
||||||
command: cmd,
|
|
||||||
};
|
|
||||||
} else if (!message.author.bot) {
|
|
||||||
const cmdName = message.content.split(" ")[0];
|
|
||||||
const cmd: Command | null = this.get(cmdName, "NO_PREFIX");
|
|
||||||
if (cmd) {
|
|
||||||
const cmdArgumentsStr = message.content.replace(cmdName, "");
|
|
||||||
let cmdArguments: string[] = cmdArgumentsStr.split(",");
|
|
||||||
cmdArguments = cmdArguments.map((a) => {
|
|
||||||
return a.replace(" ", "");
|
|
||||||
});
|
|
||||||
if (
|
if (
|
||||||
(cmdArguments[0] == "" || cmdArguments[0] == " ") &&
|
(argumentsList[0] == "" || argumentsList[0] == " ") &&
|
||||||
cmdArguments.length == 1
|
argumentsList.length == 1
|
||||||
) {
|
) {
|
||||||
cmdArguments = [];
|
argumentsList.splice(0, 1);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
name: cmdName,
|
command: command,
|
||||||
arguments: cmdArguments,
|
arguments: argumentsList,
|
||||||
command: cmd,
|
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in new issue
Block a user