Archived
argument system + permissions
This commit is contained in:
1 parent
261634e213
commit
876a274372
4 files changed
+48
-53
No files matched your search
+14
-9
@@ -1,8 +1,7 @@
|
||||
//IMPORTS
|
||||
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 { CommandMessage } from './CommandMessage.js';
|
||||
import * as http from 'http';
|
||||
|
||||
//TYPE DEFINITIONS
|
||||
@@ -31,7 +30,7 @@ class Bot {
|
||||
}
|
||||
on: {
|
||||
message: (m: Message) => any,
|
||||
command: (m: CommandMessage) => any
|
||||
command: (m: Message, cmdInfo?: CommandMessageStructure) => any
|
||||
}
|
||||
|
||||
constructor(options: ConstructorOptions) {
|
||||
@@ -54,7 +53,7 @@ class Bot {
|
||||
}
|
||||
this.on = {
|
||||
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> {
|
||||
@@ -76,12 +75,18 @@ class Bot {
|
||||
console.log('BOT IS READY!\n');
|
||||
this.client.on('message', (m) => {
|
||||
this.on.message(m);
|
||||
const cmdMsg: CommandMessage = new CommandMessage(m, this.config.prefix);
|
||||
if(cmdMsg.command) {
|
||||
this.on.command(cmdMsg);
|
||||
const calledCommand = this.commands.get(cmdMsg.command.name);
|
||||
const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix);
|
||||
if(cmdMsg) {
|
||||
this.on.command(m, cmdMsg);
|
||||
const calledCommand: Command | null = this.commands.get(cmdMsg.name);
|
||||
if(calledCommand) {
|
||||
calledCommand.start(cmdMsg);
|
||||
try {
|
||||
calledCommand.start(m, cmdMsg.arguments);
|
||||
}
|
||||
catch (e) {
|
||||
console.error(`ERROR! ${e.toString()}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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
@@ -10,7 +10,11 @@ interface CommandBuilder {
|
||||
usage?: string,
|
||||
permissions?: PermissionResolvable,
|
||||
visible?: boolean
|
||||
function: (message?: Message) => void | string | MessageEmbed;
|
||||
function: (message?: Message, cmdArguments?: string[]) => void | string | MessageEmbed;
|
||||
}
|
||||
interface CommandMessageStructure {
|
||||
name: string,
|
||||
arguments: string[]
|
||||
}
|
||||
|
||||
//ERROR CLASSES
|
||||
@@ -35,28 +39,28 @@ class Command {
|
||||
usage: string;
|
||||
permissions: Permissions;
|
||||
visible: boolean;
|
||||
private function: (message?: Message) => void | string | MessageEmbed;
|
||||
private function: (message?: Message, cmdArguments?: string[]) => void | string | MessageEmbed;
|
||||
|
||||
constructor(options: CommandBuilder) {
|
||||
this.name = options.name.split(' ').join('_');
|
||||
this.aliases = Command.ProcessPhrase(options.aliases);
|
||||
this.keywords = Command.ProcessPhrase(options.keywords);
|
||||
this.aliases = Command.processPhrase(options.aliases);
|
||||
this.keywords = Command.processPhrase(options.keywords);
|
||||
this.description = options.description || "No description";
|
||||
this.usage = options.usage || "";
|
||||
this.permissions = new Permissions(options.permissions || 0);
|
||||
this.visible = options.visible || true;
|
||||
this.function = options.function;
|
||||
}
|
||||
async start(message?: Message) {
|
||||
async start(message?: Message, cmdArguments?: string[]) {
|
||||
const memberPermissions : Readonly<Permissions> = message?.member?.permissions || new Permissions(0);
|
||||
if(this.permissions.any(memberPermissions, true)) {
|
||||
await this.function(message);
|
||||
if(memberPermissions.any(this.permissions, true)) {
|
||||
await this.function(message, cmdArguments);
|
||||
}
|
||||
else {
|
||||
throw new PermissionsError(this, message?.member);
|
||||
}
|
||||
}
|
||||
static ProcessPhrase(phrase?: string | string[]) : string[] {
|
||||
static processPhrase(phrase?: string | string[]) : string[] {
|
||||
if(Array.isArray(phrase)) {
|
||||
const buff = phrase.map((p) => {
|
||||
return p.split(' ').join('_');
|
||||
@@ -79,8 +83,8 @@ class CommandsManager {
|
||||
constructor() {
|
||||
this.list = [];
|
||||
}
|
||||
get(name: string) : Command | undefined {
|
||||
let command : Command | undefined = undefined;
|
||||
get(name: string) : Command | null {
|
||||
let command : Command | null = null;
|
||||
this.list.map((c) => {
|
||||
if(c.name == name) {
|
||||
command = c;
|
||||
@@ -117,7 +121,25 @@ class CommandsManager {
|
||||
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
|
||||
export { Command, CommandsManager }
|
||||
export { Command, CommandsManager, CommandMessageStructure }
|
||||
+1
-1
@@ -16,7 +16,7 @@ class HelpMessage extends Command {
|
||||
usage: params.usage,
|
||||
permissions: 0,
|
||||
description: params.description,
|
||||
function: (m?: Message) => {
|
||||
function: () => {
|
||||
const helpMsg = new MessageEmbed()
|
||||
if(helpMsg != null) {
|
||||
helpMsg.setColor(params.color);
|
||||
|
||||
Reference in new issue
Block a user