index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }
  62. this.bot.addListener("error", console.error);
  63. this.bot.addListener("join", (chan, nick) => {
  64. chan = chan.toLowerCase();
  65. if (_this.modules[chan]) {
  66. if (nick == _this.name)
  67. _this.modules[chan].onSelfJoin();
  68. else
  69. _this.modules[chan].onJoin(nick);
  70. }
  71. });
  72. this.bot.addListener("names", (chan, nicks) => {
  73. chan = chan.toLowerCase();
  74. _this.modules[chan] && _this.modules[chan].onNameList(nicks);
  75. });
  76. this.bot.addListener("part", (chan, nick) => {
  77. chan = chan.toLowerCase();
  78. _this.modules[chan] && _this.modules[chan].onNickPart(nick);
  79. });
  80. this.bot.addListener("kick", (chan, nick) => {
  81. chan = chan.toLowerCase();
  82. _this.modules[chan] && _this.modules[chan].onNickPart(nick);
  83. });
  84. this.bot.addListener("nick", (oldNick, newNick) => {
  85. for (var i in _this.modules)
  86. _this.modules[i].onRename(oldNick, newNick);
  87. });
  88. this.bot.addListener("+mode", (chan, by, mode, user) => {
  89. chan = chan.toLowerCase();
  90. user && _this.modules[chan] && _this.modules[chan].onAddMode(user, mode);
  91. });
  92. this.bot.addListener("-mode", (chan, by, mode, user) => {
  93. chan = chan.toLowerCase();
  94. user && _this.modules[chan] && _this.modules[chan].onRemMode(user, mode);
  95. });
  96. this.bot.addListener("message", (user, room, text) => {
  97. room = room.toLowerCase();
  98. _this.modules[room] && _this.modules[room].onMessage(user, text);
  99. });
  100. }
  101. KnackiBot.prototype.createUser = function(nick) {
  102. return new User(nick);
  103. }
  104. KnackiBot.prototype.voice = function(chan, username) {
  105. var usernames = Array.isArray(username) ? username : [ username ];
  106. if (usernames.length > 10)
  107. {
  108. this.voice(chan, usernames.slice(0, 10));
  109. this.voice(chan, usernames.slice(10));
  110. return;
  111. }
  112. usernames.splice(0, 0, "MODE", chan, "+" +"v".repeat(usernames.length));
  113. this.bot.send.apply(this.bot, usernames);
  114. }
  115. KnackiBot.prototype.sendNotice = function(username, msg) {
  116. this.bot.notice(username, msg);
  117. }
  118. KnackiBot.prototype.sendMsg = function(room, msg) {
  119. this.bot.say(room, msg);
  120. }
  121. new KnackiBot();