| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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;
- this.bot = new irc.Client(IRC_HOSTNAME, this.name, {
- channels: Object.keys(this.modules),
- userName: this.name,
- realName: this.name,
- stripColors: true
- });
- for (var i in this.modules) {
- if (!Array.isArray(this.modules[i]))
- this.modules[i] = [this.modules[i]];
- this.modules[i].activeGame = this.modules[i].length == 1 ? this.modules[i][0] : null;
- if (this.modules[i].activeGame)
- this.modules[i].activeGame.onActivate();
- this.modules[i].forEach(mod => mod.init(this, i));
- }
- 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.setModes(this.name, "-R");
- }
- }
- }
- });
- 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].forEach(i => i.onSelfJoin());
- else
- _this.modules[chan].forEach(i => i.onJoin(nick));
- }
- });
- this.bot.addListener("names", (chan, nicks) => {
- chan = chan.toLowerCase();
- _this.modules[chan] && _this.modules[chan].forEach(i => i.onNameList(nicks));
- });
- this.bot.addListener("part", (chan, nick) => {
- chan = chan.toLowerCase();
- _this.modules[chan] && _this.modules[chan].forEach(i => i.onNickPart(nick));
- });
- this.bot.addListener("kick", (chan, nick) => {
- chan = chan.toLowerCase();
- _this.modules[chan] && _this.modules[chan].forEach(i => i.onNickPart(nick));
- });
- this.bot.addListener("nick", (oldNick, newNick) => {
- if (this.name === oldNick)
- this.name = newNick;
- else
- for (var i in _this.modules)
- _this.modules[chan] && _this.modules[i].forEach(i => i.onRename(oldNick, newNick));
- });
- this.bot.addListener("+mode", (chan, by, mode, user) => {
- chan = chan.toLowerCase();
- user && _this.modules[chan] && _this.modules[chan].forEach(i => i.onAddMode(user, mode));
- });
- this.bot.addListener("-mode", (chan, by, mode, user) => {
- chan = chan.toLowerCase();
- user && _this.modules[chan] && _this.modules[chan].forEach(i => i.onRemMode(user, mode));
- });
- this.bot.addListener("message", (user, room, text) => {
- room = room.toLowerCase();
- if (_this.modules[room]) {
- if (text.substr(0, 1) == "!" && !_this.modules[room].activeGame) {
- var found = false;
- for (var i =0, len = _this.modules[room].length; i < len; ++i) {
- if ('!' +_this.modules[room][i].getName() === text.trim().toLowerCase()) {
- console.info("Loading game " +text);
- _this.modules[room].activeGame = _this.modules[room][i];
- _this.modules[room].activeGame.onActivate();
- found = true;
- break;
- }
- }
- if (!found) {
- this.sendMsg(room, "Jeux disponibles: " +_this.modules[room].map(i => '!' +i.getName()).join(", "));
- return;
- }
- }
- if (_this.modules[room].activeGame)
- _this.modules[room].activeGame.onMessage(user, text);
- }
- });
- }
- KnackiBot.prototype.endGame = function(game) {
- for (var i in this.modules)
- if (this.modules[i].activeGame && this.modules[i].activeGame === game) {
- this.modules[i].activeGame = null;
- return;
- }
- }
- KnackiBot.prototype.createUser = function(nick) {
- return new User(nick);
- }
- KnackiBot.prototype.kick = function(chan, pseudo) {
- console.info("Kicking " +pseudo +" from " +chan);
- this.bot.send.call(this.bot, "KICK", chan, pseudo);
- }
- KnackiBot.prototype.setModes = function(chan, modes) {
- this.bot.send.call(this.bot, "MODE", chan, modes);
- }
- KnackiBot.prototype.setVoice = function(chan, username, direction) {
- 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, (direction > 0 ? '+' : '-') +"v".repeat(usernames.length));
- this.bot.send.apply(this.bot, usernames);
- }
- KnackiBot.prototype.voice = function(chan, username) {
- this.setVoice(chan, username, 1);
- }
- KnackiBot.prototype.devoice = function(chan, username) {
- this.setVoice(chan, username, -1);
- }
- KnackiBot.prototype.invite = function(channel, pseudo) {
- this.bot.send.call(this.bot, "INVITE", pseudo, channel);
- }
- KnackiBot.prototype.sendNotice = function(username, msg) {
- this.bot.notice(username, msg);
- }
- KnackiBot.prototype.sendMsg = function(room, msg) {
- this.bot.say(room, msg);
- }
- new KnackiBot();
|