Skip to content

Commit 59be0bb

Browse files
authored
Merge pull request #148 from GalvinPython/fix/untrack
2 parents 610d915 + 7a768f8 commit 59be0bb

File tree

2 files changed

+161
-145
lines changed

2 files changed

+161
-145
lines changed

src/commands.ts

Lines changed: 88 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import search from "./utils/youtube/search";
2626
import {
2727
checkIfGuildIsTrackingUserAlready,
2828
discordAddGuildTrackingUser,
29+
discordGetAllTrackedInGuild,
30+
discordRemoveGuildTrackingChannel,
2931
} from "./db/discord";
3032
import { Platform, YouTubeContentType } from "./types/types.d";
3133
import searchTwitch from "./utils/twitch/searchTwitch";
@@ -476,10 +478,25 @@ const commands: Record<string, Command> = {
476478
trackedChannels.data
477479
) {
478480
// If the channel is already being tracked in the guild, we can just return
479-
await interaction.reply({
480-
flags: MessageFlags.Ephemeral,
481-
content: `This channel is already being tracked in ${trackedChannels.data.map((channel, index) => `${index > 0 && index === trackedChannels.data.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
482-
});
481+
if (Array.isArray(trackedChannels.data)) {
482+
const channelList = trackedChannels.data
483+
.map(
484+
(channel, index, arr) =>
485+
`${index > 0 && index === arr.length - 1 ? "and " : ""}<#${channel.notificationChannelId}>`,
486+
)
487+
.join(", ");
488+
489+
await interaction.reply({
490+
flags: MessageFlags.Ephemeral,
491+
content: `This channel is already being tracked in ${channelList}!`,
492+
});
493+
} else {
494+
await interaction.reply({
495+
flags: MessageFlags.Ephemeral,
496+
content:
497+
"This channel is already being tracked, but the data format is invalid.",
498+
});
499+
}
483500

484501
return;
485502
}
@@ -589,10 +606,25 @@ const commands: Record<string, Command> = {
589606
trackedChannels.data
590607
) {
591608
// If the channel is already being tracked in the guild, we can just return
592-
await interaction.reply({
593-
flags: MessageFlags.Ephemeral,
594-
content: `This channel is already being tracked in ${trackedChannels.data.map((channel, index) => `${index > 0 && index === trackedChannels.data.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
595-
});
609+
if (Array.isArray(trackedChannels.data)) {
610+
const channelList = trackedChannels.data
611+
.map(
612+
(channel, index, arr) =>
613+
`${index > 0 && index === arr.length - 1 ? "and " : ""}<#${channel.notificationChannelId}>`,
614+
)
615+
.join(", ");
616+
617+
await interaction.reply({
618+
flags: MessageFlags.Ephemeral,
619+
content: `This channel is already being tracked in ${channelList}!`,
620+
});
621+
} else {
622+
await interaction.reply({
623+
flags: MessageFlags.Ephemeral,
624+
content:
625+
"This channel is already being tracked, but the data format is invalid.",
626+
});
627+
}
596628

597629
return;
598630
}
@@ -624,6 +656,7 @@ const commands: Record<string, Command> = {
624656
const channelAdded = await addNewStreamerToTrack(
625657
platformUserId,
626658
isLive,
659+
streamerName,
627660
);
628661

629662
if (!channelAdded.success) {
@@ -756,28 +789,14 @@ const commands: Record<string, Command> = {
756789
untrack: {
757790
data: {
758791
options: [
759-
{
760-
name: "platform",
761-
description: "Select a supported platform to track",
762-
type: 3,
763-
required: true,
764-
choices: [
765-
{
766-
name: "Twitch",
767-
value: "twitch",
768-
},
769-
{
770-
name: "YouTube",
771-
value: "youtube",
772-
},
773-
],
774-
},
775792
{
776793
name: "user_id",
794+
// TODO: Searching
777795
description:
778-
"Enter the YouTube/Twitch channel ID to stop tracking",
796+
"Select the channel or streamer to stop tracking. Searching is not supported, use the above options!",
779797
type: 3,
780798
required: true,
799+
autocomplete: true,
781800
},
782801
],
783802
name: "untrack",
@@ -787,9 +806,7 @@ const commands: Record<string, Command> = {
787806
},
788807
execute: async (interaction: CommandInteraction) => {
789808
// Get the YouTube Channel ID
790-
const youtubeChannelId = interaction.options.get("user_id")
791-
?.value as string;
792-
const platform = interaction.options.get("platform")
809+
const platformUserId = interaction.options.get("user_id")
793810
?.value as string;
794811
const guildId = interaction.guildId;
795812

@@ -819,110 +836,60 @@ const commands: Record<string, Command> = {
819836
return;
820837
}
821838

822-
// Platform check (to shut up TS)
823-
if (platform != "youtube" && platform != "twitch") {
839+
// Remove the guild from the database
840+
const trackingDeleteSuccess =
841+
await discordRemoveGuildTrackingChannel(platformUserId);
842+
843+
if (!trackingDeleteSuccess || !trackingDeleteSuccess.success) {
824844
await interaction.reply({
825845
flags: MessageFlags.Ephemeral,
826-
content:
827-
"Platform not supported! Please select a platform to track!",
846+
content: "Failed to stop tracking the channel.",
828847
});
829848

830849
return;
831850
}
832851

833-
// Remove the guild from the database
834-
switch (platform) {
835-
case "youtube":
836-
// Check if the channel is not being tracked in the guild
837-
if (
838-
!(await checkIfGuildIsTrackingUserAlready(
839-
youtubeChannelId,
840-
guildId,
841-
))
842-
) {
843-
await interaction.reply({
844-
flags: MessageFlags.Ephemeral,
845-
content:
846-
"This channel is not being tracked in this guild!",
847-
});
848-
849-
return;
850-
}
851-
if (
852-
await stopGuildTrackingChannel(
853-
guildId,
854-
youtubeChannelId,
855-
)
856-
) {
857-
await interaction.reply({
858-
flags: MessageFlags.Ephemeral,
859-
content:
860-
"Successfully stopped tracking the channel!",
861-
});
862-
} else {
863-
await interaction.reply({
864-
flags: MessageFlags.Ephemeral,
865-
content:
866-
"An error occurred while trying to stop tracking the channel! Please report this error!",
867-
});
868-
}
869-
870-
return;
871-
case "twitch": {
872-
// get the twitch id for the streamer
873-
const platformUserId =
874-
await getplatformUserId(youtubeChannelId);
875-
876-
if (!platformUserId) {
877-
await interaction.reply({
878-
flags: MessageFlags.Ephemeral,
879-
content:
880-
"An error occurred while trying to get the streamer ID! Please report this error!",
881-
});
882-
883-
return;
884-
}
885-
886-
// check if the channel is not being tracked in the guild
887-
if (
888-
!(await checkIfGuildIsTrackingUserAlready(
889-
platformUserId,
890-
guildId,
891-
))
892-
) {
893-
await interaction.reply({
894-
flags: MessageFlags.Ephemeral,
895-
content:
896-
"This streamer is not being tracked in this guild!",
897-
});
852+
await interaction.reply({
853+
content: "Successfully stopped tracking the channel.",
854+
});
855+
},
856+
autoComplete: async (interaction: AutocompleteInteraction) => {
857+
const trackedChannels = await discordGetAllTrackedInGuild(
858+
interaction.guildId as string,
859+
);
898860

899-
return;
900-
}
861+
console.dir(
862+
{ message: "Tracked channels:", data: trackedChannels },
863+
{ depth: null },
864+
);
901865

902-
if (
903-
await twitchStopGuildTrackingChannel(
904-
guildId,
905-
platformUserId,
906-
)
907-
) {
908-
await interaction.reply({
909-
flags: MessageFlags.Ephemeral,
910-
content:
911-
"Successfully stopped tracking the streamer!",
912-
});
913-
} else {
914-
await interaction.reply({
915-
flags: MessageFlags.Ephemeral,
916-
content:
917-
"An error occurred while trying to stop tracking the streamer! Please report this error!",
918-
});
919-
}
866+
if (!trackedChannels || !trackedChannels.success) {
867+
console.error(
868+
"An error occurred while trying to get the tracked channels in this guild!",
869+
);
870+
await interaction.respond([]);
920871

921-
return;
922-
}
923-
default:
924-
return;
872+
return;
925873
}
874+
875+
const trackedYouTubeChannels =
876+
trackedChannels.data.youtubeSubscriptions;
877+
const trackedTwitchChannels =
878+
trackedChannels.data.twitchSubscriptions;
879+
880+
return await interaction.respond(
881+
trackedYouTubeChannels
882+
.map((channel) => ({
883+
name: `YouTube: ${channel.youtubeChannel.youtubeChannelName} (${channel.youtubeChannel.youtubeChannelId}) | <#${channel.subscription.notificationChannelId}>`,
884+
value: `youtube.${String(channel.subscription.id)}`,
885+
}))
886+
.concat(
887+
trackedTwitchChannels.map((channel) => ({
888+
name: `Twitch: ${channel.twitchChannel.twitchChannelName} (${channel.twitchChannel.twitchChannelId}) | <#${channel.subscription.notificationChannelId}>`,
889+
value: `twitch.${String(channel.subscription.id)}`,
890+
})),
891+
),
892+
);
926893
},
927894
},
928895
tracked: {

0 commit comments

Comments
 (0)