-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDavidRevoy.ts
More file actions
77 lines (65 loc) · 2.38 KB
/
DavidRevoy.ts
File metadata and controls
77 lines (65 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Cron, findTextChannelByName } from '../framework/index.ts';
import got from 'got';
import { parse } from 'node-html-parser';
import { decode } from 'html-entities';
import { KeyValue } from '../database/index.ts';
import { EmbedBuilder, SnowflakeUtil } from 'discord.js';
export default new Cron({
enabled: true,
name: 'David Revoy',
description:
'Vérifie toutes les 30 minutes si un nouveau strip de David Revoy est sorti',
schedule: '5,35 * * * *',
async handle(context) {
const strip = await getLastDavidRevoyStrip();
// vérifie le strip trouvé avec la dernière entrée
const lastStrip = await KeyValue.get<string>('Last-Cron-DavidRevoy');
const stripStoreIdentity = strip?.id ?? null;
if (lastStrip === stripStoreIdentity) return; // skip si identique
await KeyValue.set('Last-Cron-DavidRevoy', stripStoreIdentity); // met à jour sinon
if (!strip) return; // skip si pas de strip
context.logger.info(`Found a new David Revoy strip`, strip);
const channel = findTextChannelByName(context.client.channels, 'gif');
await channel.send({
embeds: [
new EmbedBuilder()
.setURL(strip.link)
.setTitle(strip.title)
.setImage(strip.imageUrl)
.setTimestamp(strip.date),
],
enforceNonce: true,
nonce: SnowflakeUtil.generate().toString(),
});
},
});
interface IDavidRevoyStrip {
id: string;
link: string;
title: string;
date: Date;
imageUrl: string;
}
export async function getLastDavidRevoyStrip(): Promise<IDavidRevoyStrip | null> {
const { body } = await got(
'https://www.davidrevoy.com/feed/rss/categorie2/webcomics/',
);
const rss = parse(body, {
blockTextElements: {
// link tag in XML RSS is a block with text content
link: true,
},
});
const item = rss.querySelector('item');
if (!item) return null;
const rawDescription = item.querySelector('description')?.textContent ?? '';
const description = parse(decode(rawDescription));
const img = description.querySelector('img');
return {
id: item.querySelector('guid')?.textContent?.trim() ?? '',
link: item.querySelector('link')?.textContent?.trim() ?? '',
title: item.querySelector('title')?.textContent?.trim() ?? '',
date: new Date(item.querySelector('pubDate')?.textContent ?? new Date()),
imageUrl: img?.parentNode?.getAttribute('href') ?? '',
};
}