|
1 | | -import type { dbYouTube } from "../../types/database"; |
| 1 | +import type { dbDiscordTable, dbYouTube } from "../../types/database"; |
2 | 2 |
|
3 | | -import { ChannelType, TextChannel } from "discord.js"; |
| 3 | +import { ChannelType, type TextChannel } from "discord.js"; |
4 | 4 |
|
5 | 5 | import { env } from "../../config"; |
6 | 6 | import { getGuildsTrackingChannel, updateVideoId } from "../database"; |
7 | | -import client from "../.."; |
8 | 7 | import { dbYouTubeGetAllChannelsToTrack } from "../db/youtube"; |
| 8 | +import client from "../.."; |
9 | 9 |
|
10 | 10 | import getChannelDetails from "./getChannelDetails"; |
11 | 11 |
|
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() { |
13 | 21 | console.log("Fetching latest uploads..."); |
14 | 22 |
|
15 | 23 | const channels: dbYouTube[] | [] = await dbYouTubeGetAllChannelsToTrack(); |
@@ -99,42 +107,90 @@ export default async function fetchLatestUploads() { |
99 | 107 |
|
100 | 108 | const channelInfo = await getChannelDetails(channelId); |
101 | 109 |
|
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; |
137 | 177 | } |
| 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 | + ); |
138 | 194 | } |
139 | 195 | } |
140 | 196 | } |
|
0 commit comments