From 157f64d2ae1d15587b71cba5ef1553bce1db1512 Mon Sep 17 00:00:00 2001 From: Jane Petrovna Date: Sat, 21 Nov 2020 18:12:14 -0500 Subject: [PATCH] refactor whitelist and add restart cmd --- bot.js | 24 ++++++++++++++++++------ cmd/reg.js | 11 +++++++++++ parser.js | 8 ++++++-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/bot.js b/bot.js index 1acc963..dcbc2ec 100644 --- a/bot.js +++ b/bot.js @@ -16,19 +16,31 @@ const bot = new Eris(cfg.token); global.ctx = { bot: bot, log_level: levels[cfg.log_level.toUpperCase()] || levels.WARN, - whitelist: cfg.user_whitelist || [], set_ctx: (key, value) => { global.ctx[key] = value; }, }; const log = new Logger(filename(import.meta.url), global.ctx.log_level); -const parse = new CommandParser(global.ctx); +const parse = new CommandParser(global.ctx, cfg.prefix || undefined); -const checkGuild = (guild) => { - if (!cfg.whitelist.includes(guild.id)) { - log.info(`Leaving guild not on whitelist: ${guild.name}`); - guild.leave(); +const checkUser = (user) => { + if (cfg.user_whitelist.includes(user.id)) { + return true; } + log.info(`user not on whitelist: ${user.username}`); + return false; +}; +const checkGuild = (guild) => { + if (cfg.whitelist.includes(guild.id)) { + return true; + } + log.info(`guild not on whitelist: ${guild.name}`); + return false; +}; + +global.ctx.whitelist = { + guild: checkGuild, + user: checkUser, }; bot.on('ready', () => { diff --git a/cmd/reg.js b/cmd/reg.js index dfa5480..fabb293 100644 --- a/cmd/reg.js +++ b/cmd/reg.js @@ -17,4 +17,15 @@ class PingCommand extends Command { initializer.addCommand(new PingCommand()); +class RestartCommand extends Command { + name = 'restart'; + func(msg, args, ctx) { + msg.channel.createMessage('restarting.').then(() => { + process.exit(); + }); + } +} + +initializer.addCommand(new RestartCommand()); + export default initializer; diff --git a/parser.js b/parser.js index eaa4c59..3c696c5 100644 --- a/parser.js +++ b/parser.js @@ -39,7 +39,7 @@ export class CommandInitializer { export default class CommandParser { constructor(ctx, prefix = ';') { this.log = new Logger(filename(import.meta.url), ctx.log_level); - this.prefix = prefix ? prefix : this.prefix; + this.prefix = prefix ? prefix : this.prefix || ';'; } log; prefix; @@ -91,6 +91,10 @@ export default class CommandParser { if (msg.author.bot) { return; } + if (msg.channel.guild && !ctx.whitelist.guild(msg.channel.guild)) { + msg.channel.createMessage('guild not whitelisted'); + return; + } this.log.debug(msg.content); this.log.debug(msg.content.startsWith(this.prefix)); this.log.debug(msg.content[0]); @@ -106,7 +110,7 @@ export default class CommandParser { if (res != undefined) { this.log.debug(`execute function ${res.name}`); - if (res.whitelist && ctx.whitelist.indexOf(msg.author.id) == -1) { + if (res.whitelist && !ctx.whitelist.user(msg.author)) { msg.channel.createMessage('not whitelisted'); } else { res.func(msg, args, ctx);