Browse Source

Add view-feeds command

shorten
benji7425 4 years ago
parent
commit
d0d148dbca
  1. 3
      app/config.json
  2. 18
      app/index.js
  3. 7
      app/models/feed-data.js

3
app/config.json

@ -4,6 +4,7 @@
"maxCacheSize": 10,
"commands": {
"version": "version",
"addFeed": "add-feed"
"addFeed": "add-feed",
"viewFeeds": "view-feeds"
}
}

18
app/index.js

@ -30,18 +30,18 @@ module.exports = (client) => {
client.on("message", message => {
if (message.author.id !== client.user.id) { //check the bot isn't triggering itself
if (message.channel.type === "dm")
HandleMessage.DM(client, config, message);
HandleMessage.dm(client, config, message);
else if (message.channel.type === "text" && message.member)
HandleMessage.Text(client, config, message, guildsData);
HandleMessage.text(client, config, message, guildsData);
}
});
};
const HandleMessage = {
DM: (client, config, message) => {
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) => {
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
@ -55,6 +55,9 @@ const HandleMessage = {
case config.commands.addFeed:
addFeed(client, guildsData, message, config.maxCacheSize);
break;
case config.commands.viewFeeds:
viewFeeds(client, guildsData, message);
break;
}
}
else if (guildsData[message.guild.id]) {
@ -67,8 +70,6 @@ const HandleMessage = {
};
function addFeed(client, guildsData, message, maxCacheSize) {
const parameters = message.content.split(" "); //expect !addfeed <url> <channelName> <roleName>
const feedUrl = [...GetUrls(message.content)][0];
const channel = message.mentions.channels.first();
@ -102,6 +103,11 @@ function addFeed(client, guildsData, message, maxCacheSize) {
});
}
function viewFeeds(client, guildsData, message){
const guildData = guildsData[message.guild.id];
message.reply(guildData.feeds.map(f => f.toString()).join("\n"));
}
function checkFeedsInGuilds(guilds, guildsData) {
Object.keys(guildsData).forEach(key => guildsData[key].checkFeeds(guilds));
}

7
app/models/feed-data.js

@ -21,7 +21,7 @@ module.exports = class FeedData {
.filter(el => !this.cachedLinks.includes(el)); //filter out any already cached
Array.prototype.push.apply(this.cachedLinks, unique);
if(this.cachedLinks.length > this.maxCacheSize)
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
};
}
@ -68,6 +68,11 @@ module.exports = class FeedData {
});
});
}
toString() {
const blacklist = ["cachedLinks", "maxCacheSize"];
return `\`\`\`JavaScript\n ${JSON.stringify(this, (k, v) => !blacklist.includes(k) ? v : undefined, "\t")} \`\`\``;
}
};
function normaliseUrl(url) {

Loading…
Cancel
Save