@@ -33,6 +33,8 @@ import search from "./utils/youtube/search";
3333import {
3434 checkIfGuildIsTrackingUserAlready ,
3535 discordAddGuildTrackingUser ,
36+ discordAddNewGuild ,
37+ discordCheckIfDmChannelExists ,
3638 discordGetAllTrackedInGuild ,
3739 discordRemoveGuildTrackingChannel ,
3840} from "./db/discord" ;
@@ -273,6 +275,8 @@ const commands: Record<string, Command> = {
273275 contexts : [ 0 , 1 ] ,
274276 } ,
275277 execute : async ( interaction : CommandInteraction ) => {
278+ const isDm = ! interaction . inGuild ( ) ;
279+
276280 // Get the YouTube Channel ID
277281 const targetPlatform = (
278282 interaction as ChatInputCommandInteraction
@@ -284,7 +288,7 @@ const commands: Record<string, Command> = {
284288 const discordChannelId =
285289 ( interaction . options . get ( "updates_channel" ) ?. value as string ) ??
286290 interaction . channelId ;
287- const guildId = interaction . guildId ;
291+ const guildId = isDm ? discordChannelId : interaction . guildId ;
288292
289293 // Log the autocomplete value
290294 console . log ( `Autocomplete value: ${ platformUserId } ` ) ;
@@ -316,33 +320,26 @@ const commands: Record<string, Command> = {
316320 return ;
317321 }
318322
319- // TODO: Enable DMs :)
320- const isDm = interaction . channel ?. isDMBased ( ) ;
323+ console . log ( interaction . channelId ) ;
321324
322- if ( ! guildId || isDm || isDm === undefined ) {
323- await interaction . reply ( {
324- flags : MessageFlags . Ephemeral ,
325- content :
326- "This command is not supported in DMs currently!\nNot a DM? Then the bot failed to get the guild info" ,
327- } ) ;
328-
329- return ;
330- }
325+ if ( isDm ) console . log ( "DM" ) ;
331326
332327 // TODO: Embed
333328 // Check the permissions of the user
334- if (
335- ! interaction . memberPermissions ?. has (
336- PermissionFlagsBits . ManageChannels ,
337- )
338- ) {
339- await interaction . reply ( {
340- flags : MessageFlags . Ephemeral ,
341- content :
342- "You do not have the permission to manage channels!" ,
343- } ) ;
329+ if ( ! isDm ) {
330+ if (
331+ ! interaction . memberPermissions ?. has (
332+ PermissionFlagsBits . ManageChannels ,
333+ )
334+ ) {
335+ await interaction . reply ( {
336+ flags : MessageFlags . Ephemeral ,
337+ content :
338+ "You do not have the permission to manage channels!" ,
339+ } ) ;
344340
345- return ;
341+ return ;
342+ }
346343 }
347344
348345 // TODO: Embed
@@ -397,15 +394,30 @@ const commands: Record<string, Command> = {
397394
398395 return ;
399396 }
397+ } else if ( isDm ) {
398+ // DM channels don't need permission checks
400399 } else {
401400 await interaction . reply ( {
402401 flags : MessageFlags . Ephemeral ,
403- content : " The target channel is not a text channel!" ,
402+ content : ` The target channel is not a text channel! ${ isDm } ` ,
404403 } ) ;
405404
406405 return ;
407406 }
408407
408+ // Before attempting to add the subscription, if it's a DM, check if it's already in the database. If not add it
409+ if ( isDm ) {
410+ console . log ( "CHECKING DM" ) ;
411+ const data = (
412+ await discordCheckIfDmChannelExists ( discordChannelId )
413+ ) . data ;
414+
415+ if ( ! data ) {
416+ console . log ( "ADDING DM" ) ;
417+ await discordAddNewGuild ( discordChannelId , true ) ;
418+ }
419+ }
420+
409421 switch ( targetPlatform ) {
410422 case "youtube" : {
411423 const contentType = interaction . options . get ( "content_type" )
@@ -571,7 +583,7 @@ const commands: Record<string, Command> = {
571583
572584 await interaction . reply ( {
573585 flags : MessageFlags . Ephemeral ,
574- content : `Started tracking the channel ${ youtubeChannelInfo ?. channelName ?? platformUserId } in ${ targetChannel . name } !` ,
586+ content : `Started tracking the channel ${ youtubeChannelInfo ?. channelName ?? platformUserId } in <# ${ targetChannel ?. id } > !` ,
575587 } ) ;
576588 } else {
577589 await interaction . reply ( {
@@ -698,7 +710,7 @@ const commands: Record<string, Command> = {
698710 ) {
699711 await interaction . reply ( {
700712 flags : MessageFlags . Ephemeral ,
701- content : `Started tracking the streamer ${ platformUserId } (${ platformUserId } ) in ${ targetChannel . name } !` ,
713+ content : `Started tracking the streamer ${ platformUserId } (${ platformUserId } ) in <# ${ targetChannel ?. id } > !` ,
702714 } ) ;
703715 } else {
704716 await interaction . reply ( {
@@ -819,24 +831,15 @@ const commands: Record<string, Command> = {
819831 contexts : [ 0 , 1 ] ,
820832 } ,
821833 execute : async ( interaction : CommandInteraction ) => {
834+ const isDm = ! interaction . inGuild ( ) ;
835+
822836 // Get the YouTube Channel ID
823837 const platformUserId = interaction . options . get ( "user_id" )
824838 ?. value as string ;
825- const guildId = interaction . guildId ;
826-
827- // DMs are currently not supported, so throw back an error
828- if ( ! guildId || interaction . channel ?. isDMBased ( ) ) {
829- await interaction . reply ( {
830- flags : MessageFlags . Ephemeral ,
831- content :
832- "This command is not supported in DMs currently!\nNot a DM? Then an error has occurred :(" ,
833- } ) ;
834-
835- return ;
836- }
837839
838840 // Check the permissions of the user
839841 if (
842+ ! isDm &&
840843 ! interaction . memberPermissions ?. has (
841844 PermissionFlagsBits . ManageChannels ,
842845 )
@@ -869,7 +872,7 @@ const commands: Record<string, Command> = {
869872 } ,
870873 autoComplete : async ( interaction : AutocompleteInteraction ) => {
871874 const trackedChannels = await discordGetAllTrackedInGuild (
872- interaction . guildId as string ,
875+ interaction . guildId ?? ( interaction . channelId as string ) ,
873876 ) ;
874877
875878 console . dir (
@@ -916,14 +919,16 @@ const commands: Record<string, Command> = {
916919 contexts : [ 0 , 1 ] ,
917920 } ,
918921 execute : async ( interaction : CommandInteraction ) => {
919- const guildId = interaction . guildId ;
920- const channelId = interaction . channelId ;
922+ let guildId = interaction . guildId ;
923+
924+ const isDm = ! interaction . inGuild ( ) ;
921925
922- if ( ! guildId || ! channelId ) {
926+ if ( isDm ) guildId = interaction . channelId ;
927+
928+ if ( ! guildId ) {
923929 await interaction . reply ( {
924930 flags : MessageFlags . Ephemeral ,
925- content :
926- "You are likely in a DM, this command is not supported in DMs!" ,
931+ content : "An error occurred! Please report" ,
927932 } ) ;
928933
929934 return ;
0 commit comments