Merged release/v1.0.0 into master

This commit is contained in:
benji7425 2016-10-30 21:34:06 +00:00
commit 282db02695
7 changed files with 275 additions and 1 deletions

32
.eslintrc.json Normal file
View File

@ -0,0 +1,32 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"windows"
],
"quotes": [
"warn",
"double"
],
"semi": [
"error",
"always"
],
"no-undef": "warn",
"no-unused-vars": "warn"
}
}

57
.gitignore vendored Normal file
View File

@ -0,0 +1,57 @@
# Created by https://www.gitignore.io/api/visualstudiocode,node
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
### Node ###
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
/botConfig.json

46
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,46 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}\\feed-bot.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outFiles": []
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": false,
"outFiles": []
}
]
}

View File

@ -1 +1,26 @@
discord-feed-bot
# Features
- Posts latest link from RSS feed into specified Discord channel
- Doesn't post the link if it has already been posted in last 100 messages
- Configurable polling interval
# Planned features
- Add checking for >100 messages (currently if 100 messages are sent after posting the link, it will be re-posted straight away because it wont be detected in the previous 100)
- Add checking for link within other messages (currently only checks for messages identical to the link)
- Addition of user-defined URLs to match as 'sent' (ie if a user posts a youtu.be link, the bot will still post a youtube.com link, even if they point to the same palce - I would like to add a setting whereby you can specify alternate hosts to match)
Feel free to contact me with suggestions and feature requests - if you need a new feature, just let me know and I will see what I can do! (No promises though :p)
# Installation
1. Make sure you have nodejs (v6+) and npm installed
2. Clone repo or download zip and extract somewhere
3. Open a terminal in cloned/extracted folder
4. Run `npm install` and wait for it to finish
5. Edit *config.json* to include your RSS feed and channel ID
6. Create *botConfig.json* to include your bot token:
`{
"token": "abc123blahblahblahyourtokengoeshere"
}`
7. Run `node feed-bot.js`

5
config.json Normal file
View File

@ -0,0 +1,5 @@
{
"feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab",
"channelID": "241238530376990722",
"pollingInterval": 5000
}

86
feed-bot.js Normal file
View File

@ -0,0 +1,86 @@
var console = require("console");
var Dns = require("dns");
var Url = require("url");
var Discord = require("discord.io");
var FeedRead = require("feed-read");
var BotConfig = require("./botConfig.json");
var Config = require("./config.json");
//get a URL object from the feedUrl so we can examine it and check connectivity later
var url = Url.parse(Config.feedUrl);
//placeholder for our bot - we need to check for connectivity before assigning this though
var bot;
//check if we can connect to discordapp.com to authenticate the bot
Dns.resolve("discordapp.com", function (err) {
if (err) console.log("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot (you are probably not connected to the internet). Details: " + (err.message || err));
else {
//if there was no error, go ahead and create and authenticate the bot
bot = new Discord.Client({
token: BotConfig.token,
autorun: true
});
//when the bot is ready, set a polling interval for the rss feed
bot.on("ready", function () {
console.log(bot.username + " - (" + bot.id + ")");
setInterval(checkFeedAndPost, Config.pollingInterval);
});
//easy way to check if the bot is active - replies "pong" when you type "ping" in discord
bot.on("message", function (user, userID, channelID, message) {
if (message === "ping") {
bot.sendMessage({
to: channelID,
message: "pong"
});
}
});
}
});
function checkFeedAndPost() {
//check that we have an internet connection (well not exactly - check that we have a connection to the host of the feedUrl)
Dns.resolve(url.host, function (err) {
if (err) console.log("CONNECTION ERROR: Cannot resolve host (you are probably not connected to the internet). Details: " + (err.message || err));
else {
//check the feed asynchronously, check the latest link when done
FeedRead(Config.feedUrl, function (err, articles) {
try {
checkLinkAndPost(err, articles);
}
catch (ex) {
//checkFeedAndPost is async due to being called by setInterval so the console log has to be here
console.log("FEED ERROR: " + (ex.message || ex));
}
});
}
});
}
//checks if the link has been posted previously, posts if not
function checkLinkAndPost(err, articles) {
if (err) throw "Error reading RSS feed: " + (err.message || err);
var latestLink = articles[0].link;
//get the latest 100 messages (100 is the limit)
bot.getMessages({
channelID: Config.channelID,
limit: 100
}, function (err, messages) {
if (err) throw err;
//get an array of strings from the array of message objects
var messageContents = messages.map((message) => { return message.content; });
//if the messageContents array doesn't include the latest link, post it
if (!messageContents.includes(latestLink))
bot.sendMessage({
to: Config.channelID,
message: latestLink
});
});
}

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "discord-feed-bot",
"version": "1.0.0",
"description": "discord-feed-bot",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/benji7425/discord-feed-bot.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/benji7425/discord-feed-bot/issues"
},
"homepage": "https://github.com/benji7425/discord-feed-bot#readme",
"dependencies": {
"discord.io": "^2.2.4",
"feed-read": "0.0.1"
}
}