Merged feature/user-tagging into develop
This commit is contained in:
commit
9549ff8f24
|
@ -1,4 +1,5 @@
|
||||||
log
|
log
|
||||||
|
subscribers.json
|
||||||
|
|
||||||
# Created by https://www.gitignore.io/api/visualstudiocode,node
|
# Created by https://www.gitignore.io/api/visualstudiocode,node
|
||||||
|
|
||||||
|
|
|
@ -4,5 +4,7 @@
|
||||||
"pollingInterval": 5000,
|
"pollingInterval": 5000,
|
||||||
"numLinksToCache": 10,
|
"numLinksToCache": 10,
|
||||||
"youtubeMode": true,
|
"youtubeMode": true,
|
||||||
"logRequestMessage": "!logsplease"
|
"logRequestMessage": "!logsplease",
|
||||||
|
"subscribeRequestMessage": "!subscribe",
|
||||||
|
"unsubscribeRequestMessage": "!unsubscribe"
|
||||||
}
|
}
|
54
feed-bot.js
54
feed-bot.js
|
@ -4,6 +4,7 @@ var Url = require("url"); //for url parsing
|
||||||
var Uri = require("urijs"); //for finding urls within message strings
|
var Uri = require("urijs"); //for finding urls within message strings
|
||||||
var Discord = require("discord.io"); //for obvious reasons
|
var Discord = require("discord.io"); //for obvious reasons
|
||||||
var FeedRead = require("feed-read"); //for rss feed reading
|
var FeedRead = require("feed-read"); //for rss feed reading
|
||||||
|
var JsonFile = require("jsonfile"); //reading/writing json
|
||||||
|
|
||||||
//my imports
|
//my imports
|
||||||
var Log = require("./log.js"); //some very simple logging functions I made
|
var Log = require("./log.js"); //some very simple logging functions I made
|
||||||
|
@ -48,22 +49,30 @@ var DiscordClient = {
|
||||||
intervalFunc = DiscordClient.startup; //reassign the interval function to try restart the bot every 5 sec
|
intervalFunc = DiscordClient.startup; //reassign the interval function to try restart the bot every 5 sec
|
||||||
},
|
},
|
||||||
onMessage: function (user, userID, channelID, message) {
|
onMessage: function (user, userID, channelID, message) {
|
||||||
//check if the message is in the right channel, contains a link, and is not the latest link from the rss feed
|
if (channelID === Config.channelID) {
|
||||||
if (channelID === Config.channelID && Links.messageContainsLink(message) && (message !== Links.latestFromFeedlatestFeedLink)) {
|
//contains a link, and is not the latest link from the rss feed
|
||||||
Log.event("Detected posted link in this message: " + message, "Discord.io");
|
if (Links.messageContainsLink(message) && (message !== Links.latestFromFeedlatestFeedLink)) {
|
||||||
|
Log.event("Detected posted link in this message: " + message, "Discord.io");
|
||||||
|
|
||||||
//extract the url from the string, and cache it
|
//extract the url from the string, and cache it
|
||||||
Uri.withinString(message, function (url) {
|
Uri.withinString(message, function (url) {
|
||||||
Links.cache(Links.standardise(url));
|
Links.cache(Links.standardise(url));
|
||||||
return url;
|
return url;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
else if (message == Config.subscribeRequestMessage) {
|
||||||
|
Subscriptions.subscribe(userID, user);
|
||||||
|
}
|
||||||
|
else if (message == Config.unsubscribeRequestMessage) {
|
||||||
|
Subscriptions.unsubscribe(userID, user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (message == Config.logRequestMessage) {
|
else if (message == Config.logRequestMessage) {
|
||||||
DiscordClient.bot.uploadFile({
|
DiscordClient.bot.uploadFile({
|
||||||
to: channelID,
|
to: channelID,
|
||||||
file: "./log"
|
file: "./log"
|
||||||
}, (err, message) => {
|
}, (err, message) => {
|
||||||
if(err) Log.error("Failed to upload log file: " + message, err);
|
if (err) Log.error("Failed to upload log file: " + message, err);
|
||||||
else Log.event("Uploaded log file for user " + user + "(" + userID + ")");
|
else Log.event("Uploaded log file for user " + user + "(" + userID + ")");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -97,10 +106,14 @@ var DiscordClient = {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
post: function (link) {
|
post: function (link) {
|
||||||
|
var tags = "";
|
||||||
|
for (var userID in Subscriptions.subscribers)
|
||||||
|
tags += "<@" + Subscriptions.subscribers[userID] + "> ";
|
||||||
|
|
||||||
//send a messsage containing the new feed link to our discord channel
|
//send a messsage containing the new feed link to our discord channel
|
||||||
DiscordClient.bot.sendMessage({
|
DiscordClient.bot.sendMessage({
|
||||||
to: Config.channelID,
|
to: Config.channelID,
|
||||||
message: link
|
message: tags + link
|
||||||
}, function (err, message) {
|
}, function (err, message) {
|
||||||
if (err) {
|
if (err) {
|
||||||
Log.error("ERROR: Failed to send message: " + message.substring(0, 15) + "...", err);
|
Log.error("ERROR: Failed to send message: " + message.substring(0, 15) + "...", err);
|
||||||
|
@ -111,6 +124,26 @@ var DiscordClient = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var Subscriptions = {
|
||||||
|
subscribers: [],
|
||||||
|
parse: function () {
|
||||||
|
JsonFile.readFile("./subscribers.json", (err, obj) => {
|
||||||
|
if (err) Log.error("Unable to parse json subscribers file", err);
|
||||||
|
this.subscribers = obj || [];
|
||||||
|
});
|
||||||
|
},
|
||||||
|
subscribe: function (userID, user) {
|
||||||
|
this.subscribers.push(userID);
|
||||||
|
JsonFile.writeFile("./subscribers.json", this.subscribers, (err) => { if (err) Log.error("Unable to write subscribers to json file", err); });
|
||||||
|
Log.event("Subscribed user " + (user ? user + "(" + userID + ")" : userID));
|
||||||
|
},
|
||||||
|
unsubscribe: function (userID, user) {
|
||||||
|
this.subscribers.splice(this.subscribers.indexOf(userID));
|
||||||
|
JsonFile.writeFile("./subscribers.json", this.subscribers, (err) => { if (err) Log.error("Unable to write subscribers to json file", err); });
|
||||||
|
Log.event("Unsubscribed user " + (user ? user + "(" + userID + ")" : userID));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var YouTube = {
|
var YouTube = {
|
||||||
url: {
|
url: {
|
||||||
share: "http://youtu.be/",
|
share: "http://youtu.be/",
|
||||||
|
@ -200,6 +233,7 @@ var intervalFunc = () => { }; //do nothing by default
|
||||||
|
|
||||||
//IIFE to kickstart the bot when the app loads
|
//IIFE to kickstart the bot when the app loads
|
||||||
(function () {
|
(function () {
|
||||||
|
Subscriptions.parse();
|
||||||
DiscordClient.startup();
|
DiscordClient.startup();
|
||||||
setInterval(() => { intervalFunc(); }, Config.pollingInterval);
|
setInterval(() => { intervalFunc(); }, Config.pollingInterval);
|
||||||
})();
|
})();
|
|
@ -20,6 +20,7 @@
|
||||||
"discord.io": "^2.2.4",
|
"discord.io": "^2.2.4",
|
||||||
"feed-read": "0.0.1",
|
"feed-read": "0.0.1",
|
||||||
"simple-file-writer": "^2.0.0",
|
"simple-file-writer": "^2.0.0",
|
||||||
|
"jsonfile": "^2.4.0",
|
||||||
"urijs": "^1.18.2"
|
"urijs": "^1.18.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue