Last active
April 5, 2018 23:13
-
-
Save Announcement/c9638c3edcc818541a691b6cafa0d45e to your computer and use it in GitHub Desktop.
The Official regular expression IRC bot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const env = this | |
module.exports = function* addMessage ({ who, what, when, where }) { | |
const command = (/^(?:official|\:)\:\s*(\S+)\s*(.*)/i).exec(what) | |
if (command === null) return | |
if (command[1].toLowerCase() === 'run') { | |
const expression = command[2].match(/\/((?:\\.|[^\/])+)\/([a-z]*)\s*(.+)/) | |
const compiled = new RegExp(expression[1], expression[2]) | |
const string = expression[3] | |
const executed = compiled.exec(string) | |
const output1 = executed === null ? 'Did not match!' : `Matched: "${executed[0]}"` | |
yield output1 | |
if (executed !== null && executed.slice(1).length > 0) { | |
yield 'Groups: ' + executed.slice(1).map(it => `"${it}"`).join(', ') + (executed.groups && Object.entries(executed.groups).length > 0 ? ' /// Named Groups: ' + Object.entries(executed.groups).map(([k,v]) => `${k}: "${v}"`).join(', ') : '') | |
} | |
} | |
if (command[1].toLowerCase() === 'join') { | |
this.join(command[2]) | |
} | |
if (command[1].toLowerCase() === 'part') { | |
this.part(...command[2].split(/\s+/g)) | |
} | |
if (command[1].toLowerCase() === 'updates') { | |
let latestUpdate = env.bot.initialized | |
for (const update of env.bot.updates) | |
latestUpdate = update | |
yield `${env.bot.nickname} has been updated ${env.bot.updates.size} as of ${latestUpdate.toUTCString()}` | |
} | |
if (command[1].toLowerCase() === 'uptime') { | |
yield `${bot.nickname} has been up since ${bot.initialized.toUTCString()}` | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const vm = require('vm') | |
const irc = require('irc') | |
const sqlite3 = require('sqlite3') | |
const verbose = sqlite3.verbose() | |
db = new verbose.Database('./sqlitedb') | |
let getMessages = require('./bot.js') | |
let updates = new Set() | |
const nickname = 'Official' | |
const initialized = new Date | |
// db.serialize(function () { | |
// db.run(`CREATE TABLE Messages (why INTEGER PRIMARY KEY, when INT, who TEXT, where TEXT, what TEXT);`) | |
// }) | |
function database_each (callback) { | |
db.serialize(function () { | |
db.each(`SELECT * FROM Messages`, function (error, row) { | |
if (error) console.log(error); else { | |
callback(row) | |
} | |
}) | |
}) | |
} | |
function database_all (callback) { | |
db.serialize(function () { | |
db.all(`SELECT * FROM Messages`, function (error, rows) { | |
if (error) console.log(error); else { | |
callback(rows) | |
} | |
}) | |
}) | |
} | |
fs.watch(__dirname, function (eventName, filename) { | |
const sandbox = {} | |
const options = {} | |
options.filename = filename | |
options.timeout = 5000 | |
sandbox.module = {} | |
sandbox.module.exports = {} | |
sandbox.exports = sandbox.module.exports | |
sandbox.process = {} | |
sandbox.process.version = process.version | |
sandbox.process.versions = process.versions | |
sandbox.bot = {} | |
sandbox.bot.updates = new Set(updates) | |
sandbox.bot.nickname = nickname | |
sandbox.bot.initialized = initialized | |
sandbox.bot.colors = irc.colors | |
sandbox.bot.messages = {} | |
sandbox.bot.messages.each = database_each | |
sandbox.bot.messages.all = database_all | |
sandbox.bot._db | |
const context = vm.createContext(sandbox) | |
if (filename === 'bot.js') | |
fs.readFile(filename, 'utf8', function (error, contents) { | |
if (!error) { | |
const script = new vm.Script(contents, options) | |
script.runInContext(sandbox) | |
getMessages = sandbox.module.exports | |
updates.add(new Date()) | |
console.log('Bot received an automatic update!') | |
} | |
}) | |
}) | |
const connections = { | |
'irc.freenode.net': [ '##success', '#working.name' ], | |
// 'localhost': [ '#help', '#chat', '#test' ] | |
} | |
const servers = new Map | |
for (const [server, channels] of Object.entries(connections)) { | |
servers.set(server, new Map) | |
for (const channel of channels) | |
servers.get(server).set(channel, new Set) | |
} | |
function getConnection (server) { | |
const client = new irc.Client(server, nickname) | |
const channels = servers.get(server) | |
const channelParticipants = new Map | |
client._channels = channels | |
client._channelParticipants = channelParticipants | |
console.log(channels) | |
client.on('registered', onRegistered) | |
client.on('message', onMessage) | |
client.on('names', onNames) | |
// client.on('selfMessage', onSelfMessage) | |
client.on('join', onJoin) | |
function onNames (channel, nicks) { | |
channelParticipants.set(channel, nicks) | |
} | |
function onRegistered (message/*:string*/)/*: void */ { | |
client.say('nickserv', 'identify') | |
for (const [channel] of channels) | |
client.join(channel) | |
} | |
function onJoin (channel, nick, message) {} | |
function onMessage (nick/*:string*/, to/*:string*/, text/*:string*/, message)/*:void*/ { | |
if (!servers.get(server).has(to)) | |
servers.get(server).set(to, new Set) | |
const who = nick | |
const when = new Date() | |
const where = to | |
const what = text | |
servers.get(server).get(to).add({ who, what, when, where }) | |
db.serialize(function () { | |
db.run(`INSERT INTO Messages("when","who","where","what") VALUES($when, $who, $where, $what)`, { | |
$when: when, | |
$where: where, | |
$who: who, | |
$what: what | |
}) | |
}) | |
try{ | |
for (const message of getMessages.call(client, { who, what, when, where })) | |
client.say(to, message) | |
} catch (exception) { | |
client.say(to, 'Something went wrong!') | |
console.log(exception) | |
} | |
} | |
} | |
for (const [server] of servers) getConnection(server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment