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 = {
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', () => {

View File

@ -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;

View File

@ -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);