94 lines
2.1 KiB
TypeScript
94 lines
2.1 KiB
TypeScript
import {Message} from 'discord.js';
|
|
import {CustomClient} from "./index";
|
|
|
|
export class CommandCollection {
|
|
private commands : { [commandName : string] : Command } = {};
|
|
|
|
get(commandName : string, aliased = true) : Command {
|
|
let command = this.commands[commandName];
|
|
|
|
if (!command && aliased)
|
|
command = Object.values(this.commands).find(
|
|
(cmd : Command) => cmd.checkName(commandName)
|
|
) as Command;
|
|
|
|
return command;
|
|
}
|
|
|
|
has(commandName : string, aliased = true) : boolean {
|
|
return !!this.get(commandName, aliased);
|
|
}
|
|
|
|
add(command : Command) {
|
|
if (this.commands[command.name])
|
|
return console.error(`Command ${command.name} is already in the collection`);
|
|
|
|
this.commands[command.name] = command;
|
|
}
|
|
|
|
clear(exceptions : Command[]) {
|
|
for (const commandName in this.commands)
|
|
if (!exceptions.find(cmd => cmd.checkName(commandName)))
|
|
delete this.commands[commandName];
|
|
}
|
|
|
|
getNames() {
|
|
return Object.keys(this.commands);
|
|
}
|
|
}
|
|
|
|
interface Permissions {
|
|
users: string[];
|
|
groups: string[];
|
|
}
|
|
|
|
export class Command {
|
|
name : string;
|
|
description? : string;
|
|
args? : string[];
|
|
guildOnly? : boolean;
|
|
cooldown? : number;
|
|
aliases? : string[];
|
|
usage? : string;
|
|
permissions? : Permissions;
|
|
|
|
constructor(
|
|
name : string,
|
|
public execute : (message? : Message, args? : string[], client? : CustomClient) => void,
|
|
{
|
|
description = null as string,
|
|
args = null as string[],
|
|
guildOnly = false,
|
|
cooldown = 0,
|
|
aliases = null as string[],
|
|
usage = null as string,
|
|
permissions = null as Permissions,
|
|
}
|
|
) {
|
|
this.name = name.toLowerCase();
|
|
this.description = description;
|
|
this.args = args;
|
|
this.guildOnly = guildOnly;
|
|
this.cooldown = cooldown;
|
|
this.aliases = aliases;
|
|
this.usage = usage;
|
|
this.permissions = permissions;
|
|
}
|
|
|
|
checkName(commandName : string) {
|
|
return commandName === this.name || (this.aliases && this.aliases.includes(commandName));
|
|
}
|
|
|
|
checkPermission(userId : string) : boolean {
|
|
if (!this.permissions) return true;
|
|
|
|
if (this.permissions.users.includes(userId))
|
|
return true;
|
|
|
|
for (const group of this.permissions.groups)
|
|
if (group.includes(userId))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
} |