Skip to content

Commit 411c998

Browse files
authored
Merge pull request #134 from ToastedDev/dev
2 parents c7b0597 + 397508e commit 411c998

File tree

3 files changed

+107
-45
lines changed

3 files changed

+107
-45
lines changed

src/events/ready.ts

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

44
import client from "../index";
5-
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
65
import { config } from "../config";
76
// import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
87
import { cronUpdateBotInfo } from "../utils/cronJobs";
8+
import sendLatestUploads from "../utils/youtube/sendLatestUploads";
9+
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
910

1011
// Log into the bot
1112
client.once(Events.ClientReady, async (bot) => {
1213
console.log(`Ready! Logged in as ${bot.user?.tag}`);
1314

14-
// Set the bot's presence and update it every minute
1515
await cronUpdateBotInfo();
16-
fetchLatestUploads();
17-
18-
// Set the bot's presence and update it every minute
1916
new CronJob("0 * * * * *", async () => {
2017
await cronUpdateBotInfo();
2118
}).start();
19+
20+
fetchLatestUploads();
2221
setInterval(fetchLatestUploads, config.updateIntervalYouTube as number);
2322

23+
sendLatestUploads();
24+
setInterval(sendLatestUploads, config.updateIntervalYouTube as number);
25+
2426
// One at a time
2527
// checkIfStreamersAreLive();
2628
// setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number);

src/utils/youtube/fetchLatestUploads.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import type { dbYouTube } from "../../types/database";
2-
3-
import { ChannelType, TextChannel } from "discord.js";
1+
import type { dbDiscordTable, dbYouTube } from "../../types/database";
42

53
import { env } from "../../config";
64
import { getGuildsTrackingChannel, updateVideoId } from "../database";
7-
import client from "../..";
85
import { dbYouTubeGetAllChannelsToTrack } from "../db/youtube";
96

107
import getChannelDetails from "./getChannelDetails";
118

9+
export const updates = new Map<
10+
string,
11+
{
12+
channelInfo: Awaited<ReturnType<typeof getChannelDetails>>;
13+
discordGuildsToUpdate: dbDiscordTable[];
14+
}
15+
>();
16+
1217
export default async function fetchLatestUploads() {
1318
console.log("Fetching latest uploads...");
1419

@@ -99,42 +104,47 @@ export default async function fetchLatestUploads() {
99104

100105
const channelInfo = await getChannelDetails(channelId);
101106

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-
}
137-
}
107+
updates.set(videoId, {
108+
channelInfo,
109+
discordGuildsToUpdate,
110+
});
111+
112+
// console.log("Discord guilds to update:", discordGuildsToUpdate);
113+
// for (const guild of discordGuildsToUpdate) {
114+
// try {
115+
// const channelObj = await client.channels.fetch(
116+
// guild.guild_channel_id,
117+
// );
118+
119+
// if (
120+
// !channelObj ||
121+
// (channelObj.type !== ChannelType.GuildText &&
122+
// channelObj.type !==
123+
// ChannelType.GuildAnnouncement)
124+
// ) {
125+
// console.error(
126+
// "Invalid channel or not a text channel in fetchLatestUploads",
127+
// );
128+
// continue;
129+
// }
130+
131+
// await (channelObj as TextChannel).send({
132+
// content:
133+
// guild.guild_ping_role && channelInfo
134+
// ? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
135+
// : guild.guild_ping_role
136+
// ? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
137+
// : channelInfo
138+
// ? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
139+
// : `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
140+
// });
141+
// } catch (error) {
142+
// console.error(
143+
// "Error fetching or sending message to channel in fetchLatestUploads:",
144+
// error,
145+
// );
146+
// }
147+
// }
138148
}
139149
}
140150
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { ChannelType, TextChannel } from "discord.js";
2+
3+
import client from "../..";
4+
5+
import { updates } from "./fetchLatestUploads";
6+
7+
export default async function sendLatestUploads() {
8+
for (const [videoId, update] of updates.entries()) {
9+
const channelInfo = update.channelInfo;
10+
const discordGuildsToUpdate = update.discordGuildsToUpdate;
11+
12+
console.log("Discord guilds to update:", discordGuildsToUpdate);
13+
for (const guild of discordGuildsToUpdate) {
14+
try {
15+
const channelObj = await client.channels.fetch(
16+
guild.guild_channel_id,
17+
);
18+
19+
if (
20+
!channelObj ||
21+
(channelObj.type !== ChannelType.GuildText &&
22+
channelObj.type !== ChannelType.GuildAnnouncement)
23+
) {
24+
console.error(
25+
"Invalid channel or not a text channel in fetchLatestUploads",
26+
);
27+
continue;
28+
}
29+
30+
await (channelObj as TextChannel).send({
31+
content:
32+
guild.guild_ping_role && channelInfo
33+
? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
34+
: guild.guild_ping_role
35+
? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
36+
: channelInfo
37+
? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
38+
: `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
39+
});
40+
} catch (error) {
41+
console.error(
42+
"Error fetching or sending message to channel in fetchLatestUploads:",
43+
error,
44+
);
45+
}
46+
}
47+
// Remove the processed entry from the updates map
48+
updates.delete(videoId);
49+
}
50+
}

0 commit comments

Comments
 (0)