Skip to content

Commit 144a8db

Browse files
committed
feat: /track twich updated
1 parent 93876c6 commit 144a8db

File tree

5 files changed

+158
-105
lines changed

5 files changed

+158
-105
lines changed

src/commands.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ import checkIfChannelIdIsValid from "./utils/youtube/checkIfChannelIdIsValid";
1919
import {
2020
getAllTrackedInGuild,
2121
stopGuildTrackingChannel,
22-
twitchAddNewChannelToTrack,
23-
twitchAddNewGuildToTrackChannel,
24-
twitchCheckIfChannelIsAlreadyTracked,
2522
twitchStopGuildTrackingChannel,
2623
} from "./utils/database";
2724
import getChannelDetails from "./utils/youtube/getChannelDetails";
@@ -38,6 +35,10 @@ import {
3835
import { Platform, YouTubeContentType } from "./types/types.d";
3936
import searchTwitch from "./utils/twitch/searchTwitch";
4037
import { getStreamerName } from "./utils/twitch/getStreamerName";
38+
import {
39+
addNewStreamerToTrack,
40+
checkIfStreamerIsAlreadyTracked,
41+
} from "./utils/db/twitch";
4142

4243
import client from ".";
4344

@@ -602,23 +603,39 @@ const commands: Record<string, Command> = {
602603
}
603604

604605
// Check if the channel is already being tracked globally
605-
if (
606-
!(await twitchCheckIfChannelIsAlreadyTracked(
607-
streamerId,
608-
))
606+
const isChannelTracked =
607+
await checkIfStreamerIsAlreadyTracked(platformUserId);
608+
609+
console.log(
610+
`Is channel ${platformUserId} tracked globally?`,
611+
isChannelTracked,
612+
);
613+
614+
if (!isChannelTracked.success) {
615+
await interaction.reply({
616+
flags: MessageFlags.Ephemeral,
617+
content:
618+
"An error occurred while trying to check if the channel is already being tracked globally! Please report this error!",
619+
});
620+
} else if (
621+
isChannelTracked.success &&
622+
isChannelTracked.data.length == 0
609623
) {
610-
const isLive = await checkIfStreamerIsLive(streamerId);
611-
612-
if (
613-
!(await twitchAddNewChannelToTrack(
614-
streamerId,
615-
isLive,
616-
))
617-
) {
624+
console.log(
625+
`Channel ${platformUserId} is not tracked globally, adding it now...`,
626+
);
627+
const isLive =
628+
await checkIfStreamerIsLive(platformUserId);
629+
const channelAdded = await addNewStreamerToTrack(
630+
platformUserId,
631+
isLive,
632+
);
633+
634+
if (!channelAdded.success) {
618635
await interaction.reply({
619636
flags: MessageFlags.Ephemeral,
620637
content:
621-
"An error occurred while trying to add the streamer to track! This is a new streamer being tracked globally, please report this error!",
638+
"An error occurred while trying to add the channel to track to the main YouTube database. Please report this issue!",
622639
});
623640

624641
return;
@@ -627,17 +644,19 @@ const commands: Record<string, Command> = {
627644

628645
// Add the guild to the database
629646
if (
630-
await twitchAddNewGuildToTrackChannel(
647+
await discordAddGuildTrackingUser(
631648
guildId,
632-
streamerId,
649+
Platform.Twitch,
650+
platformUserId,
633651
discordChannelId,
634652
(interaction.options.get("role")
635653
?.value as string) ?? null,
654+
isDm,
636655
)
637656
) {
638657
await interaction.reply({
639658
flags: MessageFlags.Ephemeral,
640-
content: `Started tracking the streamer ${platformUserId} (${streamerId}) in ${targetChannel.name}!`,
659+
content: `Started tracking the streamer ${platformUserId} (${platformUserId}) in ${targetChannel.name}!`,
641660
});
642661
} else {
643662
await interaction.reply({
@@ -662,9 +681,9 @@ const commands: Record<string, Command> = {
662681
const query =
663682
platform === "youtube"
664683
? (interaction.options.get("channel_id")
665-
?.value as string)
684+
?.value as string)
666685
: (interaction.options.get("streamer_id")
667-
?.value as string);
686+
?.value as string);
668687

669688
// If the query is empty or not a string, return an empty array
670689
if (!query || typeof query !== "string") {
@@ -856,9 +875,10 @@ const commands: Record<string, Command> = {
856875
return;
857876
case "twitch": {
858877
// get the twitch id for the streamer
859-
const streamerId = await getStreamerId(youtubeChannelId);
878+
const platformUserId =
879+
await getplatformUserId(youtubeChannelId);
860880

861-
if (!streamerId) {
881+
if (!platformUserId) {
862882
await interaction.reply({
863883
flags: MessageFlags.Ephemeral,
864884
content:
@@ -871,7 +891,7 @@ const commands: Record<string, Command> = {
871891
// check if the channel is not being tracked in the guild
872892
if (
873893
!(await checkIfGuildIsTrackingUserAlready(
874-
streamerId,
894+
platformUserId,
875895
guildId,
876896
))
877897
) {
@@ -887,7 +907,7 @@ const commands: Record<string, Command> = {
887907
if (
888908
await twitchStopGuildTrackingChannel(
889909
guildId,
890-
streamerId,
910+
platformUserId,
891911
)
892912
) {
893913
await interaction.reply({

src/utils/database.ts

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,9 @@ export const pool: Pool = new Pool({
2727
});
2828

2929
// #region YouTube
30-
export async function addNewGuildToTrackChannel(
31-
guild_id: string,
32-
channelId: string,
33-
guild_channel_id: string,
34-
guild_ping_role: string | null,
35-
) {
36-
const query = `INSERT INTO discord (guild_id, platform_user_id, guild_channel_id, guild_ping_role, guild_platform) VALUES (?, ?, ?, ?, 'youtube')`;
37-
38-
try {
39-
const statement = db.prepare(query);
40-
41-
statement.run(guild_id, channelId, guild_channel_id, guild_ping_role);
42-
43-
return true;
44-
} catch (err) {
45-
console.error("Error adding guild to track channel:", err);
46-
47-
return false;
48-
}
49-
}
50-
30+
/**
31+
* @deprecated This function is deprecated and being removed
32+
*/
5133
export async function getGuildsTrackingChannel(channelId: string) {
5234
const query = `SELECT * FROM discord WHERE platform_user_id = ?`;
5335

@@ -62,6 +44,9 @@ export async function getGuildsTrackingChannel(channelId: string) {
6244
}
6345
}
6446

47+
/**
48+
* @deprecated This function is deprecated and being removed
49+
*/
6550
export async function updateVideoId(channelId: string, videoId: string) {
6651
const query = `UPDATE youtube SET latest_video_id = ? WHERE youtube_channel_id = ?`;
6752

@@ -78,6 +63,9 @@ export async function updateVideoId(channelId: string, videoId: string) {
7863
}
7964
}
8065

66+
/**
67+
* @deprecated This function is deprecated and being removed
68+
*/
8169
export async function stopGuildTrackingChannel(
8270
guild_id: string,
8371
channelId: string,
@@ -99,6 +87,9 @@ export async function stopGuildTrackingChannel(
9987

10088
// #endregion
10189
// #region Twitch
90+
/**
91+
* @deprecated This function is deprecated and being removed
92+
*/
10293
export async function twitchGetAllChannelsToTrack() {
10394
const query = `SELECT * FROM twitch`;
10495

@@ -113,63 +104,9 @@ export async function twitchGetAllChannelsToTrack() {
113104
}
114105
}
115106

116-
export async function twitchCheckIfChannelIsAlreadyTracked(channelId: string) {
117-
const query = `SELECT * FROM twitch WHERE twitch_channel_id = ?`;
118-
119-
try {
120-
const statement = db.prepare(query);
121-
const result = statement.all(channelId);
122-
123-
return result.length > 0;
124-
} catch (err) {
125-
console.error(
126-
"Error checking if Twitch channel is already tracked:",
127-
err,
128-
);
129-
throw err;
130-
}
131-
}
132-
133-
export async function twitchAddNewChannelToTrack(
134-
channelId: string,
135-
isLive: boolean,
136-
) {
137-
const query = `INSERT INTO twitch (twitch_channel_id, is_live) VALUES (?, ?)`;
138-
139-
try {
140-
const statement = db.prepare(query);
141-
142-
statement.run(channelId, isLive);
143-
144-
return true;
145-
} catch (err) {
146-
console.error("Error adding Twitch channel to track:", err);
147-
148-
return false;
149-
}
150-
}
151-
152-
export async function twitchAddNewGuildToTrackChannel(
153-
guild_id: string,
154-
channelId: string,
155-
guild_channel_id: string,
156-
guild_ping_role: string | null,
157-
) {
158-
const query = `INSERT INTO discord (guild_id, platform_user_id, guild_channel_id, guild_ping_role, guild_platform) VALUES (?, ?, ?, ?, 'twitch')`;
159-
160-
try {
161-
const statement = db.prepare(query);
162-
163-
statement.run(guild_id, channelId, guild_channel_id, guild_ping_role);
164-
165-
return true;
166-
} catch (err) {
167-
console.error("Error adding guild to track Twitch channel:", err);
168-
169-
return false;
170-
}
171-
}
172-
107+
/**
108+
* @deprecated This function is deprecated and being removed
109+
*/
173110
export async function twitchGetGuildsTrackingChannel(channelId: string) {
174111
const query = `SELECT * FROM discord WHERE platform_user_id = ?`;
175112

@@ -184,6 +121,9 @@ export async function twitchGetGuildsTrackingChannel(channelId: string) {
184121
}
185122
}
186123

124+
/**
125+
* @deprecated This function is deprecated and being removed
126+
*/
187127
export async function twitchUpdateIsLive(channelId: string, isLive: boolean) {
188128
const query = `UPDATE twitch SET is_live = ? WHERE twitch_channel_id = ?`;
189129

@@ -200,6 +140,9 @@ export async function twitchUpdateIsLive(channelId: string, isLive: boolean) {
200140
}
201141
}
202142

143+
/**
144+
* @deprecated This function is deprecated and being removed
145+
*/
203146
export async function twitchStopGuildTrackingChannel(
204147
guild_id: string,
205148
channelId: string,
@@ -221,7 +164,9 @@ export async function twitchStopGuildTrackingChannel(
221164
// #endregion
222165

223166
// #region i have no idea what im doing here
224-
167+
/**
168+
* @deprecated This function is deprecated and being removed
169+
*/
225170
export async function getAllTrackedInGuild(
226171
guild_id: string,
227172
): Promise<dbDiscordTable[]> {

src/utils/db/discord.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export async function discordAddGuildTrackingUser(
103103
} else if (platform === Platform.Twitch) {
104104
query = `
105105
INSERT INTO guild_twitch_subscriptions (
106-
twitch_user_id, guild_id, guild_channel_id, role_id, is_dm
106+
twitch_channel_id, guild_id, notification_channel_id, notification_role_id, is_dm
107107
) VALUES ($1, $2, $3, $4, $5)
108108
`;
109109
params = [platformUserId, guildId, guildChannelId, roleId, isDm];

src/utils/db/init.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ export default async function initTables(): Promise<boolean> {
141141
ALTER TABLE guild_youtube_subscriptions
142142
DROP CONSTRAINT guild_youtube_subscriptions_guild_id_fkey;
143143
END IF;
144+
145+
IF EXISTS (
146+
SELECT 1 FROM information_schema.table_constraints
147+
WHERE constraint_name = 'guild_twitch_subscriptions_guild_id_fkey'
148+
AND table_name = 'guild_twitch_subscriptions'
149+
) THEN
150+
ALTER TABLE guild_twitch_subscriptions
151+
DROP CONSTRAINT guild_twitch_subscriptions_guild_id_fkey;
152+
END IF;
144153
END$$;
145154
`;
146155

0 commit comments

Comments
 (0)