Merge branch 'v2'
Also updated changelog with v2-b1 changes
This commit is contained in:
commit
0d87676ee0
|
@ -12,7 +12,8 @@
|
||||||
"rules": {
|
"rules": {
|
||||||
"indent": [
|
"indent": [
|
||||||
"warn",
|
"warn",
|
||||||
"tab"
|
"tab",
|
||||||
|
{ "SwitchCase": 1}
|
||||||
],
|
],
|
||||||
"quotes": [
|
"quotes": [
|
||||||
"warn",
|
"warn",
|
||||||
|
@ -26,4 +27,4 @@
|
||||||
"no-unused-vars": "warn",
|
"no-unused-vars": "warn",
|
||||||
"eqeqeq": ["error", "always"]
|
"eqeqeq": ["error", "always"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
21
.gitignore
vendored
21
.gitignore
vendored
|
@ -1,14 +1,7 @@
|
||||||
|
### Discord bots ####
|
||||||
|
guilds.json
|
||||||
|
token.json
|
||||||
log
|
log
|
||||||
subscribers.json
|
|
||||||
|
|
||||||
# Created by https://www.gitignore.io/api/visualstudiocode,node
|
|
||||||
|
|
||||||
### VisualStudioCode ###
|
|
||||||
.vscode/*
|
|
||||||
!.vscode/settings.json
|
|
||||||
!.vscode/tasks.json
|
|
||||||
!.vscode/launch.json
|
|
||||||
|
|
||||||
|
|
||||||
### Node ###
|
### Node ###
|
||||||
# Logs
|
# Logs
|
||||||
|
@ -42,6 +35,7 @@ build/Release
|
||||||
|
|
||||||
# Dependency directories
|
# Dependency directories
|
||||||
node_modules
|
node_modules
|
||||||
|
node_cache
|
||||||
jspm_packages
|
jspm_packages
|
||||||
|
|
||||||
# Optional npm cache directory
|
# Optional npm cache directory
|
||||||
|
@ -56,5 +50,8 @@ jspm_packages
|
||||||
# Output of 'npm pack'
|
# Output of 'npm pack'
|
||||||
*.tgz
|
*.tgz
|
||||||
|
|
||||||
/botConfig.json
|
# Yarn Integrity file
|
||||||
/bot-config.json
|
.yarn-integrity
|
||||||
|
|
||||||
|
|
||||||
|
# End of https://www.gitignore.io/api/node
|
56
.vscode/launch.json
vendored
56
.vscode/launch.json
vendored
|
@ -1,46 +1,14 @@
|
||||||
{
|
{
|
||||||
"version": "0.2.0",
|
// Use IntelliSense to learn about possible Node.js debug attributes.
|
||||||
"configurations": [
|
// Hover to view descriptions of existing attributes.
|
||||||
{
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"name": "Launch",
|
"version": "0.2.0",
|
||||||
"type": "node",
|
"configurations": [
|
||||||
"request": "launch",
|
{
|
||||||
"program": "${workspaceRoot}\\wrapper\\index.js",
|
"type": "node",
|
||||||
"stopOnEntry": false,
|
"request": "launch",
|
||||||
"args": [],
|
"name": "Launch Program",
|
||||||
"cwd": "${workspaceRoot}",
|
"program": "${workspaceRoot}/wrapper.js"
|
||||||
"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": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
13
CHANGELOG.md
13
CHANGELOG.md
|
@ -1,5 +1,18 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v2.0.0-b1
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Multi-guild support
|
||||||
|
- In-chat commands for setup and configuration
|
||||||
|
- Add a new feed
|
||||||
|
- View a list of feeds
|
||||||
|
- Remove an existing feed
|
||||||
|
|
||||||
|
### Updated
|
||||||
|
- Now uses discord.js instead of discord.io
|
||||||
|
- YouTube links automatically handled; no more separate "YouTube mode" config item
|
||||||
|
|
||||||
## v1.4.0
|
## v1.4.0
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -1,22 +1,11 @@
|
||||||
{
|
{
|
||||||
"feeds": [
|
"saveIntervalSec": 60,
|
||||||
{ "url": "https://www.youtube.com/feeds/videos.xml?user=CorridorDigital", "roleID": "272788856447959040" },
|
"feedCheckIntervalSec": 30,
|
||||||
{ "url": "https://www.youtube.com/feeds/videos.xml?user=samandniko" },
|
"maxCacheSize": 10,
|
||||||
{ "url": "https://www.youtube.com/feeds/videos.xml?user=Node", "roleID": "306212762504134667" }
|
"commands": {
|
||||||
],
|
"version": "version",
|
||||||
"channelID": "264420391282409473",
|
"addFeed": "add-feed",
|
||||||
"pollingInterval": 5000,
|
"removeFeed": "remove-feed",
|
||||||
"numLinksToCache": 10,
|
"viewFeeds": "view-feeds"
|
||||||
"messageDeleteDelay": 10000,
|
}
|
||||||
"youtubeMode": true,
|
|
||||||
"allowSubscriptions": true,
|
|
||||||
"userCommands": {
|
|
||||||
"help": "!help"
|
|
||||||
},
|
|
||||||
"developerCommands": {
|
|
||||||
"cacheList": "!cached"
|
|
||||||
},
|
|
||||||
"developers": [
|
|
||||||
"117966411548196870"
|
|
||||||
]
|
|
||||||
}
|
}
|
308
app/index.js
308
app/index.js
|
@ -1,208 +1,152 @@
|
||||||
//external library imports
|
//node imports
|
||||||
var Dns = require("dns"); //for connectivity checking
|
const FileSystem = require("fs");
|
||||||
var Url = require("url"); //for url parsing
|
|
||||||
var Uri = require("urijs"); //for finding urls within message strings
|
|
||||||
var FeedRead = require("feed-read"); //for rss feed reading
|
|
||||||
var Console = require("console");
|
|
||||||
|
|
||||||
var config;
|
//external lib imports
|
||||||
|
const JsonFile = require("jsonfile"); //for saving to/from JSON
|
||||||
|
const Url = require("url"); //for url parsing
|
||||||
|
const GetUrls = require("get-urls"); //for extracting urls from messages
|
||||||
|
|
||||||
module.exports = (_config) => {
|
//my imports
|
||||||
config = _config || require("./config.json");
|
const DiscordUtil = require("discordjs-util");
|
||||||
|
|
||||||
this.onReady = (bot) => {
|
//app component imports
|
||||||
Actions.checkPastMessagesForLinks(bot); //we need to check past messages for links on startup, but also on reconnect because we don't know what has happened during the downtime
|
const GuildData = require("./models/guild-data.js");
|
||||||
|
const FeedData = require("./models/feed-data.js");
|
||||||
|
|
||||||
//set the interval function to check the feed
|
//global vars
|
||||||
intervalFunc = () => {
|
const SAVE_FILE = "./guilds.json";
|
||||||
var callback = (err, articles, feed) => Links.validate(err, articles, (latestLink) => Actions.post(bot, latestLink, feed.roleID));
|
|
||||||
|
|
||||||
Feed.checkFeeds(config.feeds, callback);
|
module.exports = (client) => {
|
||||||
};
|
const config = require("./config.json");
|
||||||
|
|
||||||
setInterval(() => { intervalFunc(); }, config.pollingInterval);
|
const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {};
|
||||||
};
|
setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000);
|
||||||
|
|
||||||
this.onMessage = (bot, user, userID, channelID, message) => {
|
parseLinksInGuilds(client.guilds, guildsData).then(() => writeFile(guildsData))
|
||||||
//contains a link, and is not the latest link from the rss feed
|
.then(() => checkFeedsInGuilds(client.guilds, guildsData))
|
||||||
if (channelID === config.channelID && Links.messageContainsLink(message) && (message !== Links.latestFromFeedlatestFeedLink)) {
|
.then(() => setInterval(() => checkFeedsInGuilds(client.guilds, guildsData), config.feedCheckIntervalSec * 1000)); //set up an interval to check all the feeds
|
||||||
Console.info("Detected posted link in this message: " + message, "Discord.io");
|
|
||||||
|
|
||||||
//extract the url from the string, and cache it
|
//set up an on message handler to detect when links are posted
|
||||||
Uri.withinString(message, function (url) {
|
client.on("message", message => {
|
||||||
Links.cache(Links.standardise(url));
|
if (message.author.id !== client.user.id) { //check the bot isn't triggering itself
|
||||||
return url;
|
if (message.channel.type === "dm")
|
||||||
|
HandleMessage.dm(client, config, message);
|
||||||
|
else if (message.channel.type === "text" && message.member)
|
||||||
|
HandleMessage.text(client, config, message, guildsData);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const HandleMessage = {
|
||||||
|
dm: (client, config, message) => {
|
||||||
|
message.reply("This bot does not have any handling for direct messages. To learn more or get help please visit http://benji7425.github.io, or join my Discord server here: https://discord.gg/SSkbwSJ");
|
||||||
|
},
|
||||||
|
text: (client, config, message, guildsData) => {
|
||||||
|
//handle admins invoking commands
|
||||||
|
if (message.content.startsWith(message.guild.me.toString()) //user is @mention-ing the bot
|
||||||
|
&& message.member.permissions.has("ADMINISTRATOR")) //user has admin perms
|
||||||
|
{
|
||||||
|
const params = message.content.split(" "); //split the message at the spaces
|
||||||
|
switch (params[1]) {
|
||||||
|
//add handling for different commands here
|
||||||
|
case config.commands.version:
|
||||||
|
message.reply("v" + require("../package.json").version);
|
||||||
|
break;
|
||||||
|
case config.commands.addFeed:
|
||||||
|
addFeed(client, guildsData, message, config.maxCacheSize);
|
||||||
|
break;
|
||||||
|
case config.commands.removeFeed:
|
||||||
|
removeFeed(client, guildsData, message);
|
||||||
|
break;
|
||||||
|
case config.commands.viewFeeds:
|
||||||
|
viewFeeds(client, guildsData[message.guild.id], message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (guildsData[message.guild.id]) {
|
||||||
|
guildsData[message.guild.id].feeds.forEach(feedData => {
|
||||||
|
if (message.channel.name === feedData.channelName)
|
||||||
|
feedData.cachedLinks.push(...GetUrls(message.content)); //spread the urlSet returned by GetUrls into the cache array
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
|
||||||
|
|
||||||
this.commands = [
|
|
||||||
{
|
|
||||||
command: config.userCommands.help,
|
|
||||||
type: "equals",
|
|
||||||
action: (bot, user, userID, channelID, message) => {
|
|
||||||
bot.sendMessage({
|
|
||||||
to: config.channelID,
|
|
||||||
message: "Available commands: " + getValues(config.userCommands).join(", ")
|
|
||||||
});
|
|
||||||
},
|
|
||||||
channelIDs: [config.channelID]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
command: config.developerCommands.logUpload,
|
|
||||||
type: "equals",
|
|
||||||
action: (bot, user, userID, channelID, message) => {
|
|
||||||
bot.uploadFile({
|
|
||||||
to: channelID,
|
|
||||||
file: config.logFile
|
|
||||||
});
|
|
||||||
},
|
|
||||||
userIDs: config.developers
|
|
||||||
},
|
|
||||||
{
|
|
||||||
command: config.developerCommands.cacheList,
|
|
||||||
type: "equals",
|
|
||||||
action: (bot, user, userID, channelID, message) => {
|
|
||||||
bot.sendMessage({
|
|
||||||
to: channelID,
|
|
||||||
message: Links.cached.join(", ")
|
|
||||||
});
|
|
||||||
},
|
|
||||||
userIDs: config.developers
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
return this;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var Actions = {
|
function addFeed(client, guildsData, message, maxCacheSize) {
|
||||||
post: (bot, link, roleID) => {
|
const feedUrl = [...GetUrls(message.content)][0];
|
||||||
//send a messsage containing the new feed link to our discord channel
|
const channel = message.mentions.channels.first();
|
||||||
bot.sendMessage({
|
|
||||||
to: config.channelID,
|
|
||||||
message: ((roleID !== "" && roleID !== undefined) ? "<@&" + roleID + ">" : "") + " " + link
|
|
||||||
});
|
|
||||||
},
|
|
||||||
checkPastMessagesForLinks: (bot) => {
|
|
||||||
var limit = 100;
|
|
||||||
Console.info("Attempting to check past " + limit + " messages for links");
|
|
||||||
|
|
||||||
//get the last however many messsages from our discord channel
|
if (!feedUrl || !channel)
|
||||||
bot.getMessages({
|
return message.reply("Please provide both a channel and an RSS feed URL. You can optionally @mention a role also.");
|
||||||
channelID: config.channelID,
|
|
||||||
limit: limit
|
|
||||||
}, function (err, messages) {
|
|
||||||
if (err) Console.error("Error fetching discord messages.", err);
|
|
||||||
else {
|
|
||||||
Console.info("Pulled last " + messages.length + " messages, scanning for links");
|
|
||||||
|
|
||||||
var messageContents = messages.map((x) => { return x.content; }).reverse(); //extract an array of strings from the array of message objects
|
const role = message.mentions.roles.first();
|
||||||
|
|
||||||
for (var messageIdx in messageContents) {
|
const feedData = new FeedData({
|
||||||
var message = messageContents[messageIdx];
|
url: feedUrl,
|
||||||
|
channelName: channel.name,
|
||||||
|
roleName: role ? role.name : null,
|
||||||
|
maxCacheSize: maxCacheSize
|
||||||
|
});
|
||||||
|
|
||||||
if (Links.messageContainsLink(message)) //test if the message contains a url
|
//ask the user if they're happy with the details they set up, save if yes, don't if no
|
||||||
//detect the url inside the string, and cache it
|
DiscordUtil.ask(client, message.channel, message.member, "Are you happy with this?\n" + feedData.toString())
|
||||||
Uri.withinString(message, function (url) {
|
.then(responseMessage => {
|
||||||
Links.cache(url);
|
|
||||||
return url;
|
//if they responded yes, save the feed and let them know, else tell them to start again
|
||||||
});
|
if (responseMessage.content.toLowerCase() === "yes") {
|
||||||
}
|
if (!guildsData[message.guild.id])
|
||||||
|
guildsData[message.guild.id] = new GuildData({ id: message.guild.id, feeds: [] });
|
||||||
|
|
||||||
|
guildsData[message.guild.id].feeds.push(feedData);
|
||||||
|
writeFile(guildsData);
|
||||||
|
responseMessage.reply("Your new feed has been saved!");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
responseMessage.reply("Your feed has not been saved, please add it again with the correct details");
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
};
|
|
||||||
|
|
||||||
var YouTube = {
|
function removeFeed(client, guildsData, message) {
|
||||||
url: {
|
const parameters = message.content.split(" ");
|
||||||
share: "http://youtu.be/",
|
if (parameters.length !== 3)
|
||||||
full: "http://www.youtube.com/watch?v=",
|
message.reply(`Please use the command as such:\n\`\`\` @${client.user.username} remove-feed feedid\`\`\``);
|
||||||
createFullUrl: function (shareUrl) {
|
else {
|
||||||
return shareUrl.replace(YouTube.url.share, YouTube.url.full);
|
const guildData = guildsData[message.guild.id];
|
||||||
},
|
const idx = guildData.feeds.findIndex(feed => feed.id === parameters[2]);
|
||||||
createShareUrl: function (fullUrl) {
|
if (!Number.isInteger(idx))
|
||||||
var shareUrl = fullUrl.replace(YouTube.url.full, YouTube.url.share);
|
message.reply("Can't find feed with id " + parameters[2]);
|
||||||
|
|
||||||
if (shareUrl.includes("&")) shareUrl = shareUrl.slice(0, fullUrl.indexOf("&"));
|
|
||||||
|
|
||||||
return shareUrl;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
var Links = {
|
|
||||||
standardise: function (link) {
|
|
||||||
link = link.replace("https://", "http://"); //cheaty way to get around http and https not matching
|
|
||||||
if (config.youtubeMode) link = link.split("&")[0]; //quick way to chop off stuff like &feature=youtube etc
|
|
||||||
return link;
|
|
||||||
},
|
|
||||||
messageContainsLink: function (message) {
|
|
||||||
var messageLower = message.toLowerCase();
|
|
||||||
return messageLower.includes("http://") || messageLower.includes("https://") || messageLower.includes("www.");
|
|
||||||
},
|
|
||||||
cached: [],
|
|
||||||
latestFeedLink: "",
|
|
||||||
cache: function (link) {
|
|
||||||
link = Links.standardise(link);
|
|
||||||
|
|
||||||
if (config.youtubeMode) link = YouTube.url.createShareUrl(link);
|
|
||||||
|
|
||||||
//store the new link if not stored already
|
|
||||||
if (!Links.isCached(link)) {
|
|
||||||
Links.cached.push(link);
|
|
||||||
Console.info("Cached URL: " + link);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Links.cached.length > config.numLinksToCache) Links.cached.shift(); //get rid of the first array element if we have reached our cache limit
|
|
||||||
},
|
|
||||||
isCached: function (link) {
|
|
||||||
link = Links.standardise(link);
|
|
||||||
|
|
||||||
if (config.youtubeMode)
|
|
||||||
return Links.cached.includes(YouTube.url.createShareUrl(link));
|
|
||||||
|
|
||||||
return Links.cached.includes(link);
|
|
||||||
},
|
|
||||||
validate: function (err, articles, callback) {
|
|
||||||
if (err) Console.error("FEED ERROR: Error reading RSS feed.", err);
|
|
||||||
else {
|
else {
|
||||||
var latestLink = Links.standardise(articles[0].link);
|
guildData.feeds.splice(idx, 1);
|
||||||
if (config.youtubeMode) latestLink = YouTube.url.createShareUrl(latestLink);
|
writeFile(guildsData);
|
||||||
|
message.reply("Feed removed!");
|
||||||
//make sure we don't spam the latest link
|
|
||||||
if (latestLink === Links.latestFeedLink)
|
|
||||||
return;
|
|
||||||
|
|
||||||
//make sure the latest link hasn't been posted already
|
|
||||||
if (!Links.isCached(latestLink)) {
|
|
||||||
callback(latestLink);
|
|
||||||
|
|
||||||
Links.cache(latestLink); //make sure the link is cached, so it doesn't get posted again
|
|
||||||
}
|
|
||||||
|
|
||||||
Links.latestFeedLink = latestLink; //ensure our latest feed link variable is up to date, so we can track when the feed updates
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
var Feed = {
|
function viewFeeds(client, guildData, message) {
|
||||||
checkFeeds: function (feeds, individualCallback) {
|
message.reply(guildData.feeds.map(f => f.toString()).join("\n"));
|
||||||
feeds.forEach((feed) => {
|
}
|
||||||
Dns.resolve(Url.parse(feed.url).host, (err) => {
|
|
||||||
if (err) Console.error("CONNECTION ERROR: Cannot resolve host.", err);
|
function checkFeedsInGuilds(guilds, guildsData) {
|
||||||
else FeedRead(feed.url, (err, articles) => individualCallback(err, articles, feed));
|
Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(guilds));
|
||||||
});
|
}
|
||||||
});
|
|
||||||
|
function parseLinksInGuilds(guilds, guildsData) {
|
||||||
|
const promises = [];
|
||||||
|
for (let guildId of guilds.keys()) {
|
||||||
|
const guildData = guildsData[guildId];
|
||||||
|
if (guildData)
|
||||||
|
promises.push(guildData.cachePastPostedLinks(guilds.get(guildId)));
|
||||||
}
|
}
|
||||||
};
|
return Promise.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
var getValues = function (obj) {
|
function writeFile(guildsData) {
|
||||||
var values = [];
|
JsonFile.writeFile(SAVE_FILE, guildsData, err => { if (err) DiscordUtil.dateError("Error writing file", err); });
|
||||||
for (var value in obj)
|
}
|
||||||
if (obj.hasOwnProperty(value))
|
|
||||||
values.push(obj[value]);
|
|
||||||
return values;
|
|
||||||
};
|
|
||||||
|
|
||||||
var intervalFunc = () => { }; //do nothing by default
|
function fromJSON(json) {
|
||||||
|
const guildsData = Object.keys(json);
|
||||||
|
guildsData.forEach(guildID => { json[guildID] = new GuildData(json[guildID]); });
|
||||||
|
return json;
|
||||||
|
}
|
89
app/models/feed-data.js
Normal file
89
app/models/feed-data.js
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
//my imports
|
||||||
|
const DiscordUtil = require("discordjs-util");
|
||||||
|
|
||||||
|
//external lib imports
|
||||||
|
const Dns = require("dns"); //for host resolution checking
|
||||||
|
const Url = require("url"); //for url parsing
|
||||||
|
const FeedRead = require("feed-read"); //for extracing new links from RSS feeds
|
||||||
|
const GetUrls = require("get-urls"); //for extracting urls from messages
|
||||||
|
const ShortID = require("shortid"); //to provide ids for each feed, allowing guilds to remove them
|
||||||
|
|
||||||
|
module.exports = class FeedData {
|
||||||
|
constructor({ id, url, channelName, roleName, cachedLinks, maxCacheSize }) {
|
||||||
|
this.id = id || ShortID.generate();
|
||||||
|
this.url = url;
|
||||||
|
this.channelName = channelName;
|
||||||
|
this.roleName = roleName;
|
||||||
|
this.cachedLinks = cachedLinks || [];
|
||||||
|
this.maxCacheSize = maxCacheSize || 10;
|
||||||
|
|
||||||
|
this.cachedLinks.push = (...elements) => {
|
||||||
|
const unique = elements
|
||||||
|
.map(el => normaliseUrl(el)) //normalise all the urls
|
||||||
|
.filter(el => !this.cachedLinks.includes(el)); //filter out any already cached
|
||||||
|
Array.prototype.push.apply(this.cachedLinks, unique);
|
||||||
|
|
||||||
|
if (this.cachedLinks.length > this.maxCacheSize)
|
||||||
|
this.cachedLinks.splice(0, this.cachedLinks.length - this.maxCacheSize); //remove the # of elements above the max from the beginning
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a promise providing all the links posted in the last 100 messages
|
||||||
|
* @param {Discord.Guild} guild The guild this feed belongs to
|
||||||
|
* @returns {Promise<string[]>} Links posted in last 100 messages
|
||||||
|
*/
|
||||||
|
updatePastPostedLinks(guild) {
|
||||||
|
const channel = guild.channels.find(ch => ch.type === "text" && ch.name === this.channelName);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
channel.fetchMessages({ limit: 100 })
|
||||||
|
.then(messages => {
|
||||||
|
new Map([...messages].reverse()).forEach(m => this.cachedLinks.push(...GetUrls(m.content))); //push all the links in each message into our links array
|
||||||
|
resolve(this);
|
||||||
|
})
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
check(guild) {
|
||||||
|
Dns.resolve(Url.parse(this.url).host || "", err => { //check we can resolve the host, so we can throw an appropriate error if it fails
|
||||||
|
if (err)
|
||||||
|
DiscordUtil.dateError("Connection Error: Can't resolve host", err); //log our error if we can't resolve the host
|
||||||
|
else
|
||||||
|
FeedRead(this.url, (err, articles) => { //check the feed
|
||||||
|
if (err)
|
||||||
|
DiscordUtil.dateError(err);
|
||||||
|
else {
|
||||||
|
let latest = articles[0].link; //extract the latest link
|
||||||
|
latest = normaliseUrl(latest); //standardise it a bit
|
||||||
|
|
||||||
|
//if we don't have it cached already, cache it and callback
|
||||||
|
if (!this.cachedLinks.includes(latest)) {
|
||||||
|
this.cachedLinks.push(latest);
|
||||||
|
|
||||||
|
const channel = guild.channels.find(ch => ch.type === "text" && ch.name.toLowerCase() === this.channelName.toLowerCase());
|
||||||
|
const role = this.roleName ? guild.roles.find(role => role.name.toLowerCase() === this.roleName.toLowerCase()) : null;
|
||||||
|
channel.send((role ? role + " " : "") + latest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
const blacklist = ["cachedLinks", "maxCacheSize"];
|
||||||
|
return `\`\`\`JavaScript\n ${JSON.stringify(this, (k, v) => !blacklist.includes(k) ? v : undefined, "\t")} \`\`\``;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function normaliseUrl(url) {
|
||||||
|
url = url.replace("https://", "http://"); //cheaty way to get around http and https not matching
|
||||||
|
|
||||||
|
if (Url.parse(url).host.includes("youtu")) //detect youtu.be and youtube.com - yes I know it's hacky
|
||||||
|
url = url.split("&")[0]; //quick way to chop off stuff like &feature=youtube
|
||||||
|
|
||||||
|
url = url.replace(/(www.)?youtube.com\/watch\?v=/, "youtu.be/"); //turn full url into share url
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
23
app/models/guild-data.js
Normal file
23
app/models/guild-data.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
const FeedData = require("./feed-data.js");
|
||||||
|
const Util = require("discordjs-util");
|
||||||
|
|
||||||
|
module.exports = class GuildData {
|
||||||
|
constructor({ id, feeds }) {
|
||||||
|
this.id = id;
|
||||||
|
this.feeds = feeds.map(feed => new FeedData(feed));
|
||||||
|
}
|
||||||
|
|
||||||
|
cachePastPostedLinks(guild) {
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
|
this.feeds.forEach(feed => {
|
||||||
|
promises.push(feed.updatePastPostedLinks(guild).catch(Util.dateError));
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
checkFeeds(guilds) {
|
||||||
|
this.feeds.forEach(feed => feed.check(guilds.get(this.id)));
|
||||||
|
}
|
||||||
|
};
|
24
package.json
24
package.json
|
@ -1,27 +1,29 @@
|
||||||
{
|
{
|
||||||
"name": "discord-bot-feed-linker",
|
"name": "discord-bot-feed-linker",
|
||||||
"version": "1.4.0",
|
"version": "2.0.0",
|
||||||
"description": "discord-bot-feed-linker",
|
"description": "",
|
||||||
"main": "app/index.js",
|
"main": "app/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"start": "node wrapper.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/benji7425/discord-feed-bot.git"
|
"url": "git+https://github.com/benji7425/discord-bot-feed-linker.git"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/benji7425/discord-feed-bot/issues"
|
"url": "https://github.com/benji7425/discord-bot-feed-linker/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/benji7425/discord-feed-bot#readme",
|
"homepage": "https://github.com/benji7425/discord-bot-feed-linker#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord.io": "2.5.1",
|
"discord.js": "11.1.0",
|
||||||
|
"discordjs-util": "git+https://github.com/benji7425/discordjs-util.git",
|
||||||
|
"dns": "0.2.2",
|
||||||
"feed-read": "0.0.1",
|
"feed-read": "0.0.1",
|
||||||
"simple-file-writer": "^2.0.0",
|
"get-urls": "7.0.0",
|
||||||
"jsonfile": "^2.4.0",
|
"jsonfile": "3.0.1",
|
||||||
"urijs": "^1.18.2"
|
"shortid": "2.2.8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
wrapper.js
Normal file
20
wrapper.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
const Discord = require("discord.js");
|
||||||
|
const DiscordUtil = require("discordjs-util");
|
||||||
|
|
||||||
|
const client = new Discord.Client();
|
||||||
|
|
||||||
|
process.on("uncaughtException", (err) => {
|
||||||
|
DiscordUtil.dateError("Uncaught exception!", err);
|
||||||
|
});
|
||||||
|
|
||||||
|
client.login(require("./token.json").token);
|
||||||
|
|
||||||
|
client.on("ready", () => {
|
||||||
|
DiscordUtil.dateLog("Registered bot " + client.user.username);
|
||||||
|
require("./app/index.js")(client);
|
||||||
|
client.user.setGame("benji7425.github.io");
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("disconnect", eventData => {
|
||||||
|
DiscordUtil.dateError("Bot was disconnected!", eventData.code, eventData.reason);
|
||||||
|
});
|
Loading…
Reference in a new issue