Last active
October 23, 2016 11:51
-
-
Save larafale/358a6c171a42d27039cf189404e421ae to your computer and use it in GitHub Desktop.
This file contains 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
var irc = require('../lib/irc/lib/irc') | |
, utils = require('../lib/utils') | |
, _ = require('lodash') | |
, EventEmitter = require('events').EventEmitter | |
var IRCBot = module.exports = function(options){ | |
EventEmitter.call(this) | |
var requiredFields = ['server', 'name', 'rootCmd'] | |
this.options = options = options || {} | |
this.client = false | |
this.rootCmd = options.rootCmd || '' | |
this.defaultCmd = options.defaultCmd || '' | |
this.name = options.name | |
this.server = options.server | |
this.password = options.password | |
this.channels = options.channels || [] | |
this.cmds = options.cmds || {} | |
var missingFields = _(this).pick(_.isEmpty).keys().intersection(requiredFields).value().join(', ') | |
if(missingFields.length) | |
throw Error('missing parameters : ' + missingFields) | |
return this.listen() | |
} | |
IRCBot.prototype = new EventEmitter; | |
IRCBot.prototype.listen = function(channels, server){ | |
var self = this | |
, server = server || self.server | |
, channels = channels || self.channels | |
self.client = new irc.Client(server, self.name, { | |
password: self.password, | |
channels: channels, | |
debug: false | |
}) | |
self.client.addListener('message', function(from, to, message){ | |
if(from == self.name) return | |
self.read(from, to, message) | |
self.emit('message', { from: from, to: to, message: message }) | |
}) | |
self.client.addListener('whisper', function(from, to, message){ | |
self.whisper(from, 'back up') | |
}) | |
self.client.addListener('error', function(message){ | |
console.log('irc error: ', message) | |
}) | |
return this | |
} | |
IRCBot.prototype.read = function(from, to, message){ | |
var self = this | |
, args = (message || '').split(' ') | |
, cmd = args[0] | |
, action = args[1] | |
if(cmd != self.rootCmd) return | |
if(self.options.debug) console.log('['+to+']', from, ':', message) | |
if(!action) return self.say(to, self.options.usage, from) | |
if(!self.cmds[action] && self.defaultCmd){ | |
action = self.defaultCmd | |
message = [message.split(' ')[0], self.defaultCmd, message.split(' ').splice(1, message.split(' ').length).join(' ')].join(' ') | |
} | |
return self.cmds[action].call(self, { | |
channel: to | |
, user: from | |
, cmd: [message.split(' ')[0], message.split(' ')[1]].join(' ') | |
, message: message | |
}) | |
} | |
IRCBot.prototype.say = function(channel, message, mention){ | |
var self = this | |
if(mention) message = '@'+mention+' '+message | |
if(self.options.debug) console.log('>>', '['+channel+']', message) | |
setTimeout(function(){ self.client.say(channel, message) }, 1000) | |
return this | |
} | |
IRCBot.prototype.whisper = function(to, message){ | |
var self = this | |
if(self.options.debug) console.log('>>', '['+to+']', message) | |
setTimeout(function(){ self.client.say(to, '/w @'+to+' '+message) }, 1000) | |
return this | |
} | |
IRCBot.prototype.join = function(channel){ | |
var self = this | |
if(self.options.debug) console.log('joining '+channel) | |
self.client.join(channel + ' ' + self.password) | |
return this | |
} | |
IRCBot.prototype.part = function(channel){ | |
var self = this | |
if(self.options.debug) console.log('leaving '+channel) | |
self.client.part(channel) | |
return this | |
} | |
IRCBot.prototype.register = function(cmd, action){ | |
var self = this | |
self.cmds[cmd] = action | |
return this | |
} | |
var bot = new IRCBot({ | |
name: 'mr_flooz' | |
, rootCmd: '$flooz' | |
, defaultCmd: 'tip' | |
, usage: 'pour faire un don tape "$flooz montant tonmessage"' //$flooz tip|stats|top" | |
, password: 'oauth:xxxxxxx' | |
, server: 'irc.chat.twitch.tv' | |
, debug: true | |
}) | |
bot.register('tip', function(data){ | |
var self = this | |
// do stuff | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment