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

Commit 180de2f

Browse files
committed
add new command to send a certain amount of otts
1 parent 751b5b6 commit 180de2f

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

commands/send.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { sendLast } = require('@mods/dailyotter');
2+
module.exports = {
3+
name: 'send',
4+
maxArgs: 1,
5+
category: 'DailyOttr',
6+
description: 'Send the last or a limited amount of Otters',
7+
expectedArgs: '[limit]',
8+
callback: async (message, arguments, text) => {
9+
const { guild } = message;
10+
let [limit] = arguments
11+
console.log(limit)
12+
if(!limit) limit = 1
13+
sendLast(guild, limit);
14+
},
15+
}

mods/dailyotter.js

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,48 @@ const { Task, addTask} = require('@util/scheduler')
88

99
const parser = new Parser();
1010

11-
const fetchOtters = async () => {
11+
const fetchOtters = async (save = true, limit = -1, shuffle = false) => {
1212

13-
const otters = []
14-
15-
logger.info(`[DailyOtterMod] Fetch otters`)
13+
logger.info(`[DailyOtterMod] Fetch otters, saving: ${save}`)
1614

1715
// parse dailyotter blog for pictures
18-
let feed = await parser.parseURL('https://dailyotter.org/?format=rss')
16+
const feed = await parser.parseURL('https://dailyotter.org/?format=rss')
1917
const picRe = /data-image=\"([a-zA-Z0-9\:\.\%\-\+\/]+)\"/
2018

21-
for (item of feed.items) {
19+
if(shuffle) feed.items.sort(() => Math.random() - 0.5);
20+
const ottersItems = limit > 0 ? feed.items.slice(feed.items.length - limit) : feed.items;
21+
const otters = []
22+
23+
for (item of ottersItems) {
2224

2325
// check for url in content
2426
let url = picRe.exec(item.content)
2527
if (url && url[1]) {
26-
// check if existant.
27-
const exists = await dailyotterSchema.find({
28-
guid: item.guid
29-
}) // TODO findOneAndUpdate?
3028

31-
if(exists.length !== 0) continue;
29+
if(save) {
30+
// check if existant.
31+
const exists = await dailyotterSchema.find({
32+
guid: item.guid
33+
}) // TODO findOneAndUpdate?
34+
if(exists.length !== 0) continue;
35+
}
3236
const otter = {
3337
title: item.title,
3438
date: item.isoDate,
3539
reference: item.contentSnippet,
3640
link: item.link,
3741
imageUrl: url[1],
3842
}
39-
await dailyotterSchema({
40-
guid: item.guid
41-
}).save(function (err, doc) {
42-
if(err) {
43-
logger.error(err);
44-
console.log(doc)
45-
}
46-
});
43+
if(save) {
44+
await dailyotterSchema({
45+
guid: item.guid
46+
}).save(function (err, doc) {
47+
if(err) {
48+
logger.error(err);
49+
console.log(doc)
50+
}
51+
});
52+
}
4753
// push to list of new otts
4854
otters.push(otter)
4955
}
@@ -52,6 +58,7 @@ const fetchOtters = async () => {
5258
// we want to send it so that the newest one is the last message
5359
return otters.reverse();
5460
}
61+
5562
const sendOtter = async (guild, otters) => {
5663

5764
const me = guild.me.user
@@ -63,14 +70,7 @@ const sendOtter = async (guild, otters) => {
6370
if(!channel) throw Error(`[DailyOtterMod] Channel not found on guild ${guild.name}`);
6471

6572
for (const otter of otters) {
66-
const otterEmbed = new MessageEmbed()
67-
.setAuthor(me.username, me.avatarURL())
68-
.setTitle(otter.title)
69-
.setImage(otter.imageUrl)
70-
.setTimestamp(otter.date)
71-
.setColor('#F2B749')
72-
.setURL(otter.link)
73-
.setFooter(`Fetched from dailyotter.org - ${otter.reference}`)
73+
const otterEmbed = generateEmbed(me, otter)
7474
try {
7575
await channel.send(otterEmbed)
7676
logger.debug(`[DailyOtterMod] Sent Ott to Server ${guild.name}`)
@@ -86,7 +86,32 @@ const sendOtter = async (guild, otters) => {
8686
}
8787
}
8888

89+
/**
90+
*
91+
* @param {User} me
92+
* @param {Otter} otter
93+
* @returns MessageEmbed
94+
*/
95+
const generateEmbed = (me, otter) => {
96+
97+
return new MessageEmbed()
98+
.setAuthor(me.username, me.avatarURL())
99+
.setTitle(otter.title)
100+
.setImage(otter.imageUrl)
101+
.setTimestamp(otter.date)
102+
.setColor('#F2B749')
103+
.setURL(otter.link)
104+
.setFooter(`Fetched from dailyotter.org - ${otter.reference}`)
89105

106+
}
107+
108+
const sendLast = async (guild, limit) => {
109+
110+
// parse dailyotter blog for pictures
111+
112+
const otters = await fetchOtters(false, limit, true);
113+
await sendOtter(guild, otters);
114+
}
90115

91116
module.exports = async (client) => {
92117

@@ -110,3 +135,5 @@ module.exports = async (client) => {
110135
addTask(updateOtters)
111136
logger.info(`[DailyOtterMod] Add Task to update otters`)
112137
}
138+
139+
module.exports.sendLast = sendLast;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "daily-otter-bot",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "Posts daily Ott pics",
55
"main": "bot.js",
66
"scripts": {

0 commit comments

Comments
 (0)