argument system + permissions

This commit is contained in:
mmacierzanka committed 2021-06-20 16:47:50 +02:00
1 parent 261634e213
commit 876a274372
4 files changed
+48 -53

No files matched your search

+14 -9
View File
@@ -1,8 +1,7 @@
//IMPORTS //IMPORTS
import { Client, ClientOptions, Message } from 'discord.js'; import { Client, ClientOptions, Message } from 'discord.js';
import { Command, CommandsManager } from './Commands.js'; import { Command, CommandsManager, CommandMessageStructure } from './Commands.js';
import { HelpMessage, HelpMessageParams } from './Help.js'; import { HelpMessage, HelpMessageParams } from './Help.js';
import { CommandMessage } from './CommandMessage.js';
import * as http from 'http'; import * as http from 'http';
//TYPE DEFINITIONS //TYPE DEFINITIONS
@@ -31,7 +30,7 @@ class Bot {
} }
on: { on: {
message: (m: Message) => any, message: (m: Message) => any,
command: (m: CommandMessage) => any command: (m: Message, cmdInfo?: CommandMessageStructure) => any
} }
constructor(options: ConstructorOptions) { constructor(options: ConstructorOptions) {
@@ -54,7 +53,7 @@ class Bot {
} }
this.on = { this.on = {
message: (m: Message) => {}, message: (m: Message) => {},
command: (m: CommandMessage) => { console.log(m.command); } command: (m: Message, cmdInfo?: CommandMessageStructure) => { console.log(cmdInfo); }
} }
} }
async start(port?: number, token?: string) : Promise<boolean> { async start(port?: number, token?: string) : Promise<boolean> {
@@ -76,12 +75,18 @@ class Bot {
console.log('BOT IS READY!\n'); console.log('BOT IS READY!\n');
this.client.on('message', (m) => { this.client.on('message', (m) => {
this.on.message(m); this.on.message(m);
const cmdMsg: CommandMessage = new CommandMessage(m, this.config.prefix); const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix);
if(cmdMsg.command) { if(cmdMsg) {
this.on.command(cmdMsg); this.on.command(m, cmdMsg);
const calledCommand = this.commands.get(cmdMsg.command.name); const calledCommand: Command | null = this.commands.get(cmdMsg.name);
if(calledCommand) { if(calledCommand) {
calledCommand.start(cmdMsg); try {
calledCommand.start(m, cmdMsg.arguments);
}
catch (e) {
console.error(`ERROR! ${e.toString()}`);
return;
}
} }
} }
}); });
-32
View File
@@ -1,32 +0,0 @@
import { Message } from 'discord.js';
interface CommandMessageStructure {
name: string,
arguments: string[]
}
class CommandMessage extends Message {
command: CommandMessageStructure | null
constructor(message: Message, prefix: string) {
super(message.client, message, message.channel);
if(!message.author.bot && message.content.startsWith(prefix)) {
let msgContent = message.content.replace(prefix, '');
const cmdName = msgContent.split(' ')[0];
msgContent = msgContent.replace(cmdName, '');
let cmdArguments: string[] = msgContent.split(',');
cmdArguments = cmdArguments.map((a) => {
return a.replace(' ', '');
});
this.command = {
name: cmdName,
arguments: cmdArguments
}
}
else {
this.command = null;
}
}
}
export { CommandMessage }
+33 -11
View File
@@ -10,7 +10,11 @@ interface CommandBuilder {
usage?: string, usage?: string,
permissions?: PermissionResolvable, permissions?: PermissionResolvable,
visible?: boolean visible?: boolean
function: (message?: Message) => void | string | MessageEmbed; function: (message?: Message, cmdArguments?: string[]) => void | string | MessageEmbed;
}
interface CommandMessageStructure {
name: string,
arguments: string[]
} }
//ERROR CLASSES //ERROR CLASSES
@@ -35,28 +39,28 @@ class Command {
usage: string; usage: string;
permissions: Permissions; permissions: Permissions;
visible: boolean; visible: boolean;
private function: (message?: Message) => void | string | MessageEmbed; private function: (message?: Message, cmdArguments?: string[]) => void | string | MessageEmbed;
constructor(options: CommandBuilder) { constructor(options: CommandBuilder) {
this.name = options.name.split(' ').join('_'); this.name = options.name.split(' ').join('_');
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.permissions = new Permissions(options.permissions || 0); this.permissions = new Permissions(options.permissions || 0);
this.visible = options.visible || true; this.visible = options.visible || true;
this.function = options.function; this.function = options.function;
} }
async start(message?: Message) { async start(message?: Message, cmdArguments?: string[]) {
const memberPermissions : Readonly<Permissions> = message?.member?.permissions || new Permissions(0); const memberPermissions : Readonly<Permissions> = message?.member?.permissions || new Permissions(0);
if(this.permissions.any(memberPermissions, true)) { if(memberPermissions.any(this.permissions, true)) {
await this.function(message); await this.function(message, cmdArguments);
} }
else { else {
throw new PermissionsError(this, message?.member); throw new PermissionsError(this, message?.member);
} }
} }
static ProcessPhrase(phrase?: string | string[]) : string[] { static processPhrase(phrase?: string | string[]) : string[] {
if(Array.isArray(phrase)) { if(Array.isArray(phrase)) {
const buff = phrase.map((p) => { const buff = phrase.map((p) => {
return p.split(' ').join('_'); return p.split(' ').join('_');
@@ -79,8 +83,8 @@ class CommandsManager {
constructor() { constructor() {
this.list = []; this.list = [];
} }
get(name: string) : Command | undefined { get(name: string) : Command | null {
let command : Command | undefined = undefined; let command : Command | null = null;
this.list.map((c) => { this.list.map((c) => {
if(c.name == name) { if(c.name == name) {
command = c; command = c;
@@ -117,7 +121,25 @@ class CommandsManager {
return false; return false;
} }
} }
fetch(message: Message, prefix: string) : CommandMessageStructure | null {
if(!message.author.bot && message.content.startsWith(prefix)) {
let msgContent = message.content.replace(prefix, '');
const cmdName = msgContent.split(' ')[0];
msgContent = msgContent.replace(cmdName, '');
let cmdArguments: string[] = msgContent.split(',');
cmdArguments = cmdArguments.map((a) => {
return a.replace(' ', '');
});
return {
name: cmdName,
arguments: cmdArguments
}
}
else {
return null;
}
}
} }
//EXPORTS //EXPORTS
export { Command, CommandsManager } export { Command, CommandsManager, CommandMessageStructure }
+1 -1
View File
@@ -16,7 +16,7 @@ class HelpMessage extends Command {
usage: params.usage, usage: params.usage,
permissions: 0, permissions: 0,
description: params.description, description: params.description,
function: (m?: Message) => { function: () => {
const helpMsg = new MessageEmbed() const helpMsg = new MessageEmbed()
if(helpMsg != null) { if(helpMsg != null) {
helpMsg.setColor(params.color); helpMsg.setColor(params.color);