From 512cccb16eb8d7fbca7a00c9863f9661f8ab61d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dustin=20Kr=C3=B6ger?= Date: Thu, 9 Sep 2021 00:49:43 +0200 Subject: [PATCH] add image links to db and some changes, no beta --- .github/workflows/docker-image.yml | 4 +- package.json | 2 +- src/commands/health.ts | 25 +++++++++--- src/commands/send.ts | 4 +- src/interfaces/Otter.ts | 1 + src/mods/dailyotter.ts | 65 ++++++++++++++++++++++-------- src/schemas/dailyotter-schema.ts | 12 +++++- src/schemas/types.ts | 5 +++ src/version.ts | 2 +- 9 files changed, 90 insertions(+), 30 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 278ad6b..17874f0 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -16,7 +16,7 @@ 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 @@ -24,7 +24,7 @@ jobs: 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 diff --git a/package.json b/package.json index 8d17538..3f3f720 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/health.ts b/src/commands/health.ts index 752d6b3..b673385 100644 --- a/src/commands/health.ts +++ b/src/commands/health.ts @@ -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; @@ -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; diff --git a/src/commands/send.ts b/src/commands/send.ts index 8d74929..77fa711 100644 --- a/src/commands/send.ts +++ b/src/commands/send.ts @@ -14,7 +14,7 @@ export = { { name: 'limit', description: 'Number of Otters to fetch', - required: true, + required: false, type: Constants.ApplicationCommandOptionTypes.NUMBER, } as ApplicationCommandOptionData, ], @@ -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.', diff --git a/src/interfaces/Otter.ts b/src/interfaces/Otter.ts index c743c3a..d89b8bb 100644 --- a/src/interfaces/Otter.ts +++ b/src/interfaces/Otter.ts @@ -1,4 +1,5 @@ export interface Otter { + guid?: string; title: string; date: string; reference: string; diff --git a/src/mods/dailyotter.ts b/src/mods/dailyotter.ts index d7f0532..e7f028b 100644 --- a/src/mods/dailyotter.ts +++ b/src/mods/dailyotter.ts @@ -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> => { +/** + * 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> => { - 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'); @@ -46,10 +58,15 @@ const fetchOtters = async (save = true, limit = -1, shuffle = false): Promise> => { + + const otters = ( + await dailyOtterModel.find().limit(limit) + ) as Array; + if (shuffle) otters.sort(() => Math.random() - 0.5); + + return otters.reverse(); +}; + const sendOtter = async (guild: Guild, otters: Array) => { if (!guild.me) return; @@ -79,19 +106,18 @@ const sendOtter = async (guild: Guild, otters: Array) => { 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; } }; @@ -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); }); @@ -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 }; diff --git a/src/schemas/dailyotter-schema.ts b/src/schemas/dailyotter-schema.ts index 427c552..4ac0860 100644 --- a/src/schemas/dailyotter-schema.ts +++ b/src/schemas/dailyotter-schema.ts @@ -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('dailyotter-pics', DailyOtterSchema); diff --git a/src/schemas/types.ts b/src/schemas/types.ts index 8160b14..f4ae474 100644 --- a/src/schemas/types.ts +++ b/src/schemas/types.ts @@ -7,3 +7,8 @@ export const reqBoolean = { type: Boolean, required: true, }; + +export const reqDate = { + type: Date, + required: true, +}; diff --git a/src/version.ts b/src/version.ts index 0e972ed..61f1c35 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const VERSION = "1.0.4"; +export const VERSION = "1.0.5";