| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- const irc = require("irc");
- Object.assign(global, require("./config.js"));
- function User(nick) {
- this.admin = false;
- this.name = nick;
- this.score = 0;
- }
- User.prototype.setModeChar = function(mode) {
- this.admin = !!(mode && (mode.indexOf('~') >= 0 || mode.indexOf('&') >= 0 || mode.indexOf('@') >= 0 || mode.indexOf('%') >= 0));
- }
- User.prototype.setMode = function(mode) {
- if (mode == 'h' || mode == 'o' || mode == 'q' || mode == 'a')
- this.admin = true;
- };
- User.prototype.unsetMode = function(mode) {
- if (mode == 'h' || mode == 'o' || mode == 'q' || mode == 'a')
- this.admin = false;
- };
- function KnackiBot() {
- var _this = this;
- this.name = IRC_BOTNAME;
- this.password = NS_PASSWORD;
- this.modules = MODULES;
- for (var i in this.modules)
- this.modules[i].init(this, i);
- this.bot = new irc.Client(IRC_HOSTNAME, this.name, {
- channels: Object.keys(this.modules),
- userName: this.name,
- realName: this.name,
- stripColors: true
- });
- if (USE_NS) {
- var _this = this,
- registerHandler = 0;
- var tryRegister = function() {
- console.log("Trying to identify using NickServ");
- _this.bot.say("NickServ", "identify " +NS_PASSWORD);
- };
- this.bot.addListener("raw", raw => {
- if (raw.rawCommand == "MODE" && raw.args[0] === _this.name) {
- var registeredMatch = (/(\+|-)[^\+-]*r/).exec(raw.args[1]);
- if (!registeredMatch)
- return;
- if (registeredMatch[1] === '-') {
- if (registerHandler === undefined) {
- registerHandler = setInterval(tryRegister, 30000);
- tryRegister();
- }
- } else {
- if (registerHandler !== undefined) {
- clearInterval(registerHandler);
- registerHandler = undefined;
- }
- }
- }
- });
- this.bot.addListener("registered", () => {
- registerHandler = setInterval(tryRegister, 30000);
- tryRegister();
- });
- }
- this.bot.addListener("error", console.error);
- this.bot.addListener("join", (chan, nick) => {
- chan = chan.toLowerCase();
- if (_this.modules[chan]) {
- if (nick == _this.name)
- _this.modules[chan].onSelfJoin();
- else
- _this.modules[chan].onJoin(nick);
- }
- });
- this.bot.addListener("names", (chan, nicks) => {
- chan = chan.toLowerCase();
- _this.modules[chan] && _this.modules[chan].onNameList(nicks);
- });
- this.bot.addListener("part", (chan, nick) => {
- chan = chan.toLowerCase();
- _this.modules[chan] && _this.modules[chan].onNickPart(nick);
- });
- this.bot.addListener("kick", (chan, nick) => {
- chan = chan.toLowerCase();
- _this.modules[chan] && _this.modules[chan].onNickPart(nick);
- });
- this.bot.addListener("nick", (oldNick, newNick) => {
- for (var i in _this.modules)
- _this.modules[i].onRename(oldNick, newNick);
- });
- this.bot.addListener("+mode", (chan, by, mode, user) => {
- chan = chan.toLowerCase();
- user && _this.modules[chan] && _this.modules[chan].onAddMode(user, mode);
- });
- this.bot.addListener("-mode", (chan, by, mode, user) => {
- chan = chan.toLowerCase();
- user && _this.modules[chan] && _this.modules[chan].onRemMode(user, mode);
- });
- this.bot.addListener("message", (user, room, text) => {
- room = room.toLowerCase();
- _this.modules[room] && _this.modules[room].onMessage(user, text);
- });
- }
- KnackiBot.prototype.createUser = function(nick) {
- return new User(nick);
- }
- KnackiBot.prototype.voice = function(chan, username) {
- var usernames = Array.isArray(username) ? username : [ username ];
- if (usernames.length > 10)
- {
- this.voice(chan, usernames.slice(0, 10));
- this.voice(chan, usernames.slice(10));
- return;
- }
- usernames.splice(0, 0, "MODE", chan, "+" +"v".repeat(usernames.length));
- this.bot.send.apply(this.bot, usernames);
- }
- KnackiBot.prototype.sendNotice = function(username, msg) {
- this.bot.notice(username, msg);
- }
- KnackiBot.prototype.sendMsg = function(room, msg) {
- this.bot.say(room, msg);
- }
- new KnackiBot();
|