led-bot/parser.js

118 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-11-08 03:29:13 +00:00
import Logger, { levels } from './logger.js';
import { filename } from './utils.js';
2020-11-07 23:12:27 +00:00
export class Command {
2020-11-08 03:29:13 +00:00
init(ctx, log) {
this.log = log;
}
log;
name = 'DEFAULT';
whitelist = false;
func() {}
2020-11-07 23:12:27 +00:00
}
export class CommandInitializer {
2020-11-08 03:29:13 +00:00
commands = [];
uninitialized = [];
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
initialize(ctx) {
for (let index in this.uninitialized) {
this.initCommand(this.uninitialized[index], ctx);
delete this.uninitialized[index];
}
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
initCommand(cmd, ctx) {
cmd.init(ctx, new Logger(`cmd.${cmd.name}`, ctx.log_level));
this.commands.push(cmd);
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
addCommand(cmd) {
this.uninitialized.push(cmd);
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
getCommands() {
return this.commands;
}
2020-11-07 23:12:27 +00:00
}
export default class CommandParser {
2020-11-08 03:29:13 +00:00
constructor(ctx, prefix = ';') {
this.log = new Logger(filename(import.meta.url), ctx.log_level);
this.prefix = prefix ? prefix : this.prefix;
}
log;
prefix;
commands = [];
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
addCommand(cmd) {
this.log.debug(`cmd to add: ${JSON.stringify(cmd)}`);
if (this.isCmd(cmd)) {
this.commands.push(cmd);
}
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
hasCmd(str) {
let results = this.commands.filter((c) => c.name == str);
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
return results.length ? results[0] : undefined;
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
isCmd(cmd) {
return typeof cmd == 'object' && cmd instanceof Command;
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
getArgsList(split) {
let parsed_args = [];
let join_index = -1;
let add = true;
for (let index in split) {
if (split[index].startsWith('```')) {
join_index = index;
add = false;
}
if (add) {
parsed_args.push(split[index]);
}
if (split[index].endsWith('```') && join_index != -1) {
let joined = split
.slice(join_index, index + 1)
.join(' ')
.replace(/```/g, '');
parsed_args.push(joined);
add = true;
join_index = -1;
}
}
return parsed_args;
}
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
parseMsg(msg, ctx) {
if (msg.author.bot) {
return;
}
this.log.debug(msg.content);
this.log.debug(msg.content.startsWith(this.prefix));
this.log.debug(msg.content[0]);
this.log.debug(this.prefix);
if (msg.content.startsWith(this.prefix)) {
let snip = msg.content.slice(this.prefix.length);
let unsep = snip.split(' ');
let res = this.hasCmd(unsep[0]);
let args = this.getArgsList(unsep.slice(1));
this.log.debug(snip);
this.log.debug(res);
this.log.debug(args);
2020-11-07 23:12:27 +00:00
2020-11-08 03:29:13 +00:00
if (res != undefined) {
this.log.debug(`execute function ${res.name}`);
if (res.whitelist && ctx.whitelist.indexOf(msg.author.id) == -1) {
msg.channel.createMessage('not whitelisted');
} else {
res.func(msg, args, ctx);
}
}
}
}
}