1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-09-30 09:10:58 -04:00
Daggerbot-TS/src/funcs/YTModule.ts

21 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-08-30 04:34:59 -04:00
import {TextChannel} from 'discord.js';
import TClient from '../client.js';
2023-09-03 04:56:22 -04:00
import LogPrefix from '../helpers/LogPrefix.js';
import MessageTool from '../helpers/MessageTool.js';
2023-08-30 04:34:59 -04:00
export default async(client: TClient, YTChannelID: string, YTChannelName: string, DiscordChannelID: string, DiscordRoleID: string)=>{
let Data: any;
try {
await fetch(`https://www.youtube.com/feeds/videos.xml?channel_id=${YTChannelID}`, {signal: AbortSignal.timeout(8000), headers: {'User-Agent': 'Daggerbot - Notification/undici'}}).then(async xml=>Data = client.xjs.xml2js(await xml.text(), {compact: true}))
} catch(err){
2023-09-03 04:56:22 -04:00
console.log(client.logTime(), LogPrefix('YTModule'), `Failed to fetch "${YTChannelName}" from YouTube`)
2023-08-30 04:34:59 -04:00
}
if (!Data) return;
if (!client.YTCache[YTChannelID]) return client.YTCache[YTChannelID] = Data.feed.entry[0]['yt:videoId']._text;
if (Data.feed.entry[1]['yt:videoId']._text === client.YTCache[YTChannelID]){
client.YTCache[YTChannelID] = Data.feed.entry[0]['yt:videoId']._text;
(client.channels.resolve(DiscordChannelID) as TextChannel).send({content: `${MessageTool.formatMention(DiscordRoleID, 'role')}\n**${YTChannelName}** just uploaded a video!\n${Data.feed.entry[0].link._attributes.href}`, allowedMentions: {parse: ['roles']}})
2023-08-30 04:34:59 -04:00
}
}