irc-bot/spells/summon.js

95 lines
2.9 KiB
JavaScript

let nodemailer = require("nodemailer");
const PushBullet = require("pushbullet");
// Used to summon users into the IRC via email to <user>@tilde.team
module.exports = class Summon {
constructor(client, from, to, incantation) {
this.name = "Summon";
this.client = client;
this.summoner = from;
this.channel = to;
this.incantation = incantation;
this.spell = "kunvoki";
this.cheatCode = "summon";
this.casted = false;
this.varsUsed = 2;
this.locked = false;
this.transporter = nodemailer.createTransport({
sendmail: true,
newline: "unix",
path: "/usr/sbin/sendmail"
});
this.pusher = new PushBullet(client.config.api.pushbullet);
}
test() {
if (this.incantation === this.spell) {
this.casted = true;
return this.cast.apply(this, arguments);
}
return { say: false };
}
cast(user, reason) {
let response = {
say: false,
debug: {},
content: ""
};
if (!user) {
response.say = true;
response.content = `${from}, malsagxulo!. You must tell me`;
response.content = `${response.content} who to summon.`;
return response;
}
if (!reason) {
reason = "<none>";
}
const subject = "You have been summoned!";
let text = `My bot, ${this.client.config.botName}, received`;
text = `${text} a summoning request for you`;
text = `${text} from ${this.summoner}`;
text = `${text} in channel ${this.channel}`;
text = `${text} for reason: ${reason}`;
this.transporter.sendMail({
from: this.client.config.email,
to: `${user}@tilde.team`,
subject: subject,
text: text
}, (err, info) => {
// DEBUG
// console.log("Summon::email", info);
response.debug.email = info;
});
// I obvious don't know everyone's Pushbullet API
if (user === this.client.config.author) {
const self = this;
self.pusher.devices().then(function (res) {
const devices = res.devices;
devices.forEach((device) => {
if (device.nickname === self.client.config.device) {
const iden = device.iden;
self.pusher.note(iden, subject, text, (err, info) => {
// DEBUG
// console.log("Summon::pusher", info);
response.debug.pusher = info;
});
}
});
});
}
const success = `Superba ${this.summoner}! You have summoned`;
response.say = true;
response.content = `${success} ${user}!`;
return response;
}
}