mirror of
https://github.com/keanuplayz/TravBot-v3.git
synced 2024-08-15 02:33:12 +00:00
Modularized command resolution
This commit is contained in:
parent
4b03912e89
commit
2488239598
3 changed files with 88 additions and 85 deletions
|
@ -40,76 +40,65 @@ 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 invalid = false;
|
||||||
|
|
||||||
|
for(const param of $.args)
|
||||||
{
|
{
|
||||||
let usage = command.usage;
|
const type = command.resolve(param);
|
||||||
|
|
||||||
for(const param of $.args)
|
switch(type)
|
||||||
{
|
{
|
||||||
header += ` ${param}`;
|
case Command.TYPES.SUBCOMMAND: header += ` ${param}`; break;
|
||||||
|
case Command.TYPES.USER: header += " <user>" ; break;
|
||||||
if(/<\w+>/g.test(param))
|
case Command.TYPES.NUMBER: header += " <number>" ; break;
|
||||||
{
|
case Command.TYPES.ANY: header += " <any>" ; break;
|
||||||
const type = param.match(/\w+/g)[0];
|
default: header += ` ${param}`; break;
|
||||||
command = command[type];
|
|
||||||
|
|
||||||
if(types.includes(type) && command?.usage)
|
|
||||||
usage = command.usage;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
command = undefined;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(command?.subcommands?.[param])
|
|
||||||
{
|
|
||||||
command = command.subcommands[param];
|
|
||||||
|
|
||||||
if(command.usage !== "")
|
|
||||||
usage = command.usage;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
command = undefined;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!command)
|
if(type === Command.TYPES.NONE)
|
||||||
return $.channel.send(`No command found by the name \`${header}\`!`);
|
|
||||||
|
|
||||||
let append = "";
|
|
||||||
|
|
||||||
if(usage === "")
|
|
||||||
{
|
{
|
||||||
const list: string[] = [];
|
invalid = true;
|
||||||
|
break;
|
||||||
for(const subtag in command.subcommands)
|
|
||||||
{
|
|
||||||
const subcmd = command.subcommands[subtag];
|
|
||||||
const customUsage = subcmd.usage ? ` ${subcmd.usage}` : "";
|
|
||||||
list.push(`- \`${header} ${subtag}${customUsage}\` - ${subcmd.description}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(const type of types)
|
|
||||||
{
|
|
||||||
if(command[type])
|
|
||||||
{
|
|
||||||
const cmd = command[type];
|
|
||||||
const customUsage = cmd.usage ? ` ${cmd.usage}` : "";
|
|
||||||
list.push(`- \`${header} <${type}>${customUsage}\` - ${cmd.description}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
append = "Usages:" + (list.length > 0 ? `\n${list.join('\n')}` : " None.");
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
append = `Usage: \`${header} ${usage}\``;
|
|
||||||
|
|
||||||
$.channel.send(`Command: \`${header}\`\nDescription: ${command.description}\n${append}`, {split: true});
|
command = command.get(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(invalid)
|
||||||
|
return $.channel.send(`No command found by the name \`${header}\`!`);
|
||||||
|
|
||||||
|
let append = "";
|
||||||
|
|
||||||
|
if(usage === "")
|
||||||
|
{
|
||||||
|
const list: string[] = [];
|
||||||
|
|
||||||
|
for(const subtag in command.subcommands)
|
||||||
|
{
|
||||||
|
const subcmd = command.subcommands[subtag];
|
||||||
|
const customUsage = subcmd.usage ? ` ${subcmd.usage}` : "";
|
||||||
|
list.push(`- \`${header} ${subtag}${customUsage}\` - ${subcmd.description}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(const type of types)
|
||||||
|
{
|
||||||
|
if(command[type])
|
||||||
|
{
|
||||||
|
const cmd = command[type];
|
||||||
|
const customUsage = cmd.usage ? ` ${cmd.usage}` : "";
|
||||||
|
list.push(`- \`${header} <${type}>${customUsage}\` - ${cmd.description}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
append = "Usages:" + (list.length > 0 ? `\n${list.join('\n')}` : " None.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
append = `Usage: \`${header} ${usage}\``;
|
||||||
|
|
||||||
|
$.channel.send(`Command: \`${header}\`\nDescription: ${command.description}\n${append}`, {split: true});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
|
@ -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
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue