Advertisement
Guest User

Untitled

a guest
May 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. /*
  2. * 临时防喷子用
  3. */
  4.  
  5. 'use strict';
  6.  
  7. const fs = require('fs');
  8. const irc = require('irc');
  9.  
  10. const CHANNEL = '频道';
  11. const NICK = '昵称';
  12. const USER = '用户名';
  13. const PASS = '密码';
  14. const CONFIGFILE = './autokick.json';
  15.  
  16. let config = {};
  17.  
  18. try {
  19. config = require(CONFIGFILE) || {};
  20. } catch (e) {
  21.  
  22. }
  23.  
  24. config.words = config.words || {};
  25. config.wordid = config.wordid || 0;
  26.  
  27. const save = () => {
  28. fs.writeFile(CONFIGFILE, JSON.stringify(config), (err) => {});
  29. };
  30.  
  31. console.log('Starting IRCBot...');
  32. const ircClient = new irc.Client('irc.freenode.net', NICK, {
  33. userName: USER,
  34. realName: USER,
  35. port: 6697,
  36. autoRejoin: true,
  37. channels: [CHANNEL],
  38. secure: true,
  39. floodProtection: true,
  40. floodProtectionDelay: 300,
  41. sasl: true,
  42. password: PASS,
  43. encoding: 'UTF-8',
  44. autoConnect: false,
  45. });
  46.  
  47. ircClient.on('registered', (message) => {
  48. console.log('IRCBot Registered.');
  49. });
  50.  
  51. ircClient.on('join', (channel, nick, message) => {
  52. if (nick === ircClient.nick) {
  53. console.log(`IRCBot has joined channel: ${channel} as ${nick}`);
  54. }
  55. });
  56.  
  57. ircClient.on('error', (message) => {
  58. console.log(`IRCBot Error: ${message.command}`, true);
  59. });
  60.  
  61. const isRegistered = (nick, callback) => {
  62. ircClient.whois(nick, (info) => {
  63. if (info.account) {
  64. callback(true);
  65. } else {
  66. callback(false);
  67. }
  68. });
  69. };
  70.  
  71. const isOp = (nick, callback) => {
  72. ircClient.whois(nick, (info) => {
  73. if (!info) {
  74. callback(false);
  75. }
  76.  
  77. for (let chan of info.channels) {
  78. if (chan.toLowerCase() === `@${CHANNEL.toLowerCase()}` || chan.toLowerCase() === `@+${CHANNEL.toLowerCase()}`) {
  79. callback(true);
  80. return;
  81. }
  82. }
  83.  
  84. callback(false);
  85. });
  86. };
  87.  
  88. const isVoiced = (nick, callback) => {
  89. ircClient.whois(nick, (info) => {
  90. if (!info) {
  91. callback(false);
  92. }
  93.  
  94. for (let chan of info.channels) {
  95. if (chan.toLowerCase() === `+${CHANNEL.toLowerCase()}`) {
  96. callback(true);
  97. return;
  98. }
  99. }
  100.  
  101. callback(false);
  102. });
  103. };
  104.  
  105. const isAdmin = (nick, callback) => {
  106. ircClient.whois(nick, (info) => {
  107. if (!info) {
  108. callback(false);
  109. }
  110.  
  111. for (let chan of info.channels) {
  112. if (chan.toLowerCase() === `@${CHANNEL.toLowerCase()}` ||
  113. chan.toLowerCase() === `+${CHANNEL.toLowerCase()}` ||
  114. chan.toLowerCase() === `@+${CHANNEL.toLowerCase()}`) {
  115. callback(true);
  116. return;
  117. }
  118. }
  119.  
  120. callback(false);
  121. });
  122. };
  123.  
  124. ircClient.on('message', (nick, to, text, message) => {
  125. let isPrivate = to === ircClient.nick;
  126. let target = isPrivate ? to : CHANNEL;
  127.  
  128. let s = text.split(' ');
  129. let cmd = s[0];
  130. let param = s.slice(1).join(' ');
  131.  
  132. switch (cmd.toLowerCase()) {
  133. case "@addword":
  134. isAdmin(nick, (r) => {
  135. if (r && param.trim() !== '') {
  136. config.wordid++;
  137. config.words[config.wordid] = param;
  138. save();
  139. ircClient.say(target, `Added word #${config.wordid}: ${param}`);
  140. }
  141. });
  142.  
  143. break;
  144.  
  145. case "@listwords":
  146. let words = ['Bad words:'];
  147.  
  148. for (let w in config.words) {
  149. words.push(`#${w}: ${config.words[w]}`);
  150. }
  151.  
  152. ircClient.say(nick, words.join('\n'));
  153. break;
  154.  
  155. case "@delword":
  156. isAdmin(nick, (r) => {
  157. if (r && param.trim() !== '') {
  158. param = param.replace(/#/g, '');
  159.  
  160. console.log(config.words);
  161. console.log(param);
  162.  
  163. if (config.words[param]) {
  164. delete config.words[param];
  165. }
  166. save();
  167. ircClient.say(target, `Removed word #${param}`);
  168. }
  169. });
  170. break;
  171.  
  172. case '@setautokickreason':
  173. isAdmin(nick, (r) => {
  174. if (r) {
  175. config.reason = param;
  176. save();
  177. ircClient.say(target, `Changed reason to ${param}`);
  178. }
  179. });
  180. break;
  181.  
  182. case "@autokickon":
  183. isAdmin(nick, (r) => {
  184. if (r) {
  185. config.disabled = false;
  186. save();
  187. ircClient.say(CHANNEL, 'AutoKickBot on');
  188. }
  189. });
  190. break;
  191.  
  192. case "@autokickoff":
  193. isAdmin(nick, (r) => {
  194. if (r) {
  195. config.disabled = true;
  196. save();
  197. ircClient.say(CHANNEL, 'AutoKickBot off');
  198. }
  199. });
  200. break;
  201. }
  202.  
  203. if (!isPrivate && !config.disabled) {
  204. const kickunregistered = (r) => {
  205. if (!r) {
  206. let host = message.host;
  207. ircClient.send('KICK', CHANNEL, nick, `${config.reason || ''}`);
  208. ircClient.send('MODE', CHANNEL, '+b', `*!*@${host}`);
  209. }
  210. };
  211.  
  212. for (let w in config.words) {
  213. if (text.match(new RegExp(config.words[w], 'g'))) {
  214. isRegistered(nick, kickunregistered);
  215. }
  216. }
  217. }
  218. });
  219.  
  220. ircClient.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement