irc-bot/spells/banish.js

78 lines
2.3 KiB
JavaScript

const fs = require("fs");
const path = require("path");
// Used to allow the author to manually banish someone
module.exports = class Banish {
constructor(client, from, to, incantation) {
this.name = "Banish";
this.client = client;
this.summoner = from;
this.channel = to;
this.incantation = incantation;
this.spell = "malpermesi";
this.cheatCode = "banish";
this.varsUsed = 2;
this.casted = false;
this.locked = true;
}
test() {
if (this.incantation === this.spell) {
// Only the author can use these spells
if (this.locked && this.summoner !== this.client.config.author) {
const say = "Blasfemo! That spell is forbidden to you."
this.client.say(this.channel, say);
return false;
}
this.casted = true;
return this.cast.apply(this, arguments);
}
return { say: false };
}
cast(user, ...reason) {
let response = {
say: false,
debug: {},
content: ""
};
if (user === this.client.config.author) return false;
if (!reason) {
reason = "<none>";
} else {
reason = reason.join(" ");
}
// Log reason for adding to the blacklist
const blacklistReason = `${user} was added for ${reason}\n`;
const dirname = this.client.config.dirname;
const log = path.resolve(dirname, "blacklist.log");
fs.appendFile(log, blacklistReason, (err) => {
if (err) throw err;
console.log("Blacklist", blacklistReason);
});
// Apply to current session
this.client.blacklist[user] = {
reason: reason,
when: Date.now()
};
// Apply to future sessions
const blacklist = JSON.stringify(this.client.blacklist);
const file = path.resolve(dirname, "blacklist.json");
fs.writeFile(file, blacklist, (err) => {
if (err) throw err;
console.log("Added to blacklist:", user);
});
const author = this.client.config.author;
this.client.say(author, `${user} was banished for reason ${reason}`);
return response;
}
}