Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- // Create the configuration
- var config = {
- channels: ["##bottest", "#futurology", "#mybots"],
- server: "irc.snoonet.org",
- botName: "AMAbot"
- };
- var released = true;
- start = "askreddit"
- mode = "priority"
- var subredditList = ["askreddit+AskHistorians+askscience+explainlikeimfive", "askmen+askwomen+askculinary", "futurology+philosophy+psychonaut+MachineLearning+learnprogramming", "all"];
- // Get the lib
- var irc = require("irc");
- // Create the bot name
- var bot = new irc.Client(config.server, config.botName, {
- channels: config.channels
- });
- // Listen for joins
- bot.addListener("join", function(channel, who) {
- // Welcome them in!
- //bot.say(channel, who + "...dude...welcome back!");
- });
- // Listen for any message, PM said user when he posts
- bot.addListener("message", function(from, to, text, message) {
- if (to == config.botName && from != config.botName){
- console.log(from, text)
- callback1 = function(output){
- bot.say(from, output);
- }
- setTimeout(function(){getSearch(text, false, subredditList, callback1);}, (released ? 0 : 4000));
- } else {
- if (text.toLowerCase().indexOf("!ask ") == 0){
- callback1 = function(output){
- bot.say(to, output);
- }
- setTimeout(function(){getSearch(text, true, subredditList, callback1);}, (released ? 0 : 4000));
- }
- }
- });
- // Listen for any message, say to him/her in the room
- bot.addListener("message", function(from, to, text, message) {
- });
- function getSearch(question, slice, subreddits, callback1){
- if (slice){
- question2 = question.slice(5, question.length)
- }
- question2 = encodeURIComponent(question2)
- console.log(question)
- var options = {
- host: 'www.reddit.com',
- path: '/r/'+subreddits[0]+'/search.json?q=' + question2 + '&restrict_sr=on&sort=relevance&t=all',
- headers: {'user-agent': '/u/Noncomment IRC bot \'AMAbot\'; #futurology, #bottest'}
- };
- callback = function(response) {
- var str = '';
- //another chunk of data has been recieved, so append it to `str`
- response.on('data', function (chunk) {
- str += chunk;
- });
- //the whole response has been recieved, so we just print it out here
- response.on('end', function () {
- var search = JSON.parse(str);
- if (search.data.children.length < 1){
- if (subreddits.length>1){
- subreddits = subreddits.slice(1, subreddits.length);
- setTimeout(function(){getSearch(question, true, subreddits, callback1);}, 2000);
- } else {
- callback1("Sorry I don't know.");
- }
- } else {
- setTimeout(function(){getComment(search.data.children[0].data.permalink, callback1);}, 2000);
- }
- });
- }
- http.request(options, callback).end();
- }
- function getComment(permalink, callback1){
- console.log(permalink)
- var options = {
- host: 'www.reddit.com',
- path: permalink + ".json",
- headers: {'user-agent': '/u/Noncomment IRC bot \'AMAbot\'; #futurology, #bottest'}
- };
- callback = function(response) {
- var str = '';
- //another chunk of data has been recieved, so append it to `str`
- response.on('data', function (chunk) {
- str += chunk;
- });
- //the whole response has been recieved, so we just print it out here
- response.on('end', function () {
- var search = JSON.parse(str);
- var i = 0;
- var comments = search[1].data.children;
- while (i<comments.length){
- var comment = comments[i].data.body;
- console.log(comment);
- if (!(comment == "[deleted]") && comment.length < 500){
- callback1(comment);
- i = 0;
- break;
- }
- i++
- }
- if (i >= comments.length-1){
- callback1("Sorry this knowledge is forbidden.")
- }
- });
- }
- http.request(options, callback).end();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement