diff --git a/src/events/ready.ts b/src/events/ready.ts index bebe364..be0747c 100644 --- a/src/events/ready.ts +++ b/src/events/ready.ts @@ -2,25 +2,27 @@ import { Events } from "discord.js"; import { CronJob } from "cron"; import client from "../index"; -import fetchLatestUploads from "../utils/youtube/fetchLatestUploads"; import { config } from "../config"; // import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive"; import { cronUpdateBotInfo } from "../utils/cronJobs"; +import sendLatestUploads from "../utils/youtube/sendLatestUploads"; +import fetchLatestUploads from "../utils/youtube/fetchLatestUploads"; // Log into the bot client.once(Events.ClientReady, async (bot) => { console.log(`Ready! Logged in as ${bot.user?.tag}`); - // Set the bot's presence and update it every minute await cronUpdateBotInfo(); - fetchLatestUploads(); - - // Set the bot's presence and update it every minute new CronJob("0 * * * * *", async () => { await cronUpdateBotInfo(); }).start(); + + fetchLatestUploads(); setInterval(fetchLatestUploads, config.updateIntervalYouTube as number); + sendLatestUploads(); + setInterval(sendLatestUploads, config.updateIntervalYouTube as number); + // One at a time // checkIfStreamersAreLive(); // setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number); diff --git a/src/utils/youtube/fetchLatestUploads.ts b/src/utils/youtube/fetchLatestUploads.ts index 090292b..0e3c70d 100644 --- a/src/utils/youtube/fetchLatestUploads.ts +++ b/src/utils/youtube/fetchLatestUploads.ts @@ -1,14 +1,19 @@ -import type { dbYouTube } from "../../types/database"; - -import { ChannelType, TextChannel } from "discord.js"; +import type { dbDiscordTable, dbYouTube } from "../../types/database"; import { env } from "../../config"; import { getGuildsTrackingChannel, updateVideoId } from "../database"; -import client from "../.."; import { dbYouTubeGetAllChannelsToTrack } from "../db/youtube"; import getChannelDetails from "./getChannelDetails"; +export const updates = new Map< + string, + { + channelInfo: Awaited>; + discordGuildsToUpdate: dbDiscordTable[]; + } +>(); + export default async function fetchLatestUploads() { console.log("Fetching latest uploads..."); @@ -99,42 +104,47 @@ export default async function fetchLatestUploads() { const channelInfo = await getChannelDetails(channelId); - console.log("Discord guilds to update:", discordGuildsToUpdate); - for (const guild of discordGuildsToUpdate) { - try { - const channelObj = await client.channels.fetch( - guild.guild_channel_id, - ); - - if ( - !channelObj || - (channelObj.type !== ChannelType.GuildText && - channelObj.type !== - ChannelType.GuildAnnouncement) - ) { - console.error( - "Invalid channel or not a text channel in fetchLatestUploads", - ); - continue; - } - - await (channelObj as TextChannel).send({ - content: - guild.guild_ping_role && channelInfo - ? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}` - : guild.guild_ping_role - ? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}` - : channelInfo - ? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}` - : `New video uploaded! https://www.youtube.com/watch?v=${videoId}`, - }); - } catch (error) { - console.error( - "Error fetching or sending message to channel in fetchLatestUploads:", - error, - ); - } - } + updates.set(videoId, { + channelInfo, + discordGuildsToUpdate, + }); + + // console.log("Discord guilds to update:", discordGuildsToUpdate); + // for (const guild of discordGuildsToUpdate) { + // try { + // const channelObj = await client.channels.fetch( + // guild.guild_channel_id, + // ); + + // if ( + // !channelObj || + // (channelObj.type !== ChannelType.GuildText && + // channelObj.type !== + // ChannelType.GuildAnnouncement) + // ) { + // console.error( + // "Invalid channel or not a text channel in fetchLatestUploads", + // ); + // continue; + // } + + // await (channelObj as TextChannel).send({ + // content: + // guild.guild_ping_role && channelInfo + // ? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}` + // : guild.guild_ping_role + // ? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}` + // : channelInfo + // ? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}` + // : `New video uploaded! https://www.youtube.com/watch?v=${videoId}`, + // }); + // } catch (error) { + // console.error( + // "Error fetching or sending message to channel in fetchLatestUploads:", + // error, + // ); + // } + // } } } } diff --git a/src/utils/youtube/sendLatestUploads.ts b/src/utils/youtube/sendLatestUploads.ts new file mode 100644 index 0000000..9e7c94f --- /dev/null +++ b/src/utils/youtube/sendLatestUploads.ts @@ -0,0 +1,50 @@ +import { ChannelType, TextChannel } from "discord.js"; + +import client from "../.."; + +import { updates } from "./fetchLatestUploads"; + +export default async function sendLatestUploads() { + for (const [videoId, update] of updates.entries()) { + const channelInfo = update.channelInfo; + const discordGuildsToUpdate = update.discordGuildsToUpdate; + + console.log("Discord guilds to update:", discordGuildsToUpdate); + for (const guild of discordGuildsToUpdate) { + try { + const channelObj = await client.channels.fetch( + guild.guild_channel_id, + ); + + if ( + !channelObj || + (channelObj.type !== ChannelType.GuildText && + channelObj.type !== ChannelType.GuildAnnouncement) + ) { + console.error( + "Invalid channel or not a text channel in fetchLatestUploads", + ); + continue; + } + + await (channelObj as TextChannel).send({ + content: + guild.guild_ping_role && channelInfo + ? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}` + : guild.guild_ping_role + ? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}` + : channelInfo + ? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}` + : `New video uploaded! https://www.youtube.com/watch?v=${videoId}`, + }); + } catch (error) { + console.error( + "Error fetching or sending message to channel in fetchLatestUploads:", + error, + ); + } + } + // Remove the processed entry from the updates map + updates.delete(videoId); + } +}