Skip to content

Commit 5b82a03

Browse files
committed
added real RSS feed support
1 parent d704276 commit 5b82a03

File tree

4 files changed

+79
-34
lines changed

4 files changed

+79
-34
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ node_modules
2828

2929
#auth file shouldn't be checked in!
3030
auth.json
31+
32+
#other runtime config files
33+
rss.json

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ A chat bot for discord app based off <a href="https://github.com/hydrabolt/disco
2222
- !delete => deletes a channel
2323
- @botname => responds when @mentioned
2424

25+
## RSS:
26+
you can create an rss.json file adding rss feeds as commands. See rss.json.example for details
27+
2528
# ToDo:
2629

2730
- Permissions!

discord_bot.js

+59-34
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ var commands = {
239239
usage: "<invite>",
240240
description: "joins the server it's invited to",
241241
process: function(bot,msg,suffix) {
242-
console.log(suffix);
243242
console.log(bot.joinServer(suffix,function(error,server) {
244243
console.log("callback: " + arguments);
245244
if(error){
@@ -316,11 +315,18 @@ var commands = {
316315
wolfram_plugin.respond(suffix,msg.channel,bot);
317316
}
318317
},
319-
"hotspatch": {
320-
description: "gets the latest patch notes for Heroes of the Storm from blizzpro",
318+
"rss": {
319+
description: "lists available rss feeds",
321320
process: function(bot,msg,suffix) {
322-
//http://heroesofthestorm.blizzpro.com/tag/patch-notes/feed/
323-
rssfeed("heroesofthestorm.blizzpro.com","/tag/patch-notes/feed/",bot,msg,suffix);
321+
/*var args = suffix.split(" ");
322+
var count = args.shift();
323+
var url = args.join(" ");
324+
rssfeed(bot,msg,url,count,full);*/
325+
bot.sendMessage(msg.channel,"Available feeds:", function(){
326+
for(var c in rssFeeds){
327+
bot.sendMessage(msg.channel,c + ": " + rssFeeds[c].url);
328+
}
329+
});
324330
}
325331
},
326332
"reddit": {
@@ -331,48 +337,66 @@ var commands = {
331337
if(suffix){
332338
path = "/r/"+suffix+path;
333339
}
334-
rssfeed("www.reddit.com",path,bot,msg,"");
340+
rssfeed(bot,msg,"https://www.reddit.com"+path,1,false);
335341
}
336342
}
337343
};
344+
try{
345+
var rssFeeds = require("./rss.json");
346+
function loadFeeds(){
347+
for(var cmd in rssFeeds){
348+
commands[cmd] = {
349+
usage: "[count]",
350+
description: rssFeeds[cmd].description,
351+
url: rssFeeds[cmd].url,
352+
process: function(bot,msg,suffix){
353+
var count = 1;
354+
if(suffix != null && suffix != "" && !isNaN(suffix)){
355+
count = suffix;
356+
}
357+
rssfeed(bot,msg,this.url,count,false);
358+
}
359+
};
360+
}
361+
}
362+
} catch(e) {
363+
console.log("Couldn't load rss.json. See rss.json.example if you want rss feed commands. error: " + e);
364+
}
338365

339-
function rssfeed(host,path,bot,msg,suffix){
340-
var http = require('http');
341-
http.get({
342-
host:host,
343-
path:path
344-
}, function(response) {
366+
function rssfeed(bot,msg,url,count,full){
367+
var FeedParser = require('feedparser');
368+
var feedparser = new FeedParser();
369+
var request = require('request');
370+
request(url).pipe(feedparser);
371+
feedparser.on('error', function(error){
372+
bot.sendMessage(msg.channel,"failed reading feed: " + error);
373+
});
374+
var shown = 0;
375+
feedparser.on('readable',function() {
345376
var stream = this;
346-
var FeedParser = require('feedparser');
347-
var feedparser = new FeedParser();
348-
response.pipe(feedparser);
349-
feedparser.on('error', function(error){
350-
bot.sendMessage(msg.channel,"failed reading feed: " + error);
351-
});
352-
feedparser.on('readable',function() {
353-
var stream = this;
354-
if(stream.alreadyRead){
355-
return;
377+
shown += 1
378+
if(shown > count){
379+
return;
380+
}
381+
var item = stream.read();
382+
bot.sendMessage(msg.channel,item.title + " - " + item.link, function() {
383+
if(full === true){
384+
var text = htmlToText.fromString(item.description,{
385+
wordwrap:false,
386+
ignoreHref:true
387+
});
388+
bot.sendMessage(msg.channel,text);
356389
}
357-
var item = stream.read();
358-
bot.sendMessage(msg.channel,item.title + " - " + item.link, function() {
359-
if(suffix === "full"){
360-
var text = htmlToText.fromString(item.description,{
361-
wordwrap:false,
362-
ignoreHref:true
363-
});
364-
bot.sendMessage(msg.channel,text);
365-
}
366-
});
367-
stream.alreadyRead = true;
368390
});
391+
stream.alreadyRead = true;
369392
});
370393
}
371394

372395

373396
var bot = new Discord.Client();
374397

375398
bot.on("ready", function () {
399+
loadFeeds();
376400
console.log("Ready to begin! Serving in " + bot.channels.length + " channels");
377401
});
378402

@@ -385,7 +409,8 @@ bot.on("disconnected", function () {
385409

386410
bot.on("message", function (msg) {
387411
//check if message is a command
388-
if(msg.author != bot.user && (msg.content[0] === '!' || msg.content.indexOf(bot.user.mention()) == 0)){
412+
if(msg.author.id != bot.user.id && (msg.content[0] === '!' || msg.content.indexOf(bot.user.mention()) == 0)){
413+
console.log("treating " + msg.content + " from " + msg.author + " as command");
389414
var cmdTxt = msg.content.split(" ")[0].substring(1);
390415
var suffix = msg.content.substring(cmdTxt.length+2);//add one for the ! and one for the space
391416
if(msg.content.indexOf(bot.user.mention()) == 0){

rss.json.example

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"hotspatch": {
3+
"description" : "gets the latest patch notes for Heroes of the Storm from blizzpro",
4+
"url" : "http://heroesofthestorm.blizzpro.com/tag/patch-notes/feed/"
5+
},
6+
"slashdot": {
7+
"description": "see the top articles on slashdot",
8+
"url": "http://rss.slashdot.org/Slashdot/slashdotMain"
9+
},
10+
"gnews": {
11+
"description": "see top news articles on Google News",
12+
"url": "http://news.google.com/news?cf=all&hl=en&pz=1&ned=us&topic=h&num=3&output=rss"
13+
}
14+
}

0 commit comments

Comments
 (0)