Skip to content

chore(latest-uploads): split update function #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
90 changes: 50 additions & 40 deletions src/utils/youtube/fetchLatestUploads.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof getChannelDetails>>;
discordGuildsToUpdate: dbDiscordTable[];
}
>();

export default async function fetchLatestUploads() {
console.log("Fetching latest uploads...");

Expand Down Expand Up @@ -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,
// );
// }
// }
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/utils/youtube/sendLatestUploads.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}