index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. for (var i in this.modules)
  25. this.modules[i].init(this, i);
  26. this.bot = new irc.Client(IRC_HOSTNAME, this.name, {
  27. channels: Object.keys(this.modules),
  28. userName: this.name,
  29. realName: this.name,
  30. stripColors: true
  31. });
  32. if (USE_NS)
  33. var _this = this,
  34. registerHandler = 0;
  35. var tryRegister = function() {
  36. console.log("Trying to identify using NickServ");
  37. _this.bot.say("NickServ", "identify " +NS_PASSWORD);
  38. };
  39. this.bot.addListener("raw", raw => {
  40. if (raw.rawCommand == "MODE" && raw.args[0] === _this.name) {
  41. var registeredMatch = (/(\+|-)[^\+-]*r/).exec(raw.args[1]);
  42. if (!registeredMatch)
  43. return;
  44. if (registeredMatch[1] === '-') {
  45. if (registerHandler === undefined) {
  46. registerHandler = setInterval(tryRegister, 30000);
  47. tryRegister();
  48. }
  49. } else {
  50. if (registerHandler !== undefined) {
  51. clearInterval(registerHandler);
  52. registerHandler = undefined;
  53. }
  54. }
  55. }
  56. });
  57. this.bot.addListener("registered", () => {
  58. registerHandler = setInterval(tryRegister, 30000);
  59. tryRegister();
  60. });
  61. this.bot.addListener("error", console.error);
  62. this.bot.addListener("join", (chan, nick) => {
  63. chan = chan.toLowerCase();
  64. if (_this.modules[chan]) {
  65. if (nick == _this.name)
  66. _this.modules[chan].onSelfJoin();
  67. else
  68. _this.modules[chan].onJoin(nick);
  69. }
  70. });
  71. this.bot.addListener("names", (chan, nicks) => {
  72. chan = chan.toLowerCase();
  73. _this.modules[chan] && _this.modules[chan].onNameList(nicks);
  74. });
  75. this.bot.addListener("part", (chan, nick) => {
  76. chan = chan.toLowerCase();
  77. _this.modules[chan] && _this.modules[chan].onNickPart(nick);
  78. });
  79. this.bot.addListener("kick", (chan, nick) => {
  80. chan = chan.toLowerCase();
  81. _this.modules[chan] && _this.modules[chan].onNickPart(nick);
  82. });
  83. this.bot.addListener("nick", (oldNick, newNick) => {
  84. for (var i in _this.modules)
  85. _this.modules[i].onRename(oldNick, newNick);
  86. });
  87. this.bot.addListener("+mode", (chan, by, mode, user) => {
  88. chan = chan.toLowerCase();
  89. user && _this.modules[chan] && _this.modules[chan].onAddMode(user, mode);
  90. });
  91. this.bot.addListener("-mode", (chan, by, mode, user) => {
  92. chan = chan.toLowerCase();
  93. user && _this.modules[chan] && _this.modules[chan].onRemMode(user, mode);
  94. });
  95. this.bot.addListener("message", (user, room, text) => {
  96. room = room.toLowerCase();
  97. _this.modules[room] && _this.modules[room].onMessage(user, text);
  98. });
  99. }
  100. KnackiBot.prototype.createUser = function(nick) {
  101. return new User(nick);
  102. }
  103. KnackiBot.prototype.voice = function(chan, username) {
  104. var usernames = Array.isArray(username) ? username : [ username ];
  105. if (usernames.length > 10)
  106. {
  107. this.voice(chan, usernames.slice(0, 10));
  108. this.voice(chan, usernames.slice(10));
  109. return;
  110. }
  111. usernames.splice(0, 0, "MODE", chan, "+" +"v".repeat(usernames.length));
  112. this.bot.send.apply(this.bot, usernames);
  113. }
  114. KnackiBot.prototype.sendNotice = function(username, msg) {
  115. this.bot.notice(username, msg);
  116. }
  117. KnackiBot.prototype.sendMsg = function(room, msg) {
  118. this.bot.say(room, msg);
  119. }
  120. new KnackiBot();