brainlet-irc/bots/NickServ.js

101 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-01-25 07:23:34 +02:00
/*
An IRC bot to manage Brainlet accounts
Copyright (C) 2020 hiimgoodpack
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const ircClient = require("irc");
const brainlet = require("../brainlet-lib/index.js");
module.exports = {
start: (config, {tokens, sockets}) => {
let bot = new ircClient.Client("localhost", "NickServ", {
realName: "A bot to let users manage Brainlet accounts from an IRC server"
});
const commands = {
help: (from) => {
bot.notice(from, config.responses.NickServ.help);
},
register: (from, [command, password, email]) => {
if (!password || !email) {
bot.notice(from, config.responses.NickServ.invalid);
return;
}
bot.notice(from, `Registering account ${from}...`);
brainlet.users.create(config.server, {
name: from,
email: email,
password: password
}).then(() => {
bot.notice(from, "Successfully registered account");
commands.identify(from, ["identify", from, password]);
}).catch((error) => {
bot.notice(from, `Failed to register account: ${error}`);
});
},
identify: (from, [command, name, password]) => {
if (!name || !password) {
bot.notice(from, config.responses.NickServ.invalid);
return;
}
bot.notice(from, `Logging into account ${name}...`);
brainlet.users.login(config.server, {
name: name,
password: password
}).then(({token = ""}) => {
bot.notice(from, "Successfully logged into account");
tokens[from] = token;
bot.notice(from, "Creating websocket connection...");
brainlet.categories.client(config.server, token).then((client) => {
sockets[from] = client;
bot.notice(from, "Successfully connected to server\n" +
"You will likely want to use \"/msg ChanServ INVITE\" now\n" +
"Use \"/msg ChanServ HELP\" for more information");
}).catch((error) => {
bot.notice(from, `Failed to create websocket connection: ${error}`);
});
}).catch((error) => {
bot.notice(from, `Failed to log into account: ${error}`);
});
}
};
bot.addListener("error", console.error);
bot.addListener("message", (from, channel, text, message) => {
// Make sure it's a private message channel the bot
if (channel !== bot.opt.nick)
return;
const args = text.split(" ");
if (args.length == 0) {
bot.notice(from, config.responses.NickServ.invalid);
return;
}
const command = commands[args[0].toLowerCase()];
if (!command) {
bot.notice(from, config.responses.NickServ.invalid);
return;
}
command(from, args);
});
}
};