keyword + alias support

This commit is contained in:
mmacierzanka committed 2021-06-21 19:19:57 +02:00
1 parent aabfa27fdf
commit 5c899b1129
2 files changed
+67 -12

No files matched your search

+5 -8
View File
@@ -77,27 +77,24 @@ class Bot {
this.client.on('message', async m => { this.client.on('message', async m => {
this.on.message(m); this.on.message(m);
const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix); const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix);
if(cmdMsg) { if(cmdMsg?.command) {
this.on.command(m, cmdMsg); this.on.command(m, cmdMsg);
const calledCommand: Command | null = this.commands.get(cmdMsg.name);
if(calledCommand) {
try { try {
await calledCommand.start(m, cmdMsg.arguments); await cmdMsg.command.start(m, cmdMsg.arguments);
} }
catch (e) { catch (e) {
if(e instanceof PermissionsError) { if(e instanceof PermissionsError) {
this.messages.system.send('PERMISSION', { user: m.member || undefined, command: calledCommand }, m.channel); this.messages.system.send('PERMISSION', { user: m.member || undefined, command: cmdMsg.command }, m.channel);
} }
else { else {
this.messages.system.send('ERROR', { command: calledCommand, user: m.member || undefined, error: e }, m.channel); this.messages.system.send('ERROR', { command: cmdMsg.command, user: m.member || undefined, error: e }, m.channel);
} }
return; return;
} }
} }
else { else if(cmdMsg) {
this.messages.system.send('NOT_FOUND', { phrase: m.content, user: m.member || undefined }, m.channel); this.messages.system.send('NOT_FOUND', { phrase: m.content, user: m.member || undefined }, m.channel);
} }
}
}); });
} }
else { else {
+62 -4
View File
@@ -14,8 +14,10 @@ interface CommandBuilder {
} }
interface CommandMessageStructure { interface CommandMessageStructure {
name: string, name: string,
arguments: string[] arguments: string[],
command: Command | null
} }
type GetMode = 'ALL' | 'PREFIX' | 'NO_PREFIX';
//ERROR CLASSES //ERROR CLASSES
class PermissionsError { class PermissionsError {
@@ -89,12 +91,44 @@ class CommandsManager {
constructor() { constructor() {
this.list = []; this.list = [];
} }
get(name: string) : Command | null { get(phrase: string, mode?: GetMode) : Command | null {
if(!mode) mode = 'ALL';
let command : Command | null = null; let command : Command | null = null;
this.list.map((c) => { this.list.map((c) => {
if(c.name == name) { switch (mode) {
case 'PREFIX':
if(c.name == phrase) {
command = c; command = c;
} }
c.aliases.map(a => {
if(a == phrase) {
command = c;
}
});
break;
case 'NO_PREFIX':
c.keywords.map(k => {
if(k == phrase) {
command = c;
}
});
break;
case 'ALL':
if(c.name == phrase) {
command = c;
}
c.aliases.map(a => {
if(a == phrase) {
command = c;
}
});
c.keywords.map(k => {
if(k == phrase) {
command = c;
}
});
break;
}
}); });
return command; return command;
} }
@@ -131,6 +165,7 @@ class CommandsManager {
if(!message.author.bot && message.content.startsWith(prefix)) { if(!message.author.bot && message.content.startsWith(prefix)) {
let msgContent = message.content.replace(prefix, ''); let msgContent = message.content.replace(prefix, '');
const cmdName = msgContent.split(' ')[0]; const cmdName = msgContent.split(' ')[0];
const cmd: Command | null = this.get(cmdName, 'PREFIX');
msgContent = msgContent.replace(cmdName, ''); msgContent = msgContent.replace(cmdName, '');
let cmdArguments: string[] = msgContent.split(','); let cmdArguments: string[] = msgContent.split(',');
cmdArguments = cmdArguments.map((a) => { cmdArguments = cmdArguments.map((a) => {
@@ -141,7 +176,30 @@ class CommandsManager {
} }
return { return {
name: cmdName, name: cmdName,
arguments: cmdArguments arguments: cmdArguments,
command: cmd
}
}
else if(!message.author.bot) {
const cmdName = message.content.split(' ')[0];
const cmd: Command | null = this.get(cmdName, 'NO_PREFIX');
if(cmd) {
const cmdArgumentsStr = message.content.replace(cmdName, '');
let cmdArguments: string[] = cmdArgumentsStr.split(',');
cmdArguments = cmdArguments.map((a) => {
return a.replace(' ', '');
});
if((cmdArguments[0] == '' || cmdArguments[0] == ' ') && cmdArguments.length == 1) {
cmdArguments = [];
}
return {
name: cmdName,
arguments: cmdArguments,
command: cmd
}
}
else {
return null;
} }
} }
else { else {