forked from engineering/cloudservices
parent
34dc8553f1
commit
d1d324ceb0
|
@ -18,7 +18,7 @@ export default class Command {
|
||||||
|
|
||||||
guildOnly?: boolean
|
guildOnly?: boolean
|
||||||
|
|
||||||
subcommands: Map<string, Command>
|
subcommands: Command[]
|
||||||
|
|
||||||
public run(message: Message, args: string[]) {} // eslint-disable-line
|
public run(message: Message, args: string[]) {} // eslint-disable-line
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ export default class Command {
|
||||||
this.aliases = [];
|
this.aliases = [];
|
||||||
this.guildOnly = true;
|
this.guildOnly = true;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.subcommands = new Map();
|
this.subcommands = [];
|
||||||
this.permissions = {};
|
this.permissions = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,48 +36,16 @@ export default class Util {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public resolveCommand(command: string, args: string[]): { command: Command, args: string[]} {
|
public resolveCommand(command: string): Command {
|
||||||
let Cmd: Command;
|
if (this.client.commands.has(command)) return this.client.commands.get(command);
|
||||||
if (this.client.commands.has(command)) Cmd = this.client.commands.get(command);
|
|
||||||
else {
|
|
||||||
for (const cmd of this.client.commands.values()) {
|
for (const cmd of this.client.commands.values()) {
|
||||||
if (!cmd.aliases) continue;// eslint-disable-line no-continue
|
if (!cmd.aliases) continue;// eslint-disable-line no-continue
|
||||||
for (const alias of cmd.aliases) {
|
for (const alias of cmd.aliases) {
|
||||||
if (command.toLowerCase() === alias.toLowerCase()) {
|
if (command === alias.toLowerCase()) return cmd;
|
||||||
Cmd = cmd;
|
}
|
||||||
} else {
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!Cmd) return undefined;
|
|
||||||
let hasSubcmd = true;
|
|
||||||
while (hasSubcmd) {
|
|
||||||
if (!Cmd.subcommands.size) {
|
|
||||||
hasSubcmd = false; break;
|
|
||||||
}
|
|
||||||
if (Cmd.subcommands.has(args[0])) {
|
|
||||||
Cmd = Cmd.subcommands.get(args[0]);
|
|
||||||
args.shift();
|
|
||||||
} else {
|
|
||||||
for (const cmd of Cmd.subcommands.values()) {
|
|
||||||
if (!cmd.aliases) continue; // eslint-disable-line
|
|
||||||
for (const alias of cmd.aliases) {
|
|
||||||
if (args[0].toLowerCase() === alias.toLowerCase()) {
|
|
||||||
Cmd = cmd; args.shift(); break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return { command: Cmd, args };
|
|
||||||
}
|
|
||||||
|
|
||||||
public resolveSubcommand(parentCmd: Command, args?: string[]) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public async handleError(error: Error, message?: Message, command?: Command): Promise<void> {
|
public async handleError(error: Error, message?: Message, command?: Command): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -15,30 +15,30 @@ export default class {
|
||||||
if (message.content.indexOf(this.client.config.prefix) !== 0) return;
|
if (message.content.indexOf(this.client.config.prefix) !== 0) return;
|
||||||
const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g);
|
const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g);
|
||||||
const command: string = noPrefix[0].toLowerCase();
|
const command: string = noPrefix[0].toLowerCase();
|
||||||
const Args = noPrefix.slice(1);
|
const resolved: Command = this.client.util.resolveCommand(command);
|
||||||
const resolved: { command: Command, args: string[] } = this.client.util.resolveCommand(command, Args);
|
|
||||||
if (!resolved) return;
|
if (!resolved) return;
|
||||||
if (resolved.command.guildOnly && !(message.channel instanceof TextChannel)) return;
|
if (resolved.guildOnly && !(message.channel instanceof TextChannel)) return;
|
||||||
let hasUserPerms: boolean;
|
let hasUserPerms: boolean;
|
||||||
if (resolved.command.permissions.users) {
|
if (resolved.permissions.users) {
|
||||||
hasUserPerms = resolved.command.permissions.users.includes(message.author.id);
|
hasUserPerms = resolved.permissions.users.includes(message.author.id);
|
||||||
}
|
}
|
||||||
let hasRolePerms: boolean = false;
|
let hasRolePerms: boolean = false;
|
||||||
if (resolved.command.permissions.roles) {
|
if (resolved.permissions.roles) {
|
||||||
for (const role of resolved.command.permissions.roles) {
|
for (const role of resolved.permissions.roles) {
|
||||||
if (message.member && message.member.roles.includes(role)) {
|
if (message.member && message.member.roles.includes(role)) {
|
||||||
// this.client.signale.debug(message.member.roles.includes(role));
|
// this.client.signale.debug(message.member.roles.includes(role));
|
||||||
hasRolePerms = true; break;
|
hasRolePerms = true; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!resolved.command.permissions.users && !resolved.command.permissions.roles) {
|
if (!resolved.permissions.users && !resolved.permissions.roles) {
|
||||||
hasUserPerms = true;
|
hasUserPerms = true;
|
||||||
hasRolePerms = true;
|
hasRolePerms = true;
|
||||||
}
|
}
|
||||||
if (!hasRolePerms && !hasUserPerms) return;
|
if (!hasRolePerms && !hasUserPerms) return;
|
||||||
if (!resolved.command.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; }
|
if (!resolved.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; }
|
||||||
resolved.command.run(message, resolved.args);
|
const args: string[] = noPrefix.slice(1);
|
||||||
|
resolved.run(message, args);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.client.util.handleError(error, message);
|
this.client.util.handleError(error, message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue