Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/crons/CommitStrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export default new Cron({
context.logger.info(`Found a new CommitStrip (${latestCommitStrip.id})`);

const channel = findTextChannelByName(context.client.channels, 'gif');
if (!channel) {
throw new Error('found no #gif channel');
}

await channel.send(
new MessageEmbed()
Expand Down Expand Up @@ -90,7 +87,7 @@ async function getRecentCommitStrip(now: Date): Promise<CommitStrip | null> {

const [strip] = posts;

const stripDate = new Date(strip.date_gmt + ".000Z");
const stripDate = new Date(strip.date_gmt + '.000Z');
const stripTime = stripDate.getTime();
const nowTime = now.getTime();
const thirtyMinutes = 1000 * 60 * 30;
Expand Down
14 changes: 9 additions & 5 deletions src/crons/EpicGames.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MessageEmbed } from 'discord.js';
import got from 'got';
import { Logger } from 'pino';

import { Cron, findTextChannelByName } from '../framework';

Expand All @@ -19,15 +20,12 @@ export default new Cron({
'Vérifie tous les jours à 17h00 (Paris) si Epic Games offre un jeu (promotion gratuite) et alerte dans #jeux',
schedule: '0 17 * * *',
async handle(context) {
const games = await getOfferedGames(context.date);
const games = await getOfferedGames(context.date, context.logger);
if (!games) {
return;
}

const channel = findTextChannelByName(context.client.channels, 'jeux');
if (!channel) {
throw new Error('found no #jeux channel');
}

for (const game of games) {
context.logger.info(`Found a new offered game (${game.title})`);
Expand Down Expand Up @@ -145,7 +143,10 @@ const oneDay = 1000 * 60 * 60 * 24;
* were offered between the previous and current cron execution, returns them.
* @param now - Current date. Comes from cron schedule.
*/
async function getOfferedGames(now: Date): Promise<Game[] | null> {
async function getOfferedGames(
now: Date,
logger: Logger,
): Promise<Game[] | null> {
const { body } = await got<EpicGamesProducts>(
'https://www.epicgames.com/graphql',
{
Expand All @@ -161,6 +162,9 @@ async function getOfferedGames(now: Date): Promise<Game[] | null> {
responseType: 'json',
},
);

logger.info({ data: body.data }, 'Offered games GraphQL response');

const catalog = body.data.Catalog.searchStore;

if (catalog.paging.total === 0) {
Expand Down
18 changes: 16 additions & 2 deletions src/framework/Cron.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { randomUUID } from 'crypto';
import { CronJob, CronTime } from 'cron';
import { Client } from 'discord.js';
import { Client, MessageEmbed } from 'discord.js';
import { Logger } from 'pino';
import { Base, BaseConfig } from './Base';
import { Bot } from './Bot';
import { findTextChannelByName } from './helpers';

export type CronHandler = (context: CronContext) => Promise<void>;

Expand Down Expand Up @@ -52,8 +53,9 @@ export class Cron extends Base {
}

private async executeJob(date: Date, bot: Bot) {
const id = randomUUID();
const logger = bot.logger.child({
id: randomUUID(),
id,
type: 'Cron',
cronName: this.name,
});
Expand All @@ -62,6 +64,18 @@ export class Cron extends Base {
await this.handler({ date, client: bot.client, logger });
} catch (error) {
logger.error(error, 'cron handler error');
try {
await findTextChannelByName(bot.client.channels, 'logs').send(
new MessageEmbed()
.setTitle('Cron run failed')
.addField('Name', this.name, true)
.addField('Run id', id, true)
.setDescription(`\`\`\`\n${error.stack}\n\`\`\``)
.setColor('RED'),
);
} catch (error2) {
logger.error(error2, 'failed to send error to #logs');
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/framework/FormatChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export class FormatChecker extends Base {
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const channel = findTextChannelByName(message.guild!.channels, 'logs');
if (channel === undefined)
return logger.fatal('text channel not found: logs');
channel.send(`Bonjour ${author},\n${warningContent}`);
}
logger.debug('warning message sent');
Expand Down
13 changes: 10 additions & 3 deletions src/framework/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import {
export function findTextChannelByName(
manager: ChannelManager | GuildChannelManager,
name: string,
): TextChannel | undefined {
return manager.cache.find(
): TextChannel {
const channel = manager.cache.find(
(channel) => isTextChannel(channel) && channel.name === name,
) as TextChannel;
);
if (!channel) {
throw new Error(`found no #${name} channel`);
}
if (!(channel instanceof TextChannel)) {
throw new Error(`channel #${name} is not a text channel`);
}
return channel;
}

export function isTextChannel(channel: Channel): channel is TextChannel {
Expand Down