Skip to content

Commit bcd5c6b

Browse files
committed
feat(bot): complete /untrack command improvements
1 parent f2462f4 commit bcd5c6b

File tree

2 files changed

+62
-128
lines changed

2 files changed

+62
-128
lines changed

src/commands.ts

Lines changed: 51 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
checkIfGuildIsTrackingUserAlready,
2828
discordAddGuildTrackingUser,
2929
discordGetAllTrackedInGuild,
30+
discordRemoveGuildTrackingChannel,
3031
} from "./db/discord";
3132
import { Platform, YouTubeContentType } from "./types/types.d";
3233
import searchTwitch from "./utils/twitch/searchTwitch";
@@ -477,10 +478,25 @@ const commands: Record<string, Command> = {
477478
trackedChannels.data
478479
) {
479480
// If the channel is already being tracked in the guild, we can just return
480-
await interaction.reply({
481-
flags: MessageFlags.Ephemeral,
482-
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(", ")}!`,
483-
});
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+
}
484500

485501
return;
486502
}
@@ -590,10 +606,25 @@ const commands: Record<string, Command> = {
590606
trackedChannels.data
591607
) {
592608
// If the channel is already being tracked in the guild, we can just return
593-
await interaction.reply({
594-
flags: MessageFlags.Ephemeral,
595-
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(", ")}!`,
596-
});
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+
}
597628

598629
return;
599630
}
@@ -775,9 +806,7 @@ const commands: Record<string, Command> = {
775806
},
776807
execute: async (interaction: CommandInteraction) => {
777808
// Get the YouTube Channel ID
778-
const youtubeChannelId = interaction.options.get("user_id")
779-
?.value as string;
780-
const platform = interaction.options.get("platform")
809+
const platformUserId = interaction.options.get("user_id")
781810
?.value as string;
782811
const guildId = interaction.guildId;
783812

@@ -807,110 +836,22 @@ const commands: Record<string, Command> = {
807836
return;
808837
}
809838

810-
// Platform check (to shut up TS)
811-
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) {
812844
await interaction.reply({
813845
flags: MessageFlags.Ephemeral,
814-
content:
815-
"Platform not supported! Please select a platform to track!",
846+
content: "Failed to stop tracking the channel.",
816847
});
817848

818849
return;
819850
}
820851

821-
// Remove the guild from the database
822-
switch (platform) {
823-
case "youtube":
824-
// Check if the channel is not being tracked in the guild
825-
if (
826-
!(await checkIfGuildIsTrackingUserAlready(
827-
youtubeChannelId,
828-
guildId,
829-
))
830-
) {
831-
await interaction.reply({
832-
flags: MessageFlags.Ephemeral,
833-
content:
834-
"This channel is not being tracked in this guild!",
835-
});
836-
837-
return;
838-
}
839-
if (
840-
await stopGuildTrackingChannel(
841-
guildId,
842-
youtubeChannelId,
843-
)
844-
) {
845-
await interaction.reply({
846-
flags: MessageFlags.Ephemeral,
847-
content:
848-
"Successfully stopped tracking the channel!",
849-
});
850-
} else {
851-
await interaction.reply({
852-
flags: MessageFlags.Ephemeral,
853-
content:
854-
"An error occurred while trying to stop tracking the channel! Please report this error!",
855-
});
856-
}
857-
858-
return;
859-
case "twitch": {
860-
// get the twitch id for the streamer
861-
const platformUserId =
862-
await getplatformUserId(youtubeChannelId);
863-
864-
if (!platformUserId) {
865-
await interaction.reply({
866-
flags: MessageFlags.Ephemeral,
867-
content:
868-
"An error occurred while trying to get the streamer ID! Please report this error!",
869-
});
870-
871-
return;
872-
}
873-
874-
// check if the channel is not being tracked in the guild
875-
if (
876-
!(await checkIfGuildIsTrackingUserAlready(
877-
platformUserId,
878-
guildId,
879-
))
880-
) {
881-
await interaction.reply({
882-
flags: MessageFlags.Ephemeral,
883-
content:
884-
"This streamer is not being tracked in this guild!",
885-
});
886-
887-
return;
888-
}
889-
890-
if (
891-
await twitchStopGuildTrackingChannel(
892-
guildId,
893-
platformUserId,
894-
)
895-
) {
896-
await interaction.reply({
897-
flags: MessageFlags.Ephemeral,
898-
content:
899-
"Successfully stopped tracking the streamer!",
900-
});
901-
} else {
902-
await interaction.reply({
903-
flags: MessageFlags.Ephemeral,
904-
content:
905-
"An error occurred while trying to stop tracking the streamer! Please report this error!",
906-
});
907-
}
908-
909-
return;
910-
}
911-
default:
912-
return;
913-
}
852+
await interaction.reply({
853+
content: "Successfully stopped tracking the channel.",
854+
});
914855
},
915856
autoComplete: async (interaction: AutocompleteInteraction) => {
916857
const trackedChannels = await discordGetAllTrackedInGuild(
@@ -940,12 +881,12 @@ const commands: Record<string, Command> = {
940881
trackedYouTubeChannels
941882
.map((channel) => ({
942883
name: `YouTube: ${channel.youtubeChannel.youtubeChannelName} (${channel.youtubeChannel.youtubeChannelId}) | <#${channel.subscription.notificationChannelId}>`,
943-
value: String(channel.subscription.id),
884+
value: `youtube.${String(channel.subscription.id)}`,
944885
}))
945886
.concat(
946887
trackedTwitchChannels.map((channel) => ({
947888
name: `Twitch: ${channel.twitchChannel.twitchChannelName} (${channel.twitchChannel.twitchChannelId}) | <#${channel.subscription.notificationChannelId}>`,
948-
value: String(channel.subscription.id),
889+
value: `twitch.${String(channel.subscription.id)}`,
949890
})),
950891
),
951892
);

src/db/discord.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,38 +280,31 @@ export async function discordGetAllTrackedInGuild(guildId: string): Promise<
280280
}
281281

282282
// Remove tracking for a specific channel in a guild
283+
// TODO: Make it so that if the channel is no longer tracked by any guilds, it is removed from the db entirely
283284
export async function discordRemoveGuildTrackingChannel(
284-
guildId: string,
285-
platform: Platform,
286-
platformUserId: string,
285+
trackingId: string,
287286
): Promise<{ success: boolean; data: [] }> {
288-
console.log(
289-
`Removing guild ${guildId} tracking for user ${platformUserId} on platform ${platform}`,
290-
);
287+
console.log(`Removing tracking for ID: ${trackingId}`);
288+
289+
const [platform, platformTrackingId] = trackingId.split(".");
291290

292291
try {
293292
if (platform === Platform.YouTube) {
294293
await db
295294
.delete(dbGuildYouTubeSubscriptionsTable)
296295
.where(
297-
and(
298-
eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId),
299-
eq(
300-
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
301-
platformUserId,
302-
),
296+
eq(
297+
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
298+
platformTrackingId,
303299
),
304300
);
305301
} else if (platform === Platform.Twitch) {
306302
await db
307303
.delete(dbGuildTwitchSubscriptionsTable)
308304
.where(
309-
and(
310-
eq(dbGuildTwitchSubscriptionsTable.guildId, guildId),
311-
eq(
312-
dbGuildTwitchSubscriptionsTable.twitchChannelId,
313-
platformUserId,
314-
),
305+
eq(
306+
dbGuildTwitchSubscriptionsTable.twitchChannelId,
307+
platformTrackingId,
315308
),
316309
);
317310
} else {

0 commit comments

Comments
 (0)