Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var irc = require('irc'),
- config = require('./config/botconfig'),
- Sleep = require('sleep'),
- promise = require('bluebird');
- var options = {
- promiseLib: promise
- };
- var pgp = require('pg-promise')(options);
- var cn = {
- host : 'localhost',
- port : 5432,
- database : config.database.database,
- user : config.database.user,
- password : config.database.password
- };
- var db = pgp(cn);
- var bot = new irc.Client(config.irc.server, config.irc.botName, {
- } );
- var outQueue = function(who, output ) {
- if( !Array.isArray(output ) ) {
- bot.say(who, output );
- } else {
- output.map( function(speech) {
- bot.say(who, speech );
- Sleep.usleep(800000 );
- } );
- }
- };
- var botQuit = function() {
- bot.disconnect("Bye!");
- pgp.end();
- process.exit();
- };
- var channel_handler = function(nick, text, message ) {
- var words = text.toLowerCase();
- words = words.split( ' ' );
- var chan = message.args[0]; //Channel name
- var out = []; //Output queue for outQueue
- if( words[0][0] !== '!') {
- return; //Not a bot command, and not a logging bot, so not interested
- //in the channel message
- }
- switch(words[0]) {
- case '!help':
- outQueue(chan, "Sorry " + nick + ", help is coming soon!");
- break;
- case '!number':
- db.manyOrNone('SELECT doc from notetable where author = $1', nick)
- .then( function( data) {
- if( data.length === 0) {
- outQueue(chan, "Sorry " + nick + ", no notes from you");
- } else {
- outQueue( chan, nick + " you have " + data.length + " notes.");
- }
- })
- .catch( function( error) {
- console.log( "Error: " + error);
- botQuit();
- });
- break;
- case '!add':
- var data = "";
- for( var cnt = 1; cnt < words.length; cnt ++) {
- data = data + words[cnt] + " ";
- }
- db.none('INSERT INTO notetable(author, doc) VALUES($1, $2)', [nick, data])
- .then( function() {
- outQueue(chan, nick + " note added.");
- })
- .catch( function(error) {
- console.log("Error: " + error);
- botQuit();
- });
- break;
- case '!listall':
- db.manyOrNone('SELECT doc FROM notetable WHERE author = $1', nick)
- .then( function(data) {
- if(data.length === 0) {
- outQueue( chan, "Sorry " + nick + ", no notes for you");
- } else {
- out = [];
- data.forEach( function( row ) {
- out.push(row.doc);
- });
- outQueue( chan, out);
- }
- })
- .catch( function(error) {
- console.log("Error: " + error);
- botQuit();
- });
- break;
- case '!del':
- db.none('DELETE FROM notetable WHERE noteid = $1', words[1])
- .then( function(data) {
- outQueue( chan, "Record " + words[1] + " deleted");
- })
- .catch( function(error) {
- console.log("Error: " + error);
- botQuit();
- });
- break;
- case '!list':
- db.manyOrNone('SELECT noteid, doc FROM notetable WHERE author = $1', nick)
- .then( function(data) {
- if(data.length === 0) {
- outQueue(chan, "Sorry " + nick + ", no notes from you");
- } else {
- out = [];
- data.forEach( function( row ) {
- out.push( row.noteid + ":" + row.doc.substring(0, 15) );
- });
- outQueue( chan, out );
- }
- })
- .catch( function(error) {
- console.log("Error: " + error);
- botQuit();
- });
- break;
- default:
- console.log( words);
- break;
- }
- };
- bot.addListener( "motd", function( motd ) {
- bot.say( "nickserv", "identify " + config.irc.nickPass );
- bot.join(config.irc.channels[0]);
- } );
- bot.addListener( "error", function( message ) {
- console.log( message );
- } );
- bot.addListener( "pm", function( from, message ) {
- if(from !== config.irc.botOwner ) {
- console.log( "pm from : " + from );
- return;
- }
- //from bot owner, pm to bot
- var words = message.toLowerCase();
- words = message.split( ' ' );
- if( words[0] === "!quit" ) {
- bot.disconnect( "Yes Boss!" );
- pgp.end();
- process.exit();
- }
- } );
- bot.addListener( "join", function( channel, nick, message ) {
- if( nick === config.irc.botName ) {
- bot.addListener( "message"+channel, channel_handler );
- }
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement