Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,199 changes: 0 additions & 1,199 deletions bot.js

This file was deleted.

38 changes: 38 additions & 0 deletions bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Plugins from './plugins';
import * as irc from 'irc';
import * as moment from 'moment';
import * as config from './config';

let bot: irc.Client;
Plugins().then((plugins) => {
// Create the bot
var bot = new irc.Client(config.server, config.name, {
// channels: config.channels,
localAddress: config.localAddress,
realName: config.realName,
autoRejoin: true
});

for (let messageType in plugins) {
if (config.debug) {
console.log(`registering plugins for ${messageType}`)
}
bot.addListener(messageType, (...args) => {
var isAdmin = false;
if (config.admins.length) {
config.admins.forEach(function (value, index, array) {
if (value.trim().toLowerCase() === args[0].trim().toLowerCase()) {
isAdmin = true;
}
});
}

const p: Promise<any[]> = Promise.resolve(args);
for (let plugin of plugins[messageType]) {
p.then(plugin(bot, isAdmin));
}
});
}
}, (reason) => {
console.log(`Failed to load any plugins, quitting: ${reason}`);
});
35 changes: 23 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@
"description": "An IRC bot to do anything we determine is useful",
"main": "bot.js",
"dependencies": {
"async": "^1.5.2",
"cheerio": "^0.19.0",
"google": "^1.4.0",
"google-search": "0.0.3",
"irc": "^0.4.0",
"jsdom": "^9.8.3",
"moment": "^2.11.2",
"moment-timezone": "^0.5.0",
"nickserv": "^0.4.1",
"request": "^2.67.0"
"async": "^2.4.0",
"cheerio": "^0.22.0",
"google": "^1.5.0",
"google-search": "^0.0.5",
"irc": "^0.5.2",
"jsdom": "^10.1.0",
"moment": "^2.18.1",
"moment-timezone": "^0.5.13",
"nickserv": "^0.4.4",
"request": "^2.81.0",
"ts-node": "^3.0.4"
},
"devDependencies": {
"@types/async": "^2.0.40",
"@types/cheerio": "^0.22.1",
"@types/core-js": "^0.9.41",
"@types/irc": "^0.3.32",
"@types/jsdom": "^2.0.30",
"@types/moment": "^2.13.0",
"@types/moment-timezone": "^0.2.34",
"@types/node": "^7.0.18",
"@types/request": "0.0.43"
},
"devDependencies": {},
"scripts": {
"test": "node bot"
"test": "node bot.js"
},
"repository": {
"type": "git",
Expand Down
31 changes: 31 additions & 0 deletions plugins/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as config from '../config';

export function getCommand(text: any): [string, string, string] {
// Break up command string
const command = text.match(/^\.(\S+)/);
if (config.debug) {
console.log(command);
}

let cmd: string, str: string, who: string;

// Parse commands
if (command && command.length) {
// Initialize
cmd = command[1];
str = command.input.replace(`${command[0]} `, '');
[, who] = str.split('> ');
if (who) {
str = str.replace(` > ${who}`, '');
}

// Debug
if (config.debug) {
console.log('cmd: ' + cmd);
console.log('str: ' + str);
console.log('who: ' + who);
}
}

return [cmd, who, str];
}
12 changes: 12 additions & 0 deletions plugins/error/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as config from '../../config';
import * as console from 'console';
import * as irc from 'irc';

export default (bot: irc.Client) => {
return ([message]: [string]) => {
if (config.log) {
console.log('BOT ERROR: ');
console.log(message);
}
};
};
79 changes: 79 additions & 0 deletions plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as config from '../config';
import * as console from 'console';
import * as fs from 'fs';

class PluginFuncs {
[key: string]: Function[];
}

export default function() {
const re = /(\.js)$/;

return new Promise<PluginFuncs>((resolve, reject) => {
fs.readdir(__dirname, (err, files) => {
if (err) {
reject(err);
return;
}

let promises: Promise<PluginFuncs>[] = new Array<Promise<PluginFuncs>>();

files.forEach((file) => {
promises.push(new Promise<PluginFuncs>((resolve, reject) => {
const filepath = `${__dirname}/${file}`;
fs.stat(filepath, (err, stats) => {
if (err) {
if (config.debug) {
console.log(`${filepath}: ${err}`)
}
resolve();
return;
}

if (stats.isDirectory()) {
fs.readdir(filepath, (err, files) => {
if (err) {
if (config.debug) {
console.log(`${filepath}: ${err}`)
}
resolve();
return;
}

let plugins: PluginFuncs = new PluginFuncs();
files.forEach((plugin) => {
if (plugin.match(re)) {
if (config.debug) {
console.log(`adding plugin ${file}/${plugin}`);
}
if (!plugins[file]) {
plugins[file] = new Array<Function>();
}
plugins[file].push(require(`./${file}/${plugin}`).default);
}
});
resolve(plugins);
});
} else {
resolve();
}
});
}));
});

Promise.all(promises).then((values) => {
let plugins = new PluginFuncs();
for (let p of values) {
for (let key in p) {
if (key && p[key]) {
plugins[key] = p[key];
}
}
}
resolve(plugins);
}).catch((reason) => {
reject(reason);
});
});
})
}
25 changes: 25 additions & 0 deletions plugins/message/0_ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as config from '../../config';
import * as irc from 'irc';

export default (bot: irc.Client, isAdmin: boolean) => {
return ([from, to, text, message]: [string, string, string, object]) => {
// Admins are never ignored
if (isAdmin) {
return [from, to, text, message];
}

from = from.trim().toLowerCase();

// Check for ignored
if (config.ignore.indexOf(from) > -1) {
return Promise.reject(`${from} has been ignored by an admin`);
}

// Check for muted
if (config.muted.indexOf(from) > -1) {
return Promise.reject(`${from} has been automatically muted`);
}

return Promise.resolve([from, to, text, message]);
};
};
18 changes: 18 additions & 0 deletions plugins/message/1_private.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as config from '../../config';
import * as console from 'console';
import * as irc from 'irc';

export default (bot: irc.Client, isAdmin: boolean) => {
return ([from, to, text, message]: [string, string, string, object]) => {
if (to === bot.nick) {
// Private message handler
if (config.debug) {
console.log('Private Message Handler!!');
}
bot.say(from, `Hey ${from}... I'm a bot and I'm not currently programmed to handle your private messages. Check back soon.`);
return Promise.reject('Private messages to the bot are not allowed.');
}

return Promise.resolve([from, to, text, message]);
}
}
50 changes: 50 additions & 0 deletions plugins/message/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as config from '../../config';
import { getCommand } from '../commands';
import * as console from 'console';
import * as irc from 'irc';
import * as request from 'request';

export default (bot: irc.Client) => {
return ([from, to, text, message]: [string, string, string, object]) => {
return new Promise<any[]>((resolve, reject) => {
let [cmd, who, trimmed] = getCommand(text);

if (!cmd || cmd !== 'bitcoin' && !cmd.startsWith('bitcoin:')) {
return resolve([from, to, text, message]);
}

if (config.debug) {
console.log('[Bitcoin Exchange Rate]');
}

let currency: string = 'USD';
// Check if they want a specific exchange rate
if (text.indexOf('.bitcoin:') === 0) {
let s = text.split(' ');
s = s[0].split(':');
currency = s[1].toUpperCase();
}

// Grab the blockchain information for the current currency type
request('https://blockchain.info/ticker', (error, response, body) => {
if (error) {
resolve([from, to, text, message]);
return;
}

let msg = `Unable to read blockchain information for the "${currency}" currency type. Please try again later :(`;

body = JSON.parse(body);
if (body.hasOwnProperty(currency)) {
body = body[currency];
msg = `Current ${currency} Bitcoin Value: ${body.symbol}${body.last}. [Buy @ ${body.symbol}${body.buy} and Sell @ ${body.symbol}${body.sell}]`;
}

const replyTo: string = who || from;
bot.say(to, `${replyTo}: ${msg}`);

resolve([from, to, text, message]);
});
})
}
}
33 changes: 33 additions & 0 deletions plugins/message/blame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as config from '../../config';
import { getCommand } from '../commands';
import * as console from 'console';
import * as irc from 'irc';

export default (bot: irc.Client) => {
return ([from, to, text, message]: [string, string, string, object]) => {
return new Promise<any[]>((resolve, reject) => {
let [cmd, who, trimmed] = getCommand(text);

if (cmd !== 'jquery') {
return resolve([from, to, text, message]);
}

if (config.debug) {
console.log(`[jQuery API search] for: ${trimmed}`);
}

let prefix = '';
if (who) {
prefix = `${who}: `;
}

let msg = `${prefix}It's all ${trimmed}'s fault!`;
if (trimmed === bot.nick) {
msg = "That's hilarious...";
}

bot.say(to, msg);
resolve([from, to, text, message])
})
}
}
24 changes: 24 additions & 0 deletions plugins/message/css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as config from '../../config';
import { getCommand } from '../commands';
import * as console from 'console';
import * as google from '../googleHelper';
import * as irc from 'irc';

export default (bot: irc.Client) => {
return ([from, to, text, message]: [string, string, string, object]) => {
return new Promise<any[]>((resolve, reject) => {
let [cmd, who, trimmed] = getCommand(text);

if (cmd !== 'css') {
return resolve([from, to, text, message]);
}

if (config.debug) {
console.log(`[CSS search] for: ${trimmed}`);
}

const query = `${trimmed} site:https://developer.mozilla.org/en-US/docs/Web/CSS`;
google.query(resolve, bot, query, from, to, who, text, message)
});
}
}
29 changes: 29 additions & 0 deletions plugins/message/flip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getCommand } from '../commands';
import * as irc from 'irc';

export default (bot: irc.Client) => {
return ([from, to, text, message]: [string, string, string, object]) => {
return new Promise<any[]>((resolve, reject) => {
let [cmd, who, trimmed] = getCommand(text);

if (cmd !== 'flip' && cmd !== 'flip!') {
return resolve([from, to, text, message]);
}

let prefix = '';
if (who) {
prefix = `${who}: `;
}

let msg = '';
if (cmd === 'flip!') {
msg = '┻━┻︵ \\(°□°)/ ︵ ┻━┻';
} else if (cmd === 'flip') {
msg = '(╯°□°)╯︵ ┻━┻';
}

bot.say(to, `${prefix}${msg}`);
resolve([from, to, text, message]);
});
}
}
Loading