Skip to content

Commit 0417109

Browse files
committed
feat(bot): add autocomplete to /untrack
1 parent 610d915 commit 0417109

File tree

2 files changed

+94
-23
lines changed

2 files changed

+94
-23
lines changed

src/commands.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import search from "./utils/youtube/search";
2626
import {
2727
checkIfGuildIsTrackingUserAlready,
2828
discordAddGuildTrackingUser,
29+
discordGetAllTrackedInGuild,
2930
} from "./db/discord";
3031
import { Platform, YouTubeContentType } from "./types/types.d";
3132
import searchTwitch from "./utils/twitch/searchTwitch";
@@ -756,28 +757,14 @@ const commands: Record<string, Command> = {
756757
untrack: {
757758
data: {
758759
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-
},
775760
{
776761
name: "user_id",
762+
// TODO: Searching
777763
description:
778-
"Enter the YouTube/Twitch channel ID to stop tracking",
764+
"Select the channel or streamer to stop tracking. Searching is not supported, use the above options!",
779765
type: 3,
780766
required: true,
767+
autocomplete: true,
781768
},
782769
],
783770
name: "untrack",
@@ -924,6 +911,44 @@ const commands: Record<string, Command> = {
924911
return;
925912
}
926913
},
914+
autoComplete: async (interaction: AutocompleteInteraction) => {
915+
const trackedChannels = await discordGetAllTrackedInGuild(
916+
interaction.guildId as string,
917+
);
918+
919+
console.dir(
920+
{ message: "Tracked channels:", data: trackedChannels },
921+
{ depth: null },
922+
);
923+
924+
if (!trackedChannels || !trackedChannels.success) {
925+
console.error(
926+
"An error occurred while trying to get the tracked channels in this guild!",
927+
);
928+
await interaction.respond([]);
929+
930+
return;
931+
}
932+
933+
const trackedYouTubeChannels =
934+
trackedChannels.data.youtubeSubscriptions;
935+
const trackedTwitchChannels =
936+
trackedChannels.data.twitchSubscriptions;
937+
938+
return await interaction.respond(
939+
trackedYouTubeChannels
940+
.map((channel) => ({
941+
name: `YouTube: ${channel.youtubeChannel.youtubeChannelName} (${channel.youtubeChannel.youtubeChannelId}) | <#${channel.subscription.notificationChannelId}>`,
942+
value: String(channel.subscription.id),
943+
}))
944+
.concat(
945+
trackedTwitchChannels.map((channel) => ({
946+
name: `Twitch: ${channel.twitchChannel.twitchChannelName} (${channel.twitchChannel.twitchChannelId}) | <#${channel.subscription.notificationChannelId}>`,
947+
value: String(channel.subscription.id),
948+
})),
949+
),
950+
);
951+
},
927952
},
928953
tracked: {
929954
data: {

src/db/discord.ts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
dbGuildYouTubeSubscriptionsTable,
88
dbGuildTwitchSubscriptionsTable,
99
dbDiscordTable,
10+
dbYouTubeTable,
11+
dbTwitchTable,
1012
} from "./schema";
1113

1214
export async function checkIfGuildIsTrackingUserAlready(
@@ -199,22 +201,66 @@ export async function discordGetAllTrackedInGuild(guildId: string): Promise<
199201
| {
200202
success: true;
201203
data: {
202-
youtubeSubscriptions: (typeof dbGuildYouTubeSubscriptionsTable.$inferSelect)[];
203-
twitchSubscriptions: (typeof dbGuildTwitchSubscriptionsTable.$inferSelect)[];
204+
youtubeSubscriptions: {
205+
subscription: typeof dbGuildYouTubeSubscriptionsTable.$inferSelect;
206+
youtubeChannel: typeof dbYouTubeTable.$inferSelect;
207+
discord: typeof dbDiscordTable.$inferSelect;
208+
}[];
209+
twitchSubscriptions: {
210+
subscription: typeof dbGuildTwitchSubscriptionsTable.$inferSelect;
211+
twitchChannel: typeof dbTwitchTable.$inferSelect;
212+
discord: typeof dbDiscordTable.$inferSelect;
213+
}[];
204214
};
205215
}
206216
| { success: false; data: null }
207217
> {
208218
try {
209219
const youtubeSubscriptions = await db
210-
.select()
220+
.select({
221+
subscription: dbGuildYouTubeSubscriptionsTable,
222+
youtubeChannel: dbYouTubeTable,
223+
discord: dbDiscordTable,
224+
})
211225
.from(dbGuildYouTubeSubscriptionsTable)
212-
.where(eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId));
226+
.where(eq(dbGuildYouTubeSubscriptionsTable.guildId, guildId))
227+
.innerJoin(
228+
dbYouTubeTable,
229+
eq(
230+
dbGuildYouTubeSubscriptionsTable.youtubeChannelId,
231+
dbYouTubeTable.youtubeChannelId,
232+
),
233+
)
234+
.innerJoin(
235+
dbDiscordTable,
236+
eq(
237+
dbGuildYouTubeSubscriptionsTable.guildId,
238+
dbDiscordTable.guildId,
239+
),
240+
);
213241

214242
const twitchSubscriptions = await db
215-
.select()
243+
.select({
244+
subscription: dbGuildTwitchSubscriptionsTable,
245+
twitchChannel: dbTwitchTable,
246+
discord: dbDiscordTable,
247+
})
216248
.from(dbGuildTwitchSubscriptionsTable)
217-
.where(eq(dbGuildTwitchSubscriptionsTable.guildId, guildId));
249+
.where(eq(dbGuildTwitchSubscriptionsTable.guildId, guildId))
250+
.innerJoin(
251+
dbTwitchTable,
252+
eq(
253+
dbGuildTwitchSubscriptionsTable.twitchChannelId,
254+
dbTwitchTable.twitchChannelId,
255+
),
256+
)
257+
.innerJoin(
258+
dbDiscordTable,
259+
eq(
260+
dbGuildTwitchSubscriptionsTable.guildId,
261+
dbDiscordTable.guildId,
262+
),
263+
);
218264

219265
return {
220266
success: true,

0 commit comments

Comments
 (0)