Skip to content

Commit b4980b0

Browse files
committed
feat(latest-uploads): split update function
1 parent 6db12aa commit b4980b0

File tree

2 files changed

+104
-44
lines changed

2 files changed

+104
-44
lines changed

src/events/ready.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { Events } from "discord.js";
22
import { CronJob } from "cron";
33

44
import client from "../index";
5-
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
5+
import {
6+
fetchLatestUploads,
7+
sendLatestUploads,
8+
} from "../utils/youtube/fetchLatestUploads";
69
import { config } from "../config";
710
// import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
811
import { cronUpdateBotInfo } from "../utils/cronJobs";
@@ -11,16 +14,17 @@ import { cronUpdateBotInfo } from "../utils/cronJobs";
1114
client.once(Events.ClientReady, async (bot) => {
1215
console.log(`Ready! Logged in as ${bot.user?.tag}`);
1316

14-
// Set the bot's presence and update it every minute
1517
await cronUpdateBotInfo();
16-
fetchLatestUploads();
17-
18-
// Set the bot's presence and update it every minute
1918
new CronJob("0 * * * * *", async () => {
2019
await cronUpdateBotInfo();
2120
}).start();
21+
22+
fetchLatestUploads();
2223
setInterval(fetchLatestUploads, config.updateIntervalYouTube as number);
2324

25+
sendLatestUploads();
26+
setInterval(sendLatestUploads, config.updateIntervalYouTube as number);
27+
2428
// One at a time
2529
// checkIfStreamersAreLive();
2630
// setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number);

src/utils/youtube/fetchLatestUploads.ts

Lines changed: 95 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import type { dbYouTube } from "../../types/database";
1+
import type { dbDiscordTable, dbYouTube } from "../../types/database";
22

3-
import { ChannelType, TextChannel } from "discord.js";
3+
import { ChannelType, type TextChannel } from "discord.js";
44

55
import { env } from "../../config";
66
import { getGuildsTrackingChannel, updateVideoId } from "../database";
7-
import client from "../..";
87
import { dbYouTubeGetAllChannelsToTrack } from "../db/youtube";
8+
import client from "../..";
99

1010
import getChannelDetails from "./getChannelDetails";
1111

12-
export default async function fetchLatestUploads() {
12+
const updates = new Map<
13+
string,
14+
{
15+
channelInfo: Awaited<ReturnType<typeof getChannelDetails>>;
16+
discordGuildsToUpdate: dbDiscordTable[];
17+
}
18+
>();
19+
20+
export async function fetchLatestUploads() {
1321
console.log("Fetching latest uploads...");
1422

1523
const channels: dbYouTube[] | [] = await dbYouTubeGetAllChannelsToTrack();
@@ -99,42 +107,90 @@ export default async function fetchLatestUploads() {
99107

100108
const channelInfo = await getChannelDetails(channelId);
101109

102-
console.log("Discord guilds to update:", discordGuildsToUpdate);
103-
for (const guild of discordGuildsToUpdate) {
104-
try {
105-
const channelObj = await client.channels.fetch(
106-
guild.guild_channel_id,
107-
);
108-
109-
if (
110-
!channelObj ||
111-
(channelObj.type !== ChannelType.GuildText &&
112-
channelObj.type !==
113-
ChannelType.GuildAnnouncement)
114-
) {
115-
console.error(
116-
"Invalid channel or not a text channel in fetchLatestUploads",
117-
);
118-
continue;
119-
}
120-
121-
await (channelObj as TextChannel).send({
122-
content:
123-
guild.guild_ping_role && channelInfo
124-
? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
125-
: guild.guild_ping_role
126-
? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
127-
: channelInfo
128-
? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
129-
: `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
130-
});
131-
} catch (error) {
132-
console.error(
133-
"Error fetching or sending message to channel in fetchLatestUploads:",
134-
error,
135-
);
136-
}
110+
updates.set(videoId, {
111+
channelInfo,
112+
discordGuildsToUpdate,
113+
});
114+
115+
// console.log("Discord guilds to update:", discordGuildsToUpdate);
116+
// for (const guild of discordGuildsToUpdate) {
117+
// try {
118+
// const channelObj = await client.channels.fetch(
119+
// guild.guild_channel_id,
120+
// );
121+
122+
// if (
123+
// !channelObj ||
124+
// (channelObj.type !== ChannelType.GuildText &&
125+
// channelObj.type !==
126+
// ChannelType.GuildAnnouncement)
127+
// ) {
128+
// console.error(
129+
// "Invalid channel or not a text channel in fetchLatestUploads",
130+
// );
131+
// continue;
132+
// }
133+
134+
// await (channelObj as TextChannel).send({
135+
// content:
136+
// guild.guild_ping_role && channelInfo
137+
// ? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
138+
// : guild.guild_ping_role
139+
// ? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
140+
// : channelInfo
141+
// ? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
142+
// : `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
143+
// });
144+
// } catch (error) {
145+
// console.error(
146+
// "Error fetching or sending message to channel in fetchLatestUploads:",
147+
// error,
148+
// );
149+
// }
150+
// }
151+
}
152+
}
153+
}
154+
}
155+
156+
export async function sendLatestUploads() {
157+
for (const [videoId, update] of updates.entries()) {
158+
const channelInfo = update.channelInfo;
159+
const discordGuildsToUpdate = update.discordGuildsToUpdate;
160+
161+
console.log("Discord guilds to update:", discordGuildsToUpdate);
162+
for (const guild of discordGuildsToUpdate) {
163+
try {
164+
const channelObj = await client.channels.fetch(
165+
guild.guild_channel_id,
166+
);
167+
168+
if (
169+
!channelObj ||
170+
(channelObj.type !== ChannelType.GuildText &&
171+
channelObj.type !== ChannelType.GuildAnnouncement)
172+
) {
173+
console.error(
174+
"Invalid channel or not a text channel in fetchLatestUploads",
175+
);
176+
continue;
137177
}
178+
179+
await (channelObj as TextChannel).send({
180+
content:
181+
guild.guild_ping_role && channelInfo
182+
? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
183+
: guild.guild_ping_role
184+
? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
185+
: channelInfo
186+
? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
187+
: `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
188+
});
189+
} catch (error) {
190+
console.error(
191+
"Error fetching or sending message to channel in fetchLatestUploads:",
192+
error,
193+
);
138194
}
139195
}
140196
}

0 commit comments

Comments
 (0)