Modularized command resolution

This commit is contained in:
WatDuhHekBro 2020-07-25 21:35:53 -05:00
parent 4b03912e89
commit 2488239598
3 changed files with 88 additions and 85 deletions

View File

@ -40,43 +40,34 @@ export default new Command({
let command = commands.get(header); let command = commands.get(header);
if(!command || header === "test") if(!command || header === "test")
$.channel.send(`No command found by the name \`${header}\`!`); return $.channel.send(`No command found by the name \`${header}\`!`);
else
{
let usage = command.usage; let usage = command.usage;
let invalid = false;
for(const param of $.args) for(const param of $.args)
{ {
header += ` ${param}`; const type = command.resolve(param);
if(/<\w+>/g.test(param)) switch(type)
{ {
const type = param.match(/\w+/g)[0]; case Command.TYPES.SUBCOMMAND: header += ` ${param}`; break;
command = command[type]; case Command.TYPES.USER: header += " <user>" ; break;
case Command.TYPES.NUMBER: header += " <number>" ; break;
case Command.TYPES.ANY: header += " <any>" ; break;
default: header += ` ${param}`; break;
}
if(types.includes(type) && command?.usage) if(type === Command.TYPES.NONE)
usage = command.usage;
else
{ {
command = undefined; invalid = true;
break; break;
} }
}
else if(command?.subcommands?.[param])
{
command = command.subcommands[param];
if(command.usage !== "") command = command.get(param);
usage = command.usage;
}
else
{
command = undefined;
break;
}
} }
if(!command) if(invalid)
return $.channel.send(`No command found by the name \`${header}\`!`); return $.channel.send(`No command found by the name \`${header}\`!`);
let append = ""; let append = "";
@ -102,7 +93,6 @@ export default new Command({
} }
} }
append = "Usages:" + (list.length > 0 ? `\n${list.join('\n')}` : " None."); append = "Usages:" + (list.length > 0 ? `\n${list.join('\n')}` : " None.");
} }
else else
@ -110,6 +100,5 @@ export default new Command({
$.channel.send(`Command: \`${header}\`\nDescription: ${command.description}\n${append}`, {split: true}); $.channel.send(`Command: \`${header}\`\nDescription: ${command.description}\n${append}`, {split: true});
} }
}
}) })
}); });

View File

@ -21,6 +21,8 @@ interface CommandOptions
any?: Command; any?: Command;
} }
export enum TYPES {SUBCOMMAND, USER, NUMBER, ANY, NONE};
export default class Command export default class Command
{ {
public readonly description: string; public readonly description: string;
@ -34,6 +36,7 @@ export default class Command
public any: Command|null; public any: Command|null;
//public static readonly PERMISSIONS = PERMISSIONS; //public static readonly PERMISSIONS = PERMISSIONS;
[key: string]: any; // Allow for dynamic indexing. The CommandOptions interface will still prevent users from adding unused properties though. [key: string]: any; // Allow for dynamic indexing. The CommandOptions interface will still prevent users from adding unused properties though.
public static readonly TYPES = TYPES;
constructor(options?: CommandOptions) constructor(options?: CommandOptions)
{ {
@ -78,17 +81,38 @@ export default class Command
this.subcommands[key] = command; this.subcommands[key] = command;
} }
/** See if a subcommand exists for the command. */ public resolve(param: string): TYPES
/*public has(type: string): boolean
{ {
return this.subcommands && (type in this.subcommands) || false; if(this.subcommands?.[param])
}*/ return TYPES.SUBCOMMAND;
// Any Discord ID format will automatically format to a user ID.
else if(this.user && (/\d{17,19}/.test(param)))
return TYPES.USER;
// Disallow infinity and allow for 0.
else if(this.number && (Number(param) || param === "0") && !param.includes("Infinity"))
return TYPES.NUMBER;
else if(this.any)
return TYPES.ANY;
else
return TYPES.NONE;
}
/** Get the requested subcommand if it exists. */ public get(param: string): Command
/*public get(type: string): Command|null
{ {
return this.subcommands && this.subcommands[type] || null; const type = this.resolve(param);
}*/ let command;
switch(type)
{
case TYPES.SUBCOMMAND: command = this.subcommands![param]; break;
case TYPES.USER: command = this.user as Command; break;
case TYPES.NUMBER: command = this.number as Command; break;
case TYPES.ANY: command = this.any as Command; break;
default: command = this; break;
}
return command;
}
} }
/*export function hasPermission(member: GuildMember, permission: number): boolean /*export function hasPermission(member: GuildMember, permission: number): boolean

View File

@ -58,28 +58,18 @@ export default new Event({
break; break;
} }
if(command.subcommands?.[param]) const type = command.resolve(param);
command = command.subcommands[param]; command = command.get(param);
// Any Discord ID format will automatically format to a user ID.
else if(command.user && (/\d{17,19}/.test(param))) if(type === Command.TYPES.USER)
{ {
const id = param.match(/\d+/g)![0]; const id = param.match(/\d+/g)![0];
command = command.user;
try {params.push(await message.client.users.fetch(id))} try {params.push(await message.client.users.fetch(id))}
catch(error) {return message.channel.send(`No user found by the ID \`${id}\`!`)} catch(error) {return message.channel.send(`No user found by the ID \`${id}\`!`)}
} }
// Disallow infinity and allow for 0. else if(type === Command.TYPES.NUMBER)
else if(command.number && (Number(param) || param === "0") && !param.includes("Infinity"))
{
command = command.number;
params.push(Number(param)); params.push(Number(param));
} else if(type !== Command.TYPES.SUBCOMMAND)
else if(command.any)
{
command = command.any;
params.push(param);
}
else
params.push(param); params.push(param);
} }