Advertisement
robdrake041

robnotebot

Apr 15th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var     irc = require('irc'),
  2.     config = require('./config/botconfig'),
  3.     Sleep = require('sleep'),
  4.     promise = require('bluebird');
  5. var     options = {
  6.     promiseLib: promise
  7. };
  8. var     pgp = require('pg-promise')(options);
  9.  
  10. var     cn = {
  11.     host : 'localhost',
  12.     port : 5432,
  13.     database : config.database.database,
  14.     user : config.database.user,
  15.     password : config.database.password
  16. };
  17.  
  18. var db = pgp(cn);
  19.  
  20. var bot = new irc.Client(config.irc.server, config.irc.botName, {
  21. } );
  22.  
  23. var outQueue = function(who, output ) {
  24.     if( !Array.isArray(output ) ) {
  25.         bot.say(who, output );
  26.     } else {
  27.         output.map( function(speech) {
  28.             bot.say(who, speech );
  29.             Sleep.usleep(800000 );
  30.         } );
  31.     }
  32. };
  33.  
  34. var botQuit = function() {
  35.     bot.disconnect("Bye!");
  36.     pgp.end();
  37.     process.exit();
  38. };
  39.  
  40. var channel_handler = function(nick, text, message ) {
  41.  
  42.     var words = text.toLowerCase();
  43.     words = words.split( ' ' );
  44.  
  45.     var chan = message.args[0];     //Channel name
  46.  
  47.     var out = [];   //Output queue for outQueue
  48.     if( words[0][0] !== '!') {
  49.         return;     //Not a bot command, and not a logging bot, so not interested
  50.                 //in the channel message
  51.     }
  52.  
  53.     switch(words[0]) {
  54.         case '!help':
  55.             outQueue(chan, "Sorry " + nick + ", help is coming soon!");
  56.             break;
  57.         case '!number':
  58.             db.manyOrNone('SELECT doc from notetable where author = $1', nick)
  59.             .then( function( data) {
  60.                 if( data.length === 0) {
  61.                     outQueue(chan, "Sorry " + nick + ", no notes from you");
  62.                 } else {
  63.                     outQueue( chan, nick + " you have " + data.length + " notes.");
  64.                 }
  65.             })
  66.             .catch( function( error) {
  67.                 console.log( "Error: " + error);
  68.                 botQuit();
  69.             });
  70.             break;
  71.         case '!add':
  72.             var data = "";
  73.             for( var cnt = 1; cnt < words.length; cnt ++) {
  74.                 data = data + words[cnt] + " ";
  75.             }
  76.             db.none('INSERT INTO notetable(author, doc) VALUES($1, $2)', [nick, data])
  77.             .then( function() {
  78.                 outQueue(chan, nick + " note added.");
  79.             })
  80.             .catch( function(error) {
  81.                 console.log("Error: " + error);
  82.                 botQuit();
  83.             });
  84.             break;
  85.         case '!listall':
  86.             db.manyOrNone('SELECT doc FROM notetable WHERE author = $1', nick)
  87.             .then( function(data) {
  88.                 if(data.length === 0) {
  89.                     outQueue( chan, "Sorry " + nick + ", no notes for you");
  90.                 } else {
  91.                     out = [];
  92.                     data.forEach( function( row ) {
  93.                         out.push(row.doc);
  94.                     });
  95.                     outQueue( chan, out);
  96.                 }
  97.             })
  98.             .catch( function(error) {
  99.                 console.log("Error: " + error);
  100.                 botQuit();
  101.             });
  102.             break;
  103.         case '!del':
  104.             db.none('DELETE FROM notetable WHERE noteid = $1', words[1])
  105.             .then( function(data) {
  106.                 outQueue( chan, "Record " + words[1] + " deleted");
  107.             })
  108.             .catch( function(error) {
  109.                 console.log("Error: " + error);
  110.                 botQuit();
  111.             });
  112.             break;
  113.         case '!list':
  114.             db.manyOrNone('SELECT noteid, doc FROM notetable WHERE author = $1', nick)
  115.             .then( function(data) {
  116.                 if(data.length === 0) {
  117.                     outQueue(chan, "Sorry " + nick + ", no notes from you");
  118.                 } else {
  119.                     out = [];
  120.                     data.forEach( function( row ) {
  121.                         out.push( row.noteid + ":" + row.doc.substring(0, 15) );
  122.                     });
  123.                     outQueue( chan, out );
  124.                 }
  125.             })
  126.             .catch( function(error) {
  127.                 console.log("Error: " + error);
  128.                 botQuit();
  129.             });
  130.             break;
  131.         default:
  132.             console.log( words);
  133.             break;
  134.     }
  135. };
  136.  
  137. bot.addListener( "motd", function( motd ) {
  138.     bot.say( "nickserv", "identify " + config.irc.nickPass );
  139.     bot.join(config.irc.channels[0]);
  140. } );
  141.  
  142. bot.addListener( "error", function( message ) {
  143.     console.log( message );
  144. } );
  145.  
  146. bot.addListener( "pm", function( from, message ) {
  147.     if(from !== config.irc.botOwner ) {
  148.         console.log( "pm from : " + from );
  149.         return;
  150.     }
  151.  
  152.     //from bot owner, pm to bot
  153.     var words = message.toLowerCase();
  154.     words = message.split( ' ' );
  155.  
  156.     if( words[0] === "!quit" ) {
  157.         bot.disconnect( "Yes Boss!" );
  158.         pgp.end();
  159.         process.exit();
  160.     }
  161. } );
  162.  
  163. bot.addListener( "join", function( channel, nick, message ) {
  164.     if( nick === config.irc.botName ) {
  165.         bot.addListener( "message"+channel, channel_handler );
  166.     }
  167. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement