From 180de2fe5124fc0222238bc85ef1159c8516cd3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dustin=20Kr=C3=B6ger?= Date: Mon, 30 Aug 2021 21:11:43 +0200 Subject: [PATCH] add new command to send a certain amount of otts --- commands/send.js | 15 +++++++++ mods/dailyotter.js | 81 ++++++++++++++++++++++++++++++---------------- package.json | 2 +- 3 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 commands/send.js diff --git a/commands/send.js b/commands/send.js new file mode 100644 index 0000000..370e557 --- /dev/null +++ b/commands/send.js @@ -0,0 +1,15 @@ +const { sendLast } = require('@mods/dailyotter'); +module.exports = { + name: 'send', + maxArgs: 1, + category: 'DailyOttr', + description: 'Send the last or a limited amount of Otters', + expectedArgs: '[limit]', + callback: async (message, arguments, text) => { + const { guild } = message; + let [limit] = arguments + console.log(limit) + if(!limit) limit = 1 + sendLast(guild, limit); + }, +} diff --git a/mods/dailyotter.js b/mods/dailyotter.js index eb53236..4d772c6 100644 --- a/mods/dailyotter.js +++ b/mods/dailyotter.js @@ -8,27 +8,31 @@ const { Task, addTask} = require('@util/scheduler') const parser = new Parser(); -const fetchOtters = async () => { +const fetchOtters = async (save = true, limit = -1, shuffle = false) => { - const otters = [] - - logger.info(`[DailyOtterMod] Fetch otters`) + logger.info(`[DailyOtterMod] Fetch otters, saving: ${save}`) // parse dailyotter blog for pictures - let feed = await parser.parseURL('https://dailyotter.org/?format=rss') + const feed = await parser.parseURL('https://dailyotter.org/?format=rss') const picRe = /data-image=\"([a-zA-Z0-9\:\.\%\-\+\/]+)\"/ - for (item of feed.items) { + if(shuffle) feed.items.sort(() => Math.random() - 0.5); + const ottersItems = limit > 0 ? feed.items.slice(feed.items.length - limit) : feed.items; + const otters = [] + + for (item of ottersItems) { // check for url in content let url = picRe.exec(item.content) if (url && url[1]) { - // check if existant. - const exists = await dailyotterSchema.find({ - guid: item.guid - }) // TODO findOneAndUpdate? - if(exists.length !== 0) continue; + if(save) { + // check if existant. + const exists = await dailyotterSchema.find({ + guid: item.guid + }) // TODO findOneAndUpdate? + if(exists.length !== 0) continue; + } const otter = { title: item.title, date: item.isoDate, @@ -36,14 +40,16 @@ const fetchOtters = async () => { link: item.link, imageUrl: url[1], } - await dailyotterSchema({ - guid: item.guid - }).save(function (err, doc) { - if(err) { - logger.error(err); - console.log(doc) - } - }); + if(save) { + await dailyotterSchema({ + guid: item.guid + }).save(function (err, doc) { + if(err) { + logger.error(err); + console.log(doc) + } + }); + } // push to list of new otts otters.push(otter) } @@ -52,6 +58,7 @@ const fetchOtters = async () => { // we want to send it so that the newest one is the last message return otters.reverse(); } + const sendOtter = async (guild, otters) => { const me = guild.me.user @@ -63,14 +70,7 @@ const sendOtter = async (guild, otters) => { if(!channel) throw Error(`[DailyOtterMod] Channel not found on guild ${guild.name}`); for (const otter of otters) { - const otterEmbed = new MessageEmbed() - .setAuthor(me.username, me.avatarURL()) - .setTitle(otter.title) - .setImage(otter.imageUrl) - .setTimestamp(otter.date) - .setColor('#F2B749') - .setURL(otter.link) - .setFooter(`Fetched from dailyotter.org - ${otter.reference}`) + const otterEmbed = generateEmbed(me, otter) try { await channel.send(otterEmbed) logger.debug(`[DailyOtterMod] Sent Ott to Server ${guild.name}`) @@ -86,7 +86,32 @@ const sendOtter = async (guild, otters) => { } } +/** + * + * @param {User} me + * @param {Otter} otter + * @returns MessageEmbed + */ +const generateEmbed = (me, otter) => { + + return new MessageEmbed() + .setAuthor(me.username, me.avatarURL()) + .setTitle(otter.title) + .setImage(otter.imageUrl) + .setTimestamp(otter.date) + .setColor('#F2B749') + .setURL(otter.link) + .setFooter(`Fetched from dailyotter.org - ${otter.reference}`) +} + +const sendLast = async (guild, limit) => { + + // parse dailyotter blog for pictures + + const otters = await fetchOtters(false, limit, true); + await sendOtter(guild, otters); +} module.exports = async (client) => { @@ -110,3 +135,5 @@ module.exports = async (client) => { addTask(updateOtters) logger.info(`[DailyOtterMod] Add Task to update otters`) } + +module.exports.sendLast = sendLast; diff --git a/package.json b/package.json index ddbb0dd..7aaac1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "daily-otter-bot", - "version": "1.0.0", + "version": "1.0.2", "description": "Posts daily Ott pics", "main": "bot.js", "scripts": {