Skip to content

Commit 93876c6

Browse files
committed
feat: twitch autocomplete
1 parent 9bbf2bd commit 93876c6

File tree

8 files changed

+193
-119
lines changed

8 files changed

+193
-119
lines changed

perftesting.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/commands.ts

Lines changed: 91 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type CommandInteraction,
1414
} from "discord.js";
1515
import { PermissionFlagsBits } from "discord-api-types/v8";
16+
import hfksdjfskfhsjdfhkasfdhksf from "hfksdjfskfhsjdfhkasfdhksf";
1617

1718
import checkIfChannelIdIsValid from "./utils/youtube/checkIfChannelIdIsValid";
1819
import {
@@ -24,7 +25,6 @@ import {
2425
twitchStopGuildTrackingChannel,
2526
} from "./utils/database";
2627
import getChannelDetails from "./utils/youtube/getChannelDetails";
27-
import { getStreamerId } from "./utils/twitch/getStreamerId";
2828
import { checkIfStreamerIsLive } from "./utils/twitch/checkIfStreamerIsLive";
2929
import {
3030
checkIfChannelIsAlreadyTracked,
@@ -36,7 +36,8 @@ import {
3636
discordAddGuildTrackingUser,
3737
} from "./utils/db/discord";
3838
import { Platform, YouTubeContentType } from "./types/types.d";
39-
import hfksdjfskfhsjdfhkasfdhksf from 'hfksdjfskfhsjdfhkasfdhksf'
39+
import searchTwitch from "./utils/twitch/searchTwitch";
40+
import { getStreamerName } from "./utils/twitch/getStreamerName";
4041

4142
import client from ".";
4243

@@ -226,7 +227,7 @@ const commands: Record<string, Command> = {
226227
],
227228
},
228229
{
229-
type: 1,
230+
type: ApplicationCommandOptionType.Subcommand,
230231
name: "twitch",
231232
description: "Track a Twitch streamer",
232233
options: [
@@ -558,12 +559,13 @@ const commands: Record<string, Command> = {
558559

559560
case "twitch": {
560561
// Check if the streamer exists by getting the ID
561-
const streamerId = await getStreamerId(platformUserId);
562+
const streamerName = await getStreamerName(platformUserId);
562563

563-
if (!streamerId) {
564+
if (!streamerName) {
564565
await interaction.reply({
565566
flags: MessageFlags.Ephemeral,
566-
content: "That streamer doesn't exist!",
567+
content:
568+
"That streamer doesn't exist! Please use the autocomplete to find the correct streamer ID as this uses IDs that are not publicly visible on the Twitch site!",
567569
});
568570

569571
return;
@@ -572,14 +574,28 @@ const commands: Record<string, Command> = {
572574
// Check if the channel is already being tracked in the guild
573575
const trackedChannels =
574576
await checkIfGuildIsTrackingUserAlready(
577+
Platform.YouTube,
575578
platformUserId,
576579
guildId,
577580
);
578581

579-
if (trackedChannels.length) {
582+
console.log(trackedChannels);
583+
if (!trackedChannels || !trackedChannels.success) {
584+
// TODO: Embed
585+
await interaction.reply({
586+
flags: MessageFlags.Ephemeral,
587+
content: `An error occurred while trying to check if the channel is already being tracked in this guild! Please report this error!`,
588+
});
589+
590+
return;
591+
} else if (
592+
trackedChannels.success &&
593+
trackedChannels.data
594+
) {
595+
// If the channel is already being tracked in the guild, we can just return
580596
await interaction.reply({
581597
flags: MessageFlags.Ephemeral,
582-
content: `This channel is already being tracked in ${trackedChannels.map((channel, index) => `${index > 0 && index === trackedChannels.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
598+
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(", ")}!`,
583599
});
584600

585601
return;
@@ -640,8 +656,15 @@ const commands: Record<string, Command> = {
640656
},
641657
autoComplete: async (interaction: AutocompleteInteraction) => {
642658
try {
643-
const platform = interaction.options.get("platform")?.value;
644-
const query = interaction.options.get("channel_id")?.value;
659+
const platform = (
660+
interaction as unknown as ChatInputCommandInteraction
661+
).options.getSubcommand();
662+
const query =
663+
platform === "youtube"
664+
? (interaction.options.get("channel_id")
665+
?.value as string)
666+
: (interaction.options.get("streamer_id")
667+
?.value as string);
645668

646669
// If the query is empty or not a string, return an empty array
647670
if (!query || typeof query !== "string") {
@@ -650,29 +673,67 @@ const commands: Record<string, Command> = {
650673
return;
651674
}
652675

653-
// If the query is a YouTube channel ID, do not search
654-
if (query.length == 24 && query.startsWith("UC")) {
655-
await interaction.respond([
656-
{
657-
name: `${query} (using channel id)`,
658-
value: query,
659-
},
660-
]);
676+
console.log(platform, query);
661677

662-
return;
663-
}
678+
switch (platform) {
679+
case "youtube": {
680+
// If the query is a YouTube channel ID, do not search
681+
if (query.length == 24 && query.startsWith("UC")) {
682+
await interaction.respond([
683+
{
684+
name: `${query} (using channel id)`,
685+
value: query,
686+
},
687+
]);
688+
689+
return;
690+
}
664691

665-
const channels = await search(query);
692+
const channels = await search(query);
666693

667-
await interaction.respond(
668-
channels.map((channel) => ({
669-
name: `${channel.title} (${channel.handle}) | ${channel.subscribers} subscriber(s)`.slice(
670-
0,
671-
100,
672-
),
673-
value: channel.channel_id,
674-
})),
675-
);
694+
if (!channels || channels.length === 0) {
695+
await interaction.respond([]);
696+
697+
return;
698+
}
699+
700+
await interaction.respond(
701+
channels.map((channel) => ({
702+
name: `${channel.title} (${channel.handle}) | ${channel.subscribers} subscriber(s)`.slice(
703+
0,
704+
100,
705+
),
706+
value: channel.channel_id,
707+
})),
708+
);
709+
710+
break;
711+
}
712+
case "twitch": {
713+
const channels = await searchTwitch(query);
714+
715+
if (!channels || channels.length === 0) {
716+
await interaction.respond([]);
717+
718+
return;
719+
}
720+
721+
await interaction.respond(
722+
channels.map((channel) => ({
723+
name: `${channel.displayName} (${channel.loginName}) | ${channel.isLive ? "🔴" : "Offline"}`.slice(
724+
0,
725+
100,
726+
),
727+
value: channel.id,
728+
})),
729+
);
730+
731+
break;
732+
}
733+
default:
734+
await interaction.respond([]);
735+
break;
736+
}
676737
} catch (error) {
677738
console.error(error);
678739
}

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ console.log(`Successfully reloaded ${data.length} application (/) commands.`);
5959

6060
// Check if Postgres is set up properly and its working
6161
if (!(await initTables())) {
62-
console.error("Whoops");
6362
// throw new Error("Error initializing tables");
6463
}
6564

src/types/twitch.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export interface TwitchChannelSearchResponse {
2+
data: Array<{
3+
broadcaster_language: string;
4+
broadcaster_login: string;
5+
display_name: string;
6+
game_id: string;
7+
game_name: string;
8+
id: string;
9+
is_live: string;
10+
tag_ids: Array<any>;
11+
tags: Array<string>;
12+
thumbnail_url: string;
13+
title: string;
14+
started_at: string;
15+
}>;
16+
pagination: {
17+
cursor: string;
18+
};
19+
}
20+
21+
export interface TwitchChannelSearchAutocompleteResponse {
22+
id: string;
23+
displayName: string;
24+
loginName: string;
25+
gameId: string;
26+
gameName: string;
27+
isLive: boolean;
28+
thumbnailUrl: string;
29+
title?: string;
30+
startedAt?: string;
31+
}

src/utils/db/init.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,17 @@ export default async function initTables(): Promise<boolean> {
131131

132132
// TODO: Fix the guild table
133133
const tempDropQuery = `
134+
DO $$
135+
BEGIN
136+
IF EXISTS (
137+
SELECT 1 FROM information_schema.table_constraints
138+
WHERE constraint_name = 'guild_youtube_subscriptions_guild_id_fkey'
139+
AND table_name = 'guild_youtube_subscriptions'
140+
) THEN
134141
ALTER TABLE guild_youtube_subscriptions
135142
DROP CONSTRAINT guild_youtube_subscriptions_guild_id_fkey;
143+
END IF;
144+
END$$;
136145
`;
137146

138147
try {

src/utils/db/twitch.ts

Whitespace-only changes.

src/utils/twitch/getStreamerId.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)