Advertisement
Houshalter

AMAbot.js

Jan 25th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2.  
  3. // Create the configuration
  4. var config = {
  5.     channels: ["##bottest", "#futurology", "#mybots"],
  6.     server: "irc.snoonet.org",
  7.     botName: "AMAbot"
  8. };
  9. var released = true;
  10. start = "askreddit"
  11. mode = "priority"
  12. var subredditList = ["askreddit+AskHistorians+askscience+explainlikeimfive", "askmen+askwomen+askculinary", "futurology+philosophy+psychonaut+MachineLearning+learnprogramming", "all"];
  13.  
  14. // Get the lib
  15. var irc = require("irc");
  16.  
  17. // Create the bot name
  18. var bot = new irc.Client(config.server, config.botName, {
  19.     channels: config.channels
  20. });
  21.  
  22. // Listen for joins
  23. bot.addListener("join", function(channel, who) {
  24.     // Welcome them in!
  25.     //bot.say(channel, who + "...dude...welcome back!");
  26. });
  27.  
  28. // Listen for any message, PM said user when he posts
  29. bot.addListener("message", function(from, to, text, message) {
  30.     if (to == config.botName && from != config.botName){
  31.         console.log(from, text)
  32.         callback1 = function(output){
  33.             bot.say(from, output);
  34.         }
  35.         setTimeout(function(){getSearch(text, false, subredditList, callback1);}, (released ? 0 : 4000));
  36.     } else {
  37.         if (text.toLowerCase().indexOf("!ask ") == 0){
  38.             callback1 = function(output){
  39.                 bot.say(to, output);
  40.             }
  41.             setTimeout(function(){getSearch(text, true, subredditList, callback1);}, (released ? 0 : 4000));
  42.         }
  43.     }
  44. });
  45.  
  46. // Listen for any message, say to him/her in the room
  47. bot.addListener("message", function(from, to, text, message) {
  48.  
  49. });
  50.  
  51. function getSearch(question, slice, subreddits, callback1){
  52.     if (slice){
  53.         question2 = question.slice(5, question.length)
  54.     }
  55.     question2 = encodeURIComponent(question2)
  56.     console.log(question)
  57.     var options = {
  58.       host: 'www.reddit.com',
  59.       path: '/r/'+subreddits[0]+'/search.json?q=' + question2 + '&restrict_sr=on&sort=relevance&t=all',
  60.       headers: {'user-agent': '/u/Noncomment IRC bot \'AMAbot\'; #futurology, #bottest'}
  61.     };
  62.  
  63.     callback = function(response) {
  64.       var str = '';
  65.  
  66.       //another chunk of data has been recieved, so append it to `str`
  67.       response.on('data', function (chunk) {
  68.         str += chunk;
  69.       });
  70.  
  71.       //the whole response has been recieved, so we just print it out here
  72.       response.on('end', function () {
  73.         var search = JSON.parse(str);
  74.         if (search.data.children.length < 1){
  75.             if (subreddits.length>1){
  76.                 subreddits = subreddits.slice(1, subreddits.length);
  77.                 setTimeout(function(){getSearch(question, true, subreddits, callback1);}, 2000);
  78.             } else {
  79.                 callback1("Sorry I don't know.");
  80.             }
  81.         } else {
  82.             setTimeout(function(){getComment(search.data.children[0].data.permalink, callback1);}, 2000);
  83.         }
  84.       });
  85.     }
  86.  
  87.     http.request(options, callback).end();
  88. }
  89.  
  90. function getComment(permalink, callback1){
  91.     console.log(permalink)
  92.     var options = {
  93.       host: 'www.reddit.com',
  94.       path: permalink + ".json",
  95.       headers: {'user-agent': '/u/Noncomment IRC bot \'AMAbot\'; #futurology, #bottest'}
  96.     };
  97.  
  98.     callback = function(response) {
  99.       var str = '';
  100.  
  101.       //another chunk of data has been recieved, so append it to `str`
  102.       response.on('data', function (chunk) {
  103.         str += chunk;
  104.       });
  105.  
  106.       //the whole response has been recieved, so we just print it out here
  107.       response.on('end', function () {
  108.         var search = JSON.parse(str);
  109.         var i = 0;
  110.         var comments = search[1].data.children;
  111.         while (i<comments.length){
  112.             var comment = comments[i].data.body;
  113.             console.log(comment);
  114.             if (!(comment == "[deleted]") && comment.length < 500){
  115.                 callback1(comment);
  116.                 i = 0;
  117.                 break;
  118.             }
  119.             i++
  120.         }
  121.         if (i >= comments.length-1){
  122.             callback1("Sorry this knowledge is forbidden.")
  123.         }
  124.       });
  125.     }
  126.  
  127.     http.request(options, callback).end();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement