Skip to content

feat(commands): better errors for /track #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,17 @@ const commands: Record<string, Command> = {
return;
}

// Check if the channel is already being tracked in the guild
if (
const trackedChannels =
await checkIfGuildIsTrackingChannelAlready(
platformUserId,
guildId,
)
) {
);

// Check if the channel is already being tracked in the guild
if (trackedChannels.length) {
await interaction.reply({
flags: MessageFlags.Ephemeral,
content: "This channel is already being tracked!",
content: `This channel is already being tracked in ${trackedChannels.map((channel, index) => `${index > 0 && index === trackedChannels.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
Copy link
Preview

Copilot AI Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] For a two-item list this will render <#A>, and <#B> (with an extra comma). You might want to handle the two-item case specially or use a small utility to format lists in natural language.

Suggested change
content: `This channel is already being tracked in ${trackedChannels.map((channel, index) => `${index > 0 && index === trackedChannels.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
content: `This channel is already being tracked in ${formatList(trackedChannels.map(channel => `<#${channel.guild_channel_id}>`))}!`,

Copilot uses AI. Check for mistakes.

});

return;
Expand Down Expand Up @@ -412,16 +413,17 @@ const commands: Record<string, Command> = {
return;
}

// Check if the channel is already being tracked in the guild
if (
await twitchCheckIfGuildIsTrackingChannelAlready(
streamerId,
const trackedChannels =
await checkIfGuildIsTrackingChannelAlready(
platformUserId,
guildId,
)
) {
);

// Check if the channel is already being tracked in the guild
if (trackedChannels.length) {
await interaction.reply({
flags: MessageFlags.Ephemeral,
content: "This streamer is already being tracked!",
content: `This channel is already being tracked in ${trackedChannels.map((channel, index) => `${index > 0 && index === trackedChannels.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
});

return;
Expand Down
28 changes: 4 additions & 24 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { dbDiscordTable, dbYouTube } from "../types/database";
import type { dbDiscordTable } from "../types/database";

import { Pool } from "pg";

import { dbCredentials, env } from "../config";
import { dbCredentials } from "../config";

// import path from "path";
// import { Database } from "bun:sqlite";
Expand Down Expand Up @@ -30,14 +30,14 @@ export const pool: Pool = new Pool({
export async function checkIfGuildIsTrackingChannelAlready(
channelId: string,
guild_id: string,
) {
): Promise<dbDiscordTable[]> {
const query = `SELECT * FROM discord WHERE platform_user_id = ? AND guild_id = ?`;

try {
const statement = db.prepare(query);
const result = statement.all(channelId, guild_id);

return result.length > 0;
return result;
} catch (err) {
console.error(
"Error checking if guild is tracking channel already:",
Expand Down Expand Up @@ -150,26 +150,6 @@ export async function twitchCheckIfChannelIsAlreadyTracked(channelId: string) {
}
}

export async function twitchCheckIfGuildIsTrackingChannelAlready(
channelId: string,
guild_id: string,
) {
const query = `SELECT * FROM discord WHERE platform_user_id = ? AND guild_id = ?`;

try {
const statement = db.prepare(query);
const result = statement.all(channelId, guild_id);

return result.length > 0;
} catch (err) {
console.error(
"Error checking if guild is tracking Twitch channel already:",
err,
);
throw err;
}
}

export async function twitchAddNewChannelToTrack(
channelId: string,
isLive: boolean,
Expand Down