@@ -2,6 +2,12 @@ import type { TextChannel } from "discord.js";
22
33import { env } from "../../config" ;
44import client from "../.." ;
5+ import {
6+ dbTwitchGetAllChannelsToTrack ,
7+ twitchUpdateIsLive ,
8+ } from "../../db/twitch" ;
9+ import { discordGetAllGuildsTrackingChannel } from "../../db/discord" ;
10+ import { Platform } from "../../types/types.d" ;
511
612import { twitchToken } from "./auth" ;
713import { getStreamerName } from "./getStreamerName" ;
@@ -48,22 +54,19 @@ export async function checkIfStreamersAreLive(): Promise<void> {
4854 return ;
4955 }
5056
51- const allStreamerIds = await twitchGetAllChannelsToTrack ( ) ;
57+ const allStreamerIds = await dbTwitchGetAllChannelsToTrack ( ) ;
5258 const chunkSize = 100 ;
5359 const chunks = [ ] ;
5460
55- for ( let i = 0 ; i < allStreamerIds . length ; i += chunkSize ) {
56- const chunk = allStreamerIds . slice ( i , i + chunkSize ) ;
61+ for ( let i = 0 ; i < allStreamerIds . data . length ; i += chunkSize ) {
62+ const chunk = allStreamerIds . data . slice ( i , i + chunkSize ) ;
5763
5864 chunks . push ( chunk ) ;
5965 }
6066
6167 for ( const chunk of chunks ) {
6268 const urlQueries = chunk
63- . map (
64- ( streamerId : dbTwitch ) =>
65- `user_id=${ streamerId . twitch_channel_id } ` ,
66- )
69+ . map ( ( streamerId ) => `user_id=${ streamerId . twitchChannelId } ` )
6770 . join ( "&" ) ;
6871 const res = await fetch (
6972 `https://api.twitch.tv/helix/streams?${ urlQueries } ` ,
@@ -89,51 +92,53 @@ export async function checkIfStreamersAreLive(): Promise<void> {
8992
9093 for ( const streamerId of chunk ) {
9194 const isLive = allLiveStreamers . includes (
92- streamerId . twitch_channel_id ,
95+ streamerId . twitchChannelId ,
9396 ) ;
94- const needsUpdate = isLive !== Boolean ( streamerId . is_live ) ;
97+ const needsUpdate =
98+ isLive !== Boolean ( streamerId . twitchChannelIsLive ) ;
9599
96100 console . log (
97- `[Twitch] ${ streamerId . twitch_channel_id } is live:` ,
101+ `[Twitch] ${ streamerId . twitchChannelId } is live:` ,
98102 isLive ,
99103 ". Was live:" ,
100- Boolean ( streamerId . is_live ) ,
104+ Boolean ( streamerId . twitchChannelIsLive ) ,
101105 ". Needs update:" ,
102106 needsUpdate ,
103107 ) ;
104108
105109 if ( needsUpdate ) {
106110 // Update the database
107111 console . log (
108- `Updating ${ streamerId . twitch_channel_id } to be ${ isLive ? "live" : "offline" } ` ,
112+ `Updating ${ streamerId . twitchChannelId } to be ${ isLive ? "live" : "offline" } ` ,
109113 ) ;
110- await twitchUpdateIsLive ( streamerId . twitch_channel_id , isLive ) ;
114+ await twitchUpdateIsLive ( streamerId . twitchChannelId , isLive ) ;
111115
112116 if ( isLive ) {
113117 // Get the streamer's name
114118 const streamerName = await getStreamerName (
115- streamerId . twitch_channel_id ,
119+ streamerId . twitchChannelId ,
116120 ) ;
117121
118122 // Get all guilds that are tracking this streamer
119123 const guildsTrackingStreamer =
120- await twitchGetGuildsTrackingChannel (
121- streamerId . twitch_channel_id ,
124+ await discordGetAllGuildsTrackingChannel (
125+ Platform . Twitch ,
126+ streamerId . twitchChannelId ,
122127 ) ;
123128
124- for ( const guild of guildsTrackingStreamer ) {
129+ for ( const guild of guildsTrackingStreamer . data ) {
125130 // Send a message to the channel
126131 const channel = await client . channels . fetch (
127- guild . guild_channel_id ,
132+ guild . guildId ,
128133 ) ;
129134
130135 await ( channel as TextChannel ) . send (
131- `${ guild . guild_ping_role ? `<@&${ guild . guild_ping_role } >` : "" } ${ streamerName } is now live <https://twitch.tv/${ streamerName } >!` ,
136+ `${ guild . notificationRoleId ? `<@&${ guild . notificationRoleId } >` : "" } ${ streamerName } is now live <https://twitch.tv/${ streamerName } >!` ,
132137 ) ;
133138 }
134139 } else {
135140 console . log (
136- `[Twitch] ${ streamerId . twitch_channel_id } is offline!` ,
141+ `[Twitch] ${ streamerId . twitchChannelId } is offline!` ,
137142 ) ;
138143 }
139144 }
0 commit comments