Arguments template - work in progress

This commit is contained in:
mmacierzanka committed 2021-08-02 19:14:24 +02:00
1 parent 1b7400ae0a
commit 146d886d04
2 files changed
+69 -6

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
export type ArgumentType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT";
export type ArgumentResolvable = string | boolean | number;
export interface Argument {
name: string;
isOptional?: boolean;
type?: ArgumentType;
}
export const ProcessArgument = (
a: string,
type: ArgumentType
): ArgumentResolvable => {
switch (type) {
case "STRING":
return a;
case "INTEGER":
const numI = parseInt(a);
if (isNaN(numI)) {
throw new TypeError(
`Argument ${a} cannot be converted to ${type}`
);
} else {
return numI;
}
case "FLOAT":
const numF = parseFloat(a);
if (isNaN(numF)) {
throw new TypeError(
`Argument ${a} cannot be converted to ${type}`
);
} else {
return numF;
}
case "BOOLEAN":
if (a.toLowerCase() == "true") {
return true;
} else if (a.toLowerCase() == "false") {
return false;
} else {
throw new TypeError(
`Argument ${a} cannot be converted to ${type}`
);
}
default:
throw new TypeError("Invalied type specified");
}
};
+23 -6
View File
@@ -6,17 +6,19 @@ import {
GuildMember, GuildMember,
MessageEmbed, MessageEmbed,
} from "discord.js"; } from "discord.js";
import { Argument, ArgumentResolvable } 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; name: string;
arguments: string[]; arguments: ArgumentResolvable[];
command: Command | null; command: Command | null;
} }
interface CommandBuilder { interface CommandBuilder {
name: string; name: string;
arguments?: Argument[];
aliases?: string[] | string; aliases?: string[] | string;
keywords?: string[] | string; keywords?: string[] | string;
description?: string; description?: string;
@@ -26,7 +28,7 @@ interface CommandBuilder {
visible?: boolean; visible?: boolean;
function: ( function: (
message?: Message, message?: Message,
cmdArguments?: string[] cmdArguments?: ArgumentResolvable[]
) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>; ) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>;
} }
interface PhraseOccurrenceData { interface PhraseOccurrenceData {
@@ -48,6 +50,7 @@ export class PermissionsError {
} }
export class Command { export class Command {
name: string; name: string;
arguments: Argument[];
aliases: string[]; aliases: string[];
keywords: string[]; keywords: string[];
description: string; description: string;
@@ -57,7 +60,7 @@ export class Command {
visible: boolean; visible: boolean;
private function: ( private function: (
message?: Message, message?: Message,
cmdArguments?: string[] cmdArguments?: ArgumentResolvable[]
) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>; ) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>;
/** /**
@@ -76,10 +79,11 @@ export class Command {
*/ */
constructor(options: CommandBuilder) { constructor(options: CommandBuilder) {
this.name = options.name.split(" ").join("_"); this.name = options.name.split(" ").join("_");
this.arguments = options.arguments || [];
this.aliases = Command.processPhrase(options.aliases); this.aliases = Command.processPhrase(options.aliases);
this.keywords = Command.processPhrase(options.keywords); this.keywords = Command.processPhrase(options.keywords);
this.description = options.description || "No description"; this.description = options.description || "No description";
this.usage = options.usage || ""; this.usage = options.usage || this.generateUsageFromArguments();
this.permissionCheck = this.permissionCheck =
options.permissionCheck == "ALL" || options.permissionCheck == "ANY" options.permissionCheck == "ALL" || options.permissionCheck == "ANY"
? options.permissionCheck ? options.permissionCheck
@@ -95,7 +99,10 @@ 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(message?: Message, cmdArguments?: string[]): Promise<void> { async start(
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 (
@@ -133,6 +140,16 @@ export class Command {
return []; return [];
} }
} }
private generateUsageFromArguments(): string {
let usageTemplate: string = "";
this.arguments.map((e) => {
usageTemplate += `[${e.name} (${e.type}${
e.isOptional ? ", optional" : ""
})] `;
});
return usageTemplate;
}
} }
export class CommandManager { export class CommandManager {
list: Command[]; list: Command[];
@@ -233,7 +250,7 @@ export class CommandManager {
this.list.push(command); this.list.push(command);
return true; return true;
} catch (e) { } catch (e) {
console.error(`ERROR! ${e.toString()}`); console.error(`[❌ ERROR] ${e.toString()}`);
return false; return false;
} }
} }