@@ -2,7 +2,7 @@ import { heapStats } from 'bun:jsc';
22import client from '.' ;
33import { ChannelType , GuildMember , type CommandInteraction } from 'discord.js' ;
44import checkIfChannelIdIsValid from './utils/youtube/checkIfChannelIdIsValid' ;
5- import { addNewChannelToTrack , addNewGuildToTrackChannel , checkIfChannelIsAlreadyTracked , checkIfGuildIsTrackingChannelAlready , stopGuildTrackingChannel , twitchAddNewChannelToTrack , twitchAddNewGuildToTrackChannel , twitchCheckIfChannelIsAlreadyTracked , twitchCheckIfGuildIsTrackingChannelAlready } from './database' ;
5+ import { addNewChannelToTrack , addNewGuildToTrackChannel , checkIfChannelIsAlreadyTracked , checkIfGuildIsTrackingChannelAlready , stopGuildTrackingChannel , twitchAddNewChannelToTrack , twitchAddNewGuildToTrackChannel , twitchCheckIfChannelIsAlreadyTracked , twitchCheckIfGuildIsTrackingChannelAlready , twitchStopGuildTrackingChannel } from './database' ;
66import getChannelDetails from './utils/youtube/getChannelDetails' ;
77import { PermissionFlagsBits } from 'discord-api-types/v8' ;
88import { getStreamerId } from './utils/twitch/getStreamerId' ;
@@ -231,7 +231,7 @@ const commands: Record<string, Command> = {
231231 if ( platformUserId . length != 24 || ! platformUserId . startsWith ( 'UC' ) ) {
232232 await interaction . reply ( {
233233 ephemeral : true ,
234- content : 'Invalid YouTube channel ID format! Each channel ID should be 24 characters long and start with "UC".' ,
234+ content : 'Invalid YouTube channel ID format! Each channel ID should be 24 characters long and start with "UC". Handles are currently not supported. ' ,
235235 } ) ;
236236 return ;
237237 }
@@ -292,7 +292,7 @@ const commands: Record<string, Command> = {
292292 }
293293
294294 // Check if the channel is already being tracked in the guild
295- if ( await twitchCheckIfGuildIsTrackingChannelAlready ( platformUserId , guildId ) ) {
295+ if ( await twitchCheckIfGuildIsTrackingChannelAlready ( streamerId , guildId ) ) {
296296 await interaction . reply ( {
297297 ephemeral : true ,
298298 content : 'This streamer is already being tracked!' ,
@@ -301,9 +301,9 @@ const commands: Record<string, Command> = {
301301 }
302302
303303 // Check if the channel is already being tracked globally
304- if ( ! await twitchCheckIfChannelIsAlreadyTracked ( platformUserId ) ) {
304+ if ( ! await twitchCheckIfChannelIsAlreadyTracked ( streamerId ) ) {
305305 const isLive = await checkIfStreamerIsLive ( streamerId ) ;
306- if ( ! await twitchAddNewChannelToTrack ( platformUserId , isLive ) ) {
306+ if ( ! await twitchAddNewChannelToTrack ( streamerId , isLive ) ) {
307307 await interaction . reply ( {
308308 ephemeral : true ,
309309 content : 'An error occurred while trying to add the streamer to track! This is a new streamer being tracked globally, please report this error!' ,
@@ -313,10 +313,10 @@ const commands: Record<string, Command> = {
313313 }
314314
315315 // Add the guild to the database
316- if ( await twitchAddNewGuildToTrackChannel ( guildId , platformUserId , discordChannelId , interaction . options . get ( 'role' ) ?. value as string ?? null ) ) {
316+ if ( await twitchAddNewGuildToTrackChannel ( guildId , streamerId , discordChannelId , interaction . options . get ( 'role' ) ?. value as string ?? null ) ) {
317317 await interaction . reply ( {
318318 ephemeral : true ,
319- content : `Started tracking the streamer ${ platformUserId } in ${ targetChannel . name } !` ,
319+ content : `Started tracking the streamer ${ platformUserId } ( ${ streamerId } ) in ${ targetChannel . name } !` ,
320320 } ) ;
321321 } else {
322322 await interaction . reply ( {
@@ -333,67 +333,118 @@ const commands: Record<string, Command> = {
333333 } ,
334334 untrack : {
335335 data : {
336- options : [ {
337- name : 'youtube_channel' ,
338- description : 'Enter the YouTube channel ID to stop tracking' ,
339- type : 3 ,
340- required : true ,
341- } ] ,
336+ options : [
337+ {
338+ name : 'platform' ,
339+ description : 'Select a supported platform to track' ,
340+ type : 3 ,
341+ required : true ,
342+ choices : [
343+ {
344+ name : 'Twitch' ,
345+ value : 'twitch' ,
346+ } ,
347+ {
348+ name : 'YouTube' ,
349+ value : 'youtube' ,
350+ } ,
351+ ]
352+ } ,
353+ {
354+ name : 'user_id' ,
355+ description : 'Enter the YouTube/Twitch channel ID to stop tracking' ,
356+ type : 3 ,
357+ required : true ,
358+ }
359+ ] ,
342360 name : 'untrack' ,
343361 description : 'Stop a channel from being tracked in this guild!' ,
344362 integration_types : [ 0 , 1 ] ,
345363 contexts : [ 0 , 1 , 2 ] ,
346364 } ,
347365 execute : async ( interaction : CommandInteraction ) => {
348366 // Get the YouTube Channel ID
349- const youtubeChannelId = interaction . options . get ( 'youtube_channel' ) ?. value as string ;
367+ const youtubeChannelId = interaction . options . get ( 'user_id' ) ?. value as string ;
368+ const platform = interaction . options . get ( 'platform' ) ?. value as string ;
350369 const guildId = interaction . guildId ;
351370
352- // Deferring the reply is not the best practice,
353- // but in case the network/database is slow, it's better to defer the reply
354- // so we don't get a timeout error
355- await interaction . deferReply ( ) ;
356-
357371 // DMs are currently not supported, so throw back an error
358372 if ( ! guildId || interaction . channel ?. isDMBased ( ) ) {
359- await interaction . followUp ( {
373+ await interaction . reply ( {
360374 ephemeral : true ,
361375 content : 'This command is not supported in DMs currently!\nNot a DM? Then an error has occurred :(' ,
362376 } ) ;
363377 return ;
364378 }
365379
366- // First check the permissions of the user
380+ // Check the permissions of the user
367381 if ( ! interaction . memberPermissions ?. has ( PermissionFlagsBits . ManageChannels ) ) {
368- await interaction . followUp ( {
382+ await interaction . reply ( {
369383 ephemeral : true ,
370384 content : 'You do not have the permission to manage channels!' ,
371385 } ) ;
372386 return ;
373387 }
374388
375- // Check if the channel is already being tracked in the guild
376- if ( ! await checkIfGuildIsTrackingChannelAlready ( youtubeChannelId , guildId ) ) {
377- await interaction . followUp ( {
389+ // Platform check (to shut up TS)
390+ if ( platform != 'youtube' && platform != 'twitch' ) {
391+ await interaction . reply ( {
378392 ephemeral : true ,
379- content : 'This channel is not being tracked in this guild !' ,
393+ content : 'Platform not supported! Please select a platform to track !' ,
380394 } ) ;
381395 return ;
382396 }
383397
384- // Add the guild to the database
385- if ( await stopGuildTrackingChannel ( guildId , youtubeChannelId ) ) {
386- await interaction . followUp ( {
387- ephemeral : true ,
388- content : 'Successfully stopped tracking the channel!' ,
389- } ) ;
390- } else {
391- await interaction . followUp ( {
398+ // Check if the channel is not being tracked in the guild
399+ if ( ! await checkIfGuildIsTrackingChannelAlready ( youtubeChannelId , guildId ) ) {
400+ await interaction . reply ( {
392401 ephemeral : true ,
393- content : 'An error occurred while trying to stop tracking the channel! Please report this error !' ,
402+ content : 'This channel is not being tracked in this guild !' ,
394403 } ) ;
404+ return ;
405+ }
406+
407+ // Remove the guild from the database
408+ switch ( platform ) {
409+ case 'youtube' :
410+ if ( await stopGuildTrackingChannel ( guildId , youtubeChannelId ) ) {
411+ await interaction . reply ( {
412+ ephemeral : true ,
413+ content : 'Successfully stopped tracking the channel!' ,
414+ } ) ;
415+ } else {
416+ await interaction . reply ( {
417+ ephemeral : true ,
418+ content : 'An error occurred while trying to stop tracking the channel! Please report this error!' ,
419+ } ) ;
420+ }
421+ return ;
422+ case 'twitch' :
423+ // get the twitch id for the streamer
424+ const streamerId = await getStreamerId ( youtubeChannelId ) ;
425+ if ( ! streamerId ) {
426+ await interaction . reply ( {
427+ ephemeral : true ,
428+ content : 'An error occurred while trying to get the streamer ID! Please report this error!' ,
429+ } ) ;
430+ return ;
431+ }
432+
433+ if ( await twitchStopGuildTrackingChannel ( guildId , youtubeChannelId ) ) {
434+ await interaction . reply ( {
435+ ephemeral : true ,
436+ content : 'Successfully stopped tracking the streamer!' ,
437+ } ) ;
438+ } else {
439+ await interaction . reply ( {
440+ ephemeral : true ,
441+ content : 'An error occurred while trying to stop tracking the streamer! Please report this error!' ,
442+ } ) ;
443+ }
444+ return ;
445+ default :
446+ return ;
395447 }
396- return ;
397448 }
398449 }
399450} ;
0 commit comments