Browse Source

Merged feature/user-tagging into develop

shorten
benji7425 4 years ago
parent
commit
9549ff8f24
  1. 1
      .gitignore
  2. 4
      config.json
  3. 56
      feed-bot.js
  4. 1
      package.json

1
.gitignore

@ -1,4 +1,5 @@
log
subscribers.json
# Created by https://www.gitignore.io/api/visualstudiocode,node

4
config.json

@ -4,5 +4,7 @@
"pollingInterval": 5000,
"numLinksToCache": 10,
"youtubeMode": true,
"logRequestMessage": "!logsplease"
"logRequestMessage": "!logsplease",
"subscribeRequestMessage": "!subscribe",
"unsubscribeRequestMessage": "!unsubscribe"
}

56
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 Discord = require("discord.io"); //for obvious reasons
var FeedRead = require("feed-read"); //for rss feed reading
var JsonFile = require("jsonfile"); //reading/writing json
//my imports
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
},
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 && 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
Uri.withinString(message, function (url) {
Links.cache(Links.standardise(url));
return url;
});
if (channelID === Config.channelID) {
//contains a link, and is not the latest link from the rss feed
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
Uri.withinString(message, function (url) {
Links.cache(Links.standardise(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) {
DiscordClient.bot.uploadFile({
to: channelID,
file: "./log"
}, (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 + ")");
});
}
@ -97,10 +106,14 @@ var DiscordClient = {
});
},
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
DiscordClient.bot.sendMessage({
to: Config.channelID,
message: link
message: tags + link
}, function (err, message) {
if (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 = {
url: {
share: "http://youtu.be/",
@ -200,6 +233,7 @@ var intervalFunc = () => { }; //do nothing by default
//IIFE to kickstart the bot when the app loads
(function () {
Subscriptions.parse();
DiscordClient.startup();
setInterval(() => { intervalFunc(); }, Config.pollingInterval);
})();

1
package.json

@ -20,6 +20,7 @@
"discord.io": "^2.2.4",
"feed-read": "0.0.1",
"simple-file-writer": "^2.0.0",
"jsonfile": "^2.4.0",
"urijs": "^1.18.2"
}
}
Loading…
Cancel
Save