moved processing to the starting method

This commit is contained in:
mmacierzanka committed 2021-08-03 01:01:33 +02:00
1 parent 89d8319ccd
commit 69ab1237c9
1 file changed
+16 -18
+16 -18
View File
@@ -13,7 +13,7 @@ type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
type PermissionCheckTypes = "ALL" | "ANY"; type PermissionCheckTypes = "ALL" | "ANY";
export interface CommandMessageStructure { export interface CommandMessageStructure {
command: Command; command: Command;
arguments: ArgumentResolvable[]; arguments: string[];
} }
interface CommandBuilder { interface CommandBuilder {
name: string; name: string;
@@ -87,10 +87,7 @@ export class Command {
* @param {string[]} [cmdArguments] - list of processed arguments passed in a Discord message * @param {string[]} [cmdArguments] - list of processed arguments passed in a Discord message
* @returns *Promise<void>* * @returns *Promise<void>*
*/ */
async start( async start(message?: Message, cmdArguments?: string[]): Promise<void> {
message?: Message,
cmdArguments?: ArgumentResolvable[]
): Promise<void> {
const memberPermissions: Readonly<Permissions> = const memberPermissions: Readonly<Permissions> =
message?.member?.permissions || new Permissions(0); message?.member?.permissions || new Permissions(0);
if ( if (
@@ -98,6 +95,19 @@ export class Command {
? memberPermissions.has(this.permissions, true) ? memberPermissions.has(this.permissions, true)
: memberPermissions.any(this.permissions, true) : memberPermissions.any(this.permissions, true)
) { ) {
let inputArguments: ArgumentResolvable[] = cmdArguments || [];
if (this.arguments.length > 0) {
this.arguments.map((a, i) => {
if (!inputArguments[i] && !a.optional) {
throw new MissingArgumentError(a);
} else {
inputArguments[i] = ProcessArgument(
inputArguments[i] as string,
a.type
);
}
});
}
const fnResult = await this.function(message, cmdArguments); const fnResult = await this.function(message, cmdArguments);
if (typeof fnResult == "string") { if (typeof fnResult == "string") {
await message?.reply(fnResult); await message?.reply(fnResult);
@@ -257,7 +267,7 @@ export class CommandManager {
const command = this.get(name, "ALL"); const command = this.get(name, "ALL");
if (command) { if (command) {
const argumentsText = content.replace(name, ""); const argumentsText = content.replace(name, "");
const argumentsList: ArgumentResolvable[] = argumentsText const argumentsList = argumentsText
.split(this.argumentSeparator) .split(this.argumentSeparator)
.map((a) => { .map((a) => {
return a.replace(" ", ""); return a.replace(" ", "");
@@ -268,18 +278,6 @@ export class CommandManager {
) { ) {
argumentsList.splice(0, 1); argumentsList.splice(0, 1);
} }
if (command.arguments.length > 0) {
command.arguments.map((a, i) => {
if (!argumentsList[i] && !a.optional) {
throw new MissingArgumentError(a);
} else {
argumentsList[i] = ProcessArgument(
argumentsList[i] as string,
a.type
);
}
});
}
return { return {
command: command, command: command,
arguments: argumentsList, arguments: argumentsList,