Skip to content

Commit

Permalink
add image links to db and some changes, no beta
Browse files Browse the repository at this point in the history
  • Loading branch information
aottr committed Sep 8, 2021
1 parent 97cb4ff commit 512cccb
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ jobs:
- uses: actions/checkout@v2

- name: Build the Docker image
run: docker build . --file Dockerfile --tag ghcr.io/ottrone/dailyottr:beta
run: docker build . --file Dockerfile --tag ghcr.io/ottrone/dailyottr:latest --tag ghcr.io/ottrone/dailyottr:$(date +%s)

- name: Publish Docker image to Github Packages
shell: bash
env:
PAT: ${{ secrets.PAT }}
run: |
echo $PAT | docker login ghcr.io --username alexottr --password-stdin
docker push ghcr.io/ottrone/dailyottr:beta
docker push ghcr.io/ottrone/dailyottr --all-tags
docker logout
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dailyottr",
"version": "1.0.4",
"version": "1.0.5",
"description": "DailyOttr bot based on LexBot TypeScript!",
"scripts": {
"prebuild": "node -p \"'export const VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
Expand Down
25 changes: 19 additions & 6 deletions src/commands/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ const parser = new Parser();

export = {
name: 'health',
type: CommandType.NORMAL,
type: CommandType.BOTH,
category: 'DailyOttr',
maxArgs: 0,
permissions: ['ADMINISTRATOR'],
description: 'Health report about the DailyOttr bot. This call is expensive..',
run: async ({ message, member }: CallbackOptions) => {

if (!message) return;
run: async ({ message, member, interaction }: CallbackOptions) => {

const { guild } = member;

if (interaction) {
if (!interaction.channel) return;
interaction.deferReply({
ephemeral: true,
});
}

let rss = false;
let channel = false;
let embed = false;
Expand Down Expand Up @@ -66,8 +71,16 @@ export = {

checkmsg += embed ? '🟢' : '🔴';
checkmsg += ' Permission to send Embeds in the configured channel';
message.channel.send(checkmsg);

message.channel.send(`Set ${message.channel} as Ott channel !`);
if (message) {
message.channel.send(checkmsg);
}
else if (interaction) {
interaction.channel?.send(checkmsg);
interaction.editReply({
content: 'Healthtest done.',
});
}

},
} as Command;
4 changes: 2 additions & 2 deletions src/commands/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export = {
{
name: 'limit',
description: 'Number of Otters to fetch',
required: true,
required: false,
type: Constants.ApplicationCommandOptionTypes.NUMBER,
} as ApplicationCommandOptionData,
],
Expand All @@ -40,7 +40,7 @@ export = {
ephemeral: true,
});

const limit = interaction.options.getNumber('limit')!;
const limit = interaction.options.getNumber('limit') || 1;
await sendLast(guild, limit);
interaction.editReply({
content: 'Otters sent.',
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/Otter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Otter {
guid?: string;
title: string;
date: string;
reference: string;
Expand Down
65 changes: 48 additions & 17 deletions src/mods/dailyotter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ import Parser from 'rss-parser';
import dailyOtterModel from '../schemas/dailyotter-schema';
import { Otter } from '../interfaces/Otter';
import configModel from '../schemas/doconf-schema';
import { info, error as _error, debug } from '../core/logger';
import logger from '../core/logger';
import { Task, addTask } from '../core/scheduler';

const parser = new Parser();

const fetchOtters = async (save = true, limit = -1, shuffle = false): Promise<Array<Otter>> => {
/**
* DailyOtter
* @version 1.2
*/

/**
* Fetch Otters from the daily otter blog.
* @param {boolean} save the results to database
* @param {number} limit the results
* @param {boolean} shuffle the results
* @returns Promise with an Otter Array
*/
const fetchOtters = async (save: boolean = true, limit: number = -1, shuffle: boolean = false): Promise<Array<Otter>> => {

info(`[DailyOtterMod] Fetch otters, saving: ${save}`);
logger.info(`[DailyOtterMod] Fetch otters, saving: ${save}`);

// parse dailyotter blog for pictures
const feed = await parser.parseURL('https://dailyotter.org/?format=rss');
Expand Down Expand Up @@ -46,10 +58,15 @@ const fetchOtters = async (save = true, limit = -1, shuffle = false): Promise<Ar
if (save) {
new dailyOtterModel({
guid: item.guid,
title: item.title,
date: item.isoDate,
reference: item.contentSnippet,
link: item.link,
imageUrl: url[1],
}).save(function(err: any, doc: any) {
if (err) {
_error(err);
console.log(doc);
logger.error(err);
logger.debug(doc);
}
});
}
Expand All @@ -62,6 +79,16 @@ const fetchOtters = async (save = true, limit = -1, shuffle = false): Promise<Ar
return otters.reverse();
};

const fetchLocalOtters = async (limit: number = 0, shuffle: boolean = false): Promise<Array<Otter>> => {

const otters = (
await dailyOtterModel.find().limit(limit)
) as Array<Otter>;
if (shuffle) otters.sort(() => Math.random() - 0.5);

return otters.reverse();
};

const sendOtter = async (guild: Guild, otters: Array<Otter>) => {

if (!guild.me) return;
Expand All @@ -79,19 +106,18 @@ const sendOtter = async (guild: Guild, otters: Array<Otter>) => {
try {
if (channel.isText()) {
await channel.send({ embeds: [otterEmbed] });
debug(`[DailyOtterMod] Sent Ott to Server ${guild.name}`);
logger.debug(`[DailyOtterMod] Sent Ott to Server ${guild.name}`);
}
}
catch (error) {
_error(`[DailyOtterMod] Unable to send Messages in the configured channel on Server ${guild.name}`);
console.log(error);
logger.error(`[DailyOtterMod] Unable to send Messages in the configured channel on Server ${guild.name}`);
logger.error(error);
}
}

}
catch (err) {
console.log(err);
// _error(err);
logger.error(err);
return;
}
};
Expand All @@ -114,19 +140,25 @@ const generateEmbed = (me: User, otter: Otter) => {
.setFooter(`Fetched from dailyotter.org - ${otter.reference}`);
};

/**
* Send a requested amount of otts to a server.
* @param {Guild} guild the current guild operating in
* @param {number} limit the amount of pictures sent
*/
const sendLast = async (guild: Guild, limit: number) => {

// parse dailyotter blog for pictures

const otters = await fetchOtters(false, limit, true);
const otters = await fetchLocalOtters(limit, true);
await sendOtter(guild, otters);
};

export default async (client: Client) => {

fetchLocalOtters();
// first execution on startup
const otters = await fetchOtters();
debug(`[DailyOtterMod] Found ${otters.length} new Otters !`);
logger.debug(`[DailyOtterMod] Found ${otters.length} new Otters !`);
client.guilds.cache.forEach((guild) => {
sendOtter(guild, otters);
});
Expand All @@ -136,14 +168,13 @@ export default async (client: Client) => {
const clnt = context[0];

const otts = await fetchOtters();
debug(`[DailyOtterMod] Found ${otts.length} new Otters !`);
logger.debug(`[DailyOtterMod] Found ${otts.length} new Otters !`);
clnt.guilds.cache.forEach((guild: Guild) => {
sendOtter(guild, otts);
});
}, 5 * 60 * 60 * 1000, undefined, client);
}, 3 * 60 * 60 * 1000, undefined, client);
addTask(updateOtters);
info('[DailyOtterMod] Add Task to update otters');
logger.info('[DailyOtterMod] Add Task to update otters');
};

const _sendLast = sendLast;
export { _sendLast as sendLast };
export { sendLast };
12 changes: 11 additions & 1 deletion src/schemas/dailyotter-schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import mongoose, { Document, Schema } from 'mongoose';
import { reqString } from './types';
import { reqString, reqDate } from './types';

export interface IDailyOtter extends Document {
guid: string;
title: string;
date: string;
reference: string;
link: string;
imageUrl: string;
}

const DailyOtterSchema: Schema = new mongoose.Schema({
guid: reqString,
title: reqString,
date: reqDate,
reference: reqString,
link: reqString,
imageUrl: reqString,
});

export default mongoose.model<IDailyOtter>('dailyotter-pics', DailyOtterSchema);
5 changes: 5 additions & 0 deletions src/schemas/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export const reqBoolean = {
type: Boolean,
required: true,
};

export const reqDate = {
type: Date,
required: true,
};
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "1.0.4";
export const VERSION = "1.0.5";

0 comments on commit 512cccb

Please sign in to comment.