Skip to content

Commit 1a1df45

Browse files
committed
fixed sql
1 parent c26c0bd commit 1a1df45

4 files changed

Lines changed: 42 additions & 19 deletions

File tree

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,24 @@ These rules are what to follow when working and developing on Feedr. There aren'
5252

5353
### Database Function Return Guidelines
5454

55-
Each database function should **always** return a success indicator (`true`/`false`) along with associated data. To avoid confusion, here are the expected return types:
55+
Each database function (located in `/src/utils/db`) should **always** return a success indicator (`true`/`false`) along with associated data. To avoid confusion, here are the expected return types:
5656

5757
- **Success with data:** `true` should always return populated data, even if the data is not used. For example:
58-
```ts
59-
return { success: true, data: Data as Data };
60-
```
58+
59+
```ts
60+
return { success: true, data: Data as Data };
61+
```
6162

6263
- **Success without data:** `true` can also indicate a successful operation where no data is returned. In this case, an empty array (`[]`) should be provided:
63-
```ts
64-
return { success: true, data: [] };
65-
```
64+
65+
```ts
66+
return { success: true, data: [] };
67+
```
6668

6769
- **Failure:** `false` should indicate an error or unsuccessful operation. This should always return an empty array (`[]`) to ensure consistency:
68-
```ts
69-
return { success: false, data: [] };
70-
```
70+
```ts
71+
return { success: false, data: [] };
72+
```
7173

7274
These guidelines ensure predictable behavior and simplify error handling across the application.
7375

src/commands.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,23 @@ const commands: Record<string, Command> = {
372372

373373
// Check if the channel is already being tracked in the guild
374374
console.log(trackedChannels);
375-
if (trackedChannels) {
375+
if (!trackedChannels || !trackedChannels.success) {
376+
// TODO: Embed
376377
await interaction.reply({
377378
flags: MessageFlags.Ephemeral,
378-
content: `This channel is already being tracked in ${trackedChannels.map((channel, index) => `${index > 0 && index === trackedChannels.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
379+
content:
380+
"An error occurred while trying to check if the channel is already being tracked in this guild! Please report this error!",
381+
});
382+
383+
return;
384+
} else if (
385+
trackedChannels.success &&
386+
trackedChannels.data
387+
) {
388+
// If the channel is already being tracked in the guild, we can just return
389+
await interaction.reply({
390+
flags: MessageFlags.Ephemeral,
391+
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(", ")}!`,
379392
});
380393

381394
return;
@@ -423,6 +436,7 @@ const commands: Record<string, Command> = {
423436

424437
return;
425438
}
439+
426440
case "twitch": {
427441
// Check if the streamer exists by getting the ID
428442
const streamerId = await getStreamerId(platformUserId);
@@ -436,13 +450,13 @@ const commands: Record<string, Command> = {
436450
return;
437451
}
438452

453+
// Check if the channel is already being tracked in the guild
439454
const trackedChannels =
440455
await checkIfGuildIsTrackingUserAlready(
441456
platformUserId,
442457
guildId,
443458
);
444459

445-
// Check if the channel is already being tracked in the guild
446460
if (trackedChannels.length) {
447461
await interaction.reply({
448462
flags: MessageFlags.Ephemeral,

src/utils/db/discord.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function checkIfGuildIsTrackingUserAlready(
3939
if (result.rows.length > 0) {
4040
return { success: true, data: result.rows };
4141
} else {
42-
return { success: false, data: null };
42+
return { success: true, data: null };
4343
}
4444
} catch (error) {
4545
console.error("Error checking if guild is tracking user:", error);

src/utils/db/youtube.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,26 @@ export async function dbYouTubeGetAllChannelsToTrack(): Promise<
2727
// These two functions are for checking/adding a new channel to the youtube table
2828
export async function checkIfChannelIsAlreadyTracked(
2929
channelId: string,
30-
): Promise<boolean> {
31-
const query = `SELECT * FROM youtube WHERE youtube_channel_id = ?`;
30+
): Promise<{ success: boolean; data: dbYouTube[] | [] }> {
31+
const query = `SELECT * FROM youtube WHERE youtube_channel_id = $1`;
3232

3333
try {
3434
const client = await pool.connect();
3535
const result = await client.query(query, [channelId]);
3636

37-
return result.rows.length > 0;
37+
client.release();
38+
39+
return {
40+
success: true,
41+
data: result.rows as dbYouTube[],
42+
};
3843
} catch (err) {
3944
console.error("Error checking if channel is already tracked:", err);
4045

41-
// Return true if there's an error as we don't want to add the channel if we can't check it
42-
return true;
46+
return {
47+
success: false,
48+
data: [],
49+
};
4350
}
4451
}
4552

0 commit comments

Comments
 (0)