Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 临时防喷子用
- */
- 'use strict';
- const fs = require('fs');
- const irc = require('irc');
- const CHANNEL = '频道';
- const NICK = '昵称';
- const USER = '用户名';
- const PASS = '密码';
- const CONFIGFILE = './autokick.json';
- let config = {};
- try {
- config = require(CONFIGFILE) || {};
- } catch (e) {
- }
- config.words = config.words || {};
- config.wordid = config.wordid || 0;
- const save = () => {
- fs.writeFile(CONFIGFILE, JSON.stringify(config), (err) => {});
- };
- console.log('Starting IRCBot...');
- const ircClient = new irc.Client('irc.freenode.net', NICK, {
- userName: USER,
- realName: USER,
- port: 6697,
- autoRejoin: true,
- channels: [CHANNEL],
- secure: true,
- floodProtection: true,
- floodProtectionDelay: 300,
- sasl: true,
- password: PASS,
- encoding: 'UTF-8',
- autoConnect: false,
- });
- ircClient.on('registered', (message) => {
- console.log('IRCBot Registered.');
- });
- ircClient.on('join', (channel, nick, message) => {
- if (nick === ircClient.nick) {
- console.log(`IRCBot has joined channel: ${channel} as ${nick}`);
- }
- });
- ircClient.on('error', (message) => {
- console.log(`IRCBot Error: ${message.command}`, true);
- });
- const isRegistered = (nick, callback) => {
- ircClient.whois(nick, (info) => {
- if (info.account) {
- callback(true);
- } else {
- callback(false);
- }
- });
- };
- const isOp = (nick, callback) => {
- ircClient.whois(nick, (info) => {
- if (!info) {
- callback(false);
- }
- for (let chan of info.channels) {
- if (chan.toLowerCase() === `@${CHANNEL.toLowerCase()}` || chan.toLowerCase() === `@+${CHANNEL.toLowerCase()}`) {
- callback(true);
- return;
- }
- }
- callback(false);
- });
- };
- const isVoiced = (nick, callback) => {
- ircClient.whois(nick, (info) => {
- if (!info) {
- callback(false);
- }
- for (let chan of info.channels) {
- if (chan.toLowerCase() === `+${CHANNEL.toLowerCase()}`) {
- callback(true);
- return;
- }
- }
- callback(false);
- });
- };
- const isAdmin = (nick, callback) => {
- ircClient.whois(nick, (info) => {
- if (!info) {
- callback(false);
- }
- for (let chan of info.channels) {
- if (chan.toLowerCase() === `@${CHANNEL.toLowerCase()}` ||
- chan.toLowerCase() === `+${CHANNEL.toLowerCase()}` ||
- chan.toLowerCase() === `@+${CHANNEL.toLowerCase()}`) {
- callback(true);
- return;
- }
- }
- callback(false);
- });
- };
- ircClient.on('message', (nick, to, text, message) => {
- let isPrivate = to === ircClient.nick;
- let target = isPrivate ? to : CHANNEL;
- let s = text.split(' ');
- let cmd = s[0];
- let param = s.slice(1).join(' ');
- switch (cmd.toLowerCase()) {
- case "@addword":
- isAdmin(nick, (r) => {
- if (r && param.trim() !== '') {
- config.wordid++;
- config.words[config.wordid] = param;
- save();
- ircClient.say(target, `Added word #${config.wordid}: ${param}`);
- }
- });
- break;
- case "@listwords":
- let words = ['Bad words:'];
- for (let w in config.words) {
- words.push(`#${w}: ${config.words[w]}`);
- }
- ircClient.say(nick, words.join('\n'));
- break;
- case "@delword":
- isAdmin(nick, (r) => {
- if (r && param.trim() !== '') {
- param = param.replace(/#/g, '');
- console.log(config.words);
- console.log(param);
- if (config.words[param]) {
- delete config.words[param];
- }
- save();
- ircClient.say(target, `Removed word #${param}`);
- }
- });
- break;
- case '@setautokickreason':
- isAdmin(nick, (r) => {
- if (r) {
- config.reason = param;
- save();
- ircClient.say(target, `Changed reason to ${param}`);
- }
- });
- break;
- case "@autokickon":
- isAdmin(nick, (r) => {
- if (r) {
- config.disabled = false;
- save();
- ircClient.say(CHANNEL, 'AutoKickBot on');
- }
- });
- break;
- case "@autokickoff":
- isAdmin(nick, (r) => {
- if (r) {
- config.disabled = true;
- save();
- ircClient.say(CHANNEL, 'AutoKickBot off');
- }
- });
- break;
- }
- if (!isPrivate && !config.disabled) {
- const kickunregistered = (r) => {
- if (!r) {
- let host = message.host;
- ircClient.send('KICK', CHANNEL, nick, `${config.reason || ''}`);
- ircClient.send('MODE', CHANNEL, '+b', `*!*@${host}`);
- }
- };
- for (let w in config.words) {
- if (text.match(new RegExp(config.words[w], 'g'))) {
- isRegistered(nick, kickunregistered);
- }
- }
- }
- });
- ircClient.connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement