refactor whitelist and add restart cmd

This commit is contained in:
jane 2020-11-21 18:12:14 -05:00
parent 06ee822be3
commit 157f64d2ae
3 changed files with 35 additions and 8 deletions

24
bot.js
View file

@ -16,19 +16,31 @@ const bot = new Eris(cfg.token);
global.ctx = { global.ctx = {
bot: bot, bot: bot,
log_level: levels[cfg.log_level.toUpperCase()] || levels.WARN, log_level: levels[cfg.log_level.toUpperCase()] || levels.WARN,
whitelist: cfg.user_whitelist || [],
set_ctx: (key, value) => { set_ctx: (key, value) => {
global.ctx[key] = value; global.ctx[key] = value;
}, },
}; };
const log = new Logger(filename(import.meta.url), global.ctx.log_level); 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) => { const checkUser = (user) => {
if (!cfg.whitelist.includes(guild.id)) { if (cfg.user_whitelist.includes(user.id)) {
log.info(`Leaving guild not on whitelist: ${guild.name}`); return true;
guild.leave();
} }
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', () => { bot.on('ready', () => {

View file

@ -17,4 +17,15 @@ class PingCommand extends Command {
initializer.addCommand(new PingCommand()); 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; export default initializer;

View file

@ -39,7 +39,7 @@ export class CommandInitializer {
export default class CommandParser { export default class CommandParser {
constructor(ctx, prefix = ';') { constructor(ctx, prefix = ';') {
this.log = new Logger(filename(import.meta.url), ctx.log_level); this.log = new Logger(filename(import.meta.url), ctx.log_level);
this.prefix = prefix ? prefix : this.prefix; this.prefix = prefix ? prefix : this.prefix || ';';
} }
log; log;
prefix; prefix;
@ -91,6 +91,10 @@ export default class CommandParser {
if (msg.author.bot) { if (msg.author.bot) {
return; 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);
this.log.debug(msg.content.startsWith(this.prefix)); this.log.debug(msg.content.startsWith(this.prefix));
this.log.debug(msg.content[0]); this.log.debug(msg.content[0]);
@ -106,7 +110,7 @@ export default class CommandParser {
if (res != undefined) { if (res != undefined) {
this.log.debug(`execute function ${res.name}`); 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'); msg.channel.createMessage('not whitelisted');
} else { } else {
res.func(msg, args, ctx); res.func(msg, args, ctx);