123 lines
4.0 KiB
TypeScript
123 lines
4.0 KiB
TypeScript
import { Client, TextChannel } from 'discord.js'
|
|
import { Document } from 'mongoose'
|
|
import Parser from 'rss-parser'
|
|
import 'dotenv/config'
|
|
|
|
export interface Feed {
|
|
name: string
|
|
url: string
|
|
token?: string
|
|
}
|
|
|
|
export default async (client: Client, guildProfile: Document) => {
|
|
try {
|
|
let guild = client.guilds.cache.get(guildProfile.get('guildId'))
|
|
if (!guild) {
|
|
clearInterval(client.disco.interval)
|
|
console.log(`Aucun serveur trouvé avec l'id \`${guildProfile.get('guildId')}\`, veuillez utiliser la commande \`/setchannel\` !`)
|
|
return 'clear'
|
|
}
|
|
|
|
let dbData = guildProfile.get('guildRss')
|
|
let channel = client.channels.cache.get(dbData.channelId) as TextChannel
|
|
if (!channel) {
|
|
clearInterval(client.disco.interval)
|
|
console.log(`Aucun channel trouvé avec l'id \`${dbData.channelId}\`, veuillez utiliser la commande \`/setchannel\` !`)
|
|
return 'clear'
|
|
}
|
|
|
|
let feeds = [
|
|
{
|
|
name: 'Nautiljon - Actualités',
|
|
url: 'https://www.nautiljon.com/actualite/rss.php'
|
|
},
|
|
{
|
|
name: 'Nautiljon - Les Brèves',
|
|
url: 'https://www.nautiljon.com/breves/rss.php'
|
|
},
|
|
{
|
|
name: 'YGG - Application',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2144&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - Audio',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2139&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - eBook',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2140&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - Emulation',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2141&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - Flim/Vidéo',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2145&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - GPS',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2143&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - Imprimante 3D',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2200&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - Jeu Vidéo',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2142&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - Nulled',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2300&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
},
|
|
{
|
|
name: 'YGG - XXX',
|
|
url: 'https://www3.yggtorrent.qa/rss?action=generate&type=cat&id=2188&passkey=',
|
|
token: process.env.YGG_PASSKEY
|
|
}
|
|
]
|
|
|
|
if (!dbData.feeds) dbData.feeds = feeds
|
|
dbData.feeds.forEach((feed: Feed, i: Number) => { setTimeout(async () => {
|
|
let parser = new Parser()
|
|
if (feed.token) feed.url += feed.token
|
|
let feedData = await parser.parseURL(feed.url)
|
|
|
|
let thread = channel.threads.cache.find(thread => thread.name === feed.name)
|
|
if (!thread) {
|
|
thread = await channel.threads.create({
|
|
name: feed.name,
|
|
autoArchiveDuration: 60,
|
|
reason: 'Création du fil RSS'
|
|
})
|
|
await thread.send({ content: `Fil RSS créé !\nVisionnage de **${feedData.title}** sur ${feedData.link}...` })
|
|
|
|
feedData.items.reverse().forEach(async (item, i) => {
|
|
setTimeout(async () => await thread?.send({ content: `**${item.title}**\n${item.link}` }), i * 1000)
|
|
})
|
|
}
|
|
|
|
let lastItem = feedData.items[0]
|
|
if (!lastItem) return console.log('No last item found for ' + feed.name)
|
|
|
|
let messages = await thread.messages.fetch({ limit: 1 })
|
|
if (!messages) return console.log('No messages found for ' + feed.name)
|
|
|
|
let lastMessage = messages.first()
|
|
if (!lastMessage) return console.log('No last message found for ' + feed.name)
|
|
|
|
if (lastMessage.content !== `**${lastItem.title}**\n${lastItem.link}`) await thread.send({ content: `**${lastItem.title}**\n${lastItem.link}` })
|
|
//else console.log('No new item found for ' + feed.name)
|
|
}, Number(i) * 1000) })
|
|
} catch (error: any) { console.error(error); return 'clear' }
|
|
} |