index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const irc = require("irc");
  2. Object.assign(global, require("./config.js"));
  3. function User(nick) {
  4. this.admin = false;
  5. this.name = nick;
  6. this.score = 0;
  7. }
  8. User.prototype.setModeChar = function(mode) {
  9. this.admin = !!(mode && (mode.indexOf('~') >= 0 || mode.indexOf('&') >= 0 || mode.indexOf('@') >= 0 || mode.indexOf('%') >= 0));
  10. }
  11. User.prototype.setMode = function(mode) {
  12. if (mode == 'h' || mode == 'o' || mode == 'q' || mode == 'a')
  13. this.admin = true;
  14. };
  15. User.prototype.unsetMode = function(mode) {
  16. if (mode == 'h' || mode == 'o' || mode == 'q' || mode == 'a')
  17. this.admin = false;
  18. };
  19. function KnackiBot() {
  20. var _this = this;
  21. this.name = IRC_BOTNAME;
  22. this.password = NS_PASSWORD;
  23. this.modules = MODULES;
  24. this.bot = new irc.Client(IRC_HOSTNAME, this.name, {
  25. channels: Object.keys(this.modules),
  26. userName: this.name,
  27. realName: this.name,
  28. stripColors: true
  29. });
  30. for (var i in this.modules) {
  31. if (!Array.isArray(this.modules[i]))
  32. this.modules[i] = [this.modules[i]];
  33. this.modules[i].activeGame = this.modules[i].length == 1 ? this.modules[i][0] : null;
  34. if (this.modules[i].activeGame)
  35. this.modules[i].activeGame.onActivate();
  36. this.modules[i].forEach(mod => mod.init(this, i));
  37. }
  38. if (USE_NS) {
  39. var _this = this,
  40. registerHandler = 0;
  41. var tryRegister = function() {
  42. console.log("Trying to identify using NickServ");
  43. _this.bot.say("NickServ", "identify " +NS_PASSWORD);
  44. };
  45. this.bot.addListener("raw", raw => {
  46. if (raw.rawCommand == "MODE" && raw.args[0] === _this.name) {
  47. var registeredMatch = (/(\+|-)[^\+-]*r/).exec(raw.args[1]);
  48. if (!registeredMatch)
  49. return;
  50. if (registeredMatch[1] === '-') {
  51. if (registerHandler === undefined) {
  52. registerHandler = setInterval(tryRegister, 30000);
  53. tryRegister();
  54. }
  55. } else {
  56. if (registerHandler !== undefined) {
  57. clearInterval(registerHandler);
  58. registerHandler = undefined;
  59. _this.setModes(this.name, "-R");
  60. }
  61. }
  62. }
  63. });
  64. this.bot.addListener("registered", () => {
  65. registerHandler = setInterval(tryRegister, 30000);
  66. tryRegister();
  67. });
  68. }
  69. this.bot.addListener("error", console.error);
  70. this.bot.addListener("join", (chan, nick) => {
  71. chan = chan.toLowerCase();
  72. if (_this.modules[chan]) {
  73. if (nick == _this.name)
  74. _this.modules[chan].forEach(i => i.onSelfJoin());
  75. else
  76. _this.modules[chan].forEach(i => i.onJoin(nick));
  77. }
  78. });
  79. this.bot.addListener("names", (chan, nicks) => {
  80. chan = chan.toLowerCase();
  81. _this.modules[chan] && _this.modules[chan].forEach(i => i.onNameList(nicks));
  82. });
  83. this.bot.addListener("part", (chan, nick) => {
  84. chan = chan.toLowerCase();
  85. _this.modules[chan] && _this.modules[chan].forEach(i => i.onNickPart(nick));
  86. });
  87. this.bot.addListener("kick", (chan, nick) => {
  88. chan = chan.toLowerCase();
  89. _this.modules[chan] && _this.modules[chan].forEach(i => i.onNickPart(nick));
  90. });
  91. this.bot.addListener("nick", (oldNick, newNick) => {
  92. if (this.name === oldNick)
  93. this.name = newNick;
  94. else
  95. for (var i in _this.modules)
  96. _this.modules[chan] && _this.modules[i].forEach(i => i.onRename(oldNick, newNick));
  97. });
  98. this.bot.addListener("+mode", (chan, by, mode, user) => {
  99. chan = chan.toLowerCase();
  100. user && _this.modules[chan] && _this.modules[chan].forEach(i => i.onAddMode(user, mode));
  101. });
  102. this.bot.addListener("-mode", (chan, by, mode, user) => {
  103. chan = chan.toLowerCase();
  104. user && _this.modules[chan] && _this.modules[chan].forEach(i => i.onRemMode(user, mode));
  105. });
  106. this.bot.addListener("message", (user, room, text) => {
  107. room = room.toLowerCase();
  108. if (_this.modules[room]) {
  109. if (text.substr(0, 1) == "!" && !_this.modules[room].activeGame) {
  110. var found = false;
  111. for (var i =0, len = _this.modules[room].length; i < len; ++i) {
  112. if ('!' +_this.modules[room][i].getName() === text.trim().toLowerCase()) {
  113. console.info("Loading game " +text);
  114. _this.modules[room].activeGame = _this.modules[room][i];
  115. _this.modules[room].activeGame.onActivate();
  116. found = true;
  117. break;
  118. }
  119. }
  120. if (!found) {
  121. this.sendMsg(room, "Jeux disponibles: " +_this.modules[room].map(i => '!' +i.getName()).join(", "));
  122. return;
  123. }
  124. }
  125. if (_this.modules[room].activeGame)
  126. _this.modules[room].activeGame.onMessage(user, text);
  127. }
  128. });
  129. }
  130. KnackiBot.prototype.endGame = function(game) {
  131. for (var i in this.modules)
  132. if (this.modules[i].activeGame && this.modules[i].activeGame === game) {
  133. this.modules[i].activeGame = null;
  134. return;
  135. }
  136. }
  137. KnackiBot.prototype.createUser = function(nick) {
  138. return new User(nick);
  139. }
  140. KnackiBot.prototype.kick = function(chan, pseudo) {
  141. console.info("Kicking " +pseudo +" from " +chan);
  142. this.bot.send.call(this.bot, "KICK", chan, pseudo);
  143. }
  144. KnackiBot.prototype.setModes = function(chan, modes) {
  145. this.bot.send.call(this.bot, "MODE", chan, modes);
  146. }
  147. KnackiBot.prototype.setVoice = function(chan, username, direction) {
  148. var usernames = Array.isArray(username) ? username : [ username ];
  149. if (usernames.length > 10)
  150. {
  151. this.voice(chan, usernames.slice(0, 10));
  152. this.voice(chan, usernames.slice(10));
  153. return;
  154. }
  155. usernames.splice(0, 0, "MODE", chan, (direction > 0 ? '+' : '-') +"v".repeat(usernames.length));
  156. this.bot.send.apply(this.bot, usernames);
  157. }
  158. KnackiBot.prototype.voice = function(chan, username) {
  159. this.setVoice(chan, username, 1);
  160. }
  161. KnackiBot.prototype.devoice = function(chan, username) {
  162. this.setVoice(chan, username, -1);
  163. }
  164. KnackiBot.prototype.invite = function(channel, pseudo) {
  165. this.bot.send.call(this.bot, "INVITE", pseudo, channel);
  166. }
  167. KnackiBot.prototype.sendNotice = function(username, msg) {
  168. this.bot.notice(username, msg);
  169. }
  170. KnackiBot.prototype.sendMsg = function(room, msg) {
  171. this.bot.say(room, msg);
  172. }
  173. new KnackiBot();