Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
add new command to send a certain amount of otts
Browse files Browse the repository at this point in the history
  • Loading branch information
aottr committed Aug 30, 2021
1 parent 751b5b6 commit 180de2f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 28 deletions.
15 changes: 15 additions & 0 deletions commands/send.js
Original file line number Diff line number Diff line change
@@ -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);
},
}
81 changes: 54 additions & 27 deletions mods/dailyotter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,48 @@ 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,
reference: item.contentSnippet,
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)
}
Expand All @@ -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
Expand All @@ -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}`)
Expand All @@ -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) => {

Expand All @@ -110,3 +135,5 @@ module.exports = async (client) => {
addTask(updateOtters)
logger.info(`[DailyOtterMod] Add Task to update otters`)
}

module.exports.sendLast = sendLast;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 180de2f

Please sign in to comment.