UNPKG

rashi-discord-bot-lib

Version:

🚀 Powerful Discord bot framework with built-in database, event handling, and utilities

81 lines • 3.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.YouTubeNotifier = void 0; const discord_js_1 = require("discord.js"); const i18n_1 = require("../../i18n"); // upewnij się, że helper z funkcją `t()` istnieje const rss_parser_1 = __importDefault(require("rss-parser")); const parser = new rss_parser_1.default(); class YouTubeNotifier { channels; webhookUrl; lastVideos = new Map(); constructor(channels, webhookUrl) { this.channels = channels; this.webhookUrl = webhookUrl; } async poll() { for (const channelId of this.channels) { const feedUrl = `https://www.youtube.com/feeds/videos.xml?channel_id=${channelId}`; try { const feed = await parser.parseURL(feedUrl); const latest = feed.items?.[0]; if (!latest || !latest.id) continue; const videoId = latest.id.replace('yt:video:', ''); const previous = this.lastVideos.get(channelId); if (!previous || previous.id !== videoId) { const video = { title: latest.title ?? 'No title', url: `https://www.youtube.com/watch?v=${videoId}`, thumbnail: `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`, publishedAt: latest.pubDate, channel: feed.title ?? 'Unknown channel', }; await this.sendNotification(video); this.lastVideos.set(channelId, { id: videoId, published: latest.pubDate ?? new Date().toISOString(), }); } } catch (err) { console.error(`[YouTubeNotifier] Failed to poll ${channelId}:`, err); } } } async sendNotification(video) { try { const webhook = new discord_js_1.WebhookClient({ url: this.webhookUrl }); const embed = new discord_js_1.EmbedBuilder() .setTitle(video.title) .setURL(video.url) .setColor(0xff0000) .setTimestamp(new Date(video.publishedAt || Date.now())) .setAuthor({ name: `${(0, i18n_1.t)('youtube.new_upload')} ${video.channel ?? ''}`.trim(), iconURL: 'https://www.youtube.com/s/desktop/fb7c011e/img/favicon_144x144.png', }) .setDescription((0, i18n_1.t)('youtube.watch_now_description')); if (video.thumbnail) { embed.setThumbnail(video.thumbnail); } const row = new discord_js_1.ActionRowBuilder().addComponents(new discord_js_1.ButtonBuilder() .setStyle(discord_js_1.ButtonStyle.Link) .setLabel((0, i18n_1.t)('youtube.watch_button')) .setURL(video.url)); await webhook.send({ content: (0, i18n_1.t)('youtube.new_video_alert'), embeds: [embed], components: [row], }); } catch (error) { console.error('[YouTubeNotifier] Webhook error:', error); } } } exports.YouTubeNotifier = YouTubeNotifier; //# sourceMappingURL=YouTubeNotifier.js.map