src/interactions/commands/bug/shared.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import type { Client } from "discord.js";
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
EmbedBuilder,
} from "discord.js";
import config from "../../../config.ts";
import type { BugType } from "../../../database/schemas.ts";
import { Logger } from "../../../utils/logging.ts";
const logger = new Logger("bug-shared");
export const PROJECT_MAP = config.data.projects;
export function getProjectName(value: string): string {
const project = PROJECT_MAP[value as keyof typeof PROJECT_MAP];
if (!project) return "unknown project";
return project.name;
}
export async function updateBugEmbed(
client: Client,
bug: BugType,
messageId: string,
channelId: string,
): Promise<void> {
try {
const channel = await client.channels.fetch(channelId);
if (!channel?.isTextBased() || !("messages" in channel)) return;
const message = await channel.messages.fetch(messageId);
const projectInfo = PROJECT_MAP[bug.project as keyof typeof PROJECT_MAP];
const embed = new EmbedBuilder()
.setAuthor({
name: message.embeds[0].author?.name || "unknown user",
url: message.embeds[0].author?.url,
iconURL: message.embeds[0].author?.iconURL,
})
.setTitle(bug.title)
.setColor(bug.status === "closed" ? 0x95a5a6 : 0xff6b6b)
.setDescription(bug.description)
.setFooter({
text: `${projectInfo.name} • bug #${bug.bug_id} • ${bug.status}`,
})
.setTimestamp();
if (projectInfo.iconURL) embed.setThumbnail(projectInfo.iconURL);
const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId(`bug:close:${bug.bug_id}`)
.setLabel(bug.status === "closed" ? "reopen" : "close")
.setStyle(ButtonStyle.Secondary)
.setEmoji(bug.status === "closed" ? "🔓" : "🔒"),
new ButtonBuilder()
.setCustomId(`bug:edit:${bug.bug_id}`)
.setLabel("edit")
.setStyle(ButtonStyle.Primary)
.setDisabled(bug.status === "closed"),
new ButtonBuilder()
.setCustomId(`bug:delete:${bug.bug_id}`)
.setLabel("delete")
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId("bug:new")
.setLabel("new bug")
.setStyle(ButtonStyle.Success),
);
await message.edit({ embeds: [embed], components: [buttons] });
} catch (error) {
logger.error("failed to update bug embed:", error);
}
}
|