src/interactions/commands/bug/buttons.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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
import config from "@/config.ts";
import { BugModel, GuildModel, MediaModel } from "@/database/schemas.ts";
import lily from "@/utils/logging.ts";
import { hasManagerPermissions } from "@/utils/permissions";
import {
ActionRowBuilder,
ButtonBuilder,
type ButtonInteraction,
ButtonStyle,
ChannelType,
type Client,
type GuildMember,
type Interaction,
} from "discord.js";
import { showEditModal } from "./report.ts";
const logger = lily.child("bugButtons");
export function buildTrelloUrl(title: string, url: string): string {
const params = new URLSearchParams({
name: title,
url: url,
idBoard: config.data?.trelloBoardId || "",
});
return `https://trello.com/addCard?${params.toString()}`;
}
export function buildButtonRow(
bugId: number,
messageUrl: string,
bugTitle: string,
isClosed: boolean,
): ActionRowBuilder<ButtonBuilder> {
const row = new ActionRowBuilder<ButtonBuilder>();
if (isClosed) {
row.addComponents(
new ButtonBuilder()
.setCustomId(`bug:open:${bugId}`)
.setLabel("open")
.setStyle(ButtonStyle.Secondary),
);
} else {
row.addComponents(
new ButtonBuilder()
.setCustomId(`bug:close:${bugId}`)
.setLabel("close")
.setStyle(ButtonStyle.Secondary),
);
}
row.addComponents(
new ButtonBuilder()
.setCustomId(`bug:edit:${bugId}`)
.setLabel("edit")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(`bug:delete:${bugId}`)
.setLabel("delete")
.setStyle(ButtonStyle.Danger),
);
if (config.data.trelloBoardId) {
row.addComponents(
new ButtonBuilder()
.setLabel("add to trello")
.setStyle(ButtonStyle.Link)
.setURL(buildTrelloUrl(bugTitle, messageUrl)),
);
}
return row;
}
export async function canManageBug(
interaction: Interaction,
bugUserId: string,
): Promise<boolean> {
if (!interaction.guild || !interaction.member) return false;
if (interaction.user.id === bugUserId) return true;
const guildData = await GuildModel.findOne({
guild_id: interaction.guild.id,
});
if (!guildData) return false;
return await hasManagerPermissions(interaction.member as GuildMember);
}
export async function handleCloseButton(
client: Client,
interaction: ButtonInteraction,
) {
const bugId = Number.parseInt(interaction.customId.split(":")[2]);
const bug = await BugModel.findOne({ bug_id: bugId });
if (!bug) {
await interaction.reply({
content: "❌ Bug report not found.",
flags: ["Ephemeral"],
});
return;
}
const canManage = await canManageBug(interaction, bug.user_id);
if (!canManage) {
await interaction.reply({
content: "❌ You don't have permission to close this bug report.",
flags: ["Ephemeral"],
});
return;
}
bug.status = "closed";
await bug.save();
if (bug.message_id && bug.thread_id) {
const thread = await client.channels.fetch(bug.thread_id);
if (thread?.isThread()) {
const message = await thread.fetchStarterMessage();
if (message) {
const buttonRow = buildButtonRow(bugId, message.url, bug.title, true);
await message.edit({
components: [...message.components.slice(0, -1), buttonRow],
});
}
}
}
await interaction.reply({
content: "✅ Bug report closed successfully.",
flags: ["Ephemeral"],
});
logger.info(`Bug #${bugId} closed by ${interaction.user.id}`);
}
export async function handleOpenButton(
client: Client,
interaction: ButtonInteraction,
) {
const bugId = Number.parseInt(interaction.customId.split(":")[2]);
const bug = await BugModel.findOne({ bug_id: bugId });
if (!bug) {
await interaction.reply({
content: "❌ Bug report not found.",
flags: ["Ephemeral"],
});
return;
}
const canManage = await canManageBug(interaction, bug.user_id);
if (!canManage) {
await interaction.reply({
content: "❌ You don't have permission to reopen this bug report.",
flags: ["Ephemeral"],
});
return;
}
bug.status = "open";
await bug.save();
if (bug.message_id && bug.thread_id) {
const thread = await client.channels.fetch(bug.thread_id);
if (thread?.isThread()) {
const message = await thread.fetchStarterMessage();
if (message) {
const buttonRow = buildButtonRow(bugId, message.url, bug.title, false);
await message.edit({
components: [...message.components.slice(0, -1), buttonRow],
});
}
}
}
await interaction.reply({
content: "✅ Bug report reopened successfully.",
flags: ["Ephemeral"],
});
logger.info(`Bug #${bugId} reopened by ${interaction.user.id}`);
}
export async function handleEditButton(
_client: Client,
interaction: ButtonInteraction,
) {
const bugId = Number.parseInt(interaction.customId.split(":")[2]);
const bug = await BugModel.findOne({ bug_id: bugId });
if (!bug) {
await interaction.reply({
content: "❌ Bug report not found.",
flags: ["Ephemeral"],
});
return;
}
const canManage = await canManageBug(interaction, bug.user_id);
if (!canManage) {
await interaction.reply({
content: "❌ You don't have permission to edit this bug report.",
flags: ["Ephemeral"],
});
return;
}
const projectKey = bug.projects[0] || "";
await showEditModal(
bugId,
bug.title,
bug.description,
projectKey,
interaction,
);
}
export async function handleDeleteButton(
client: Client,
interaction: ButtonInteraction,
) {
const bugId = Number.parseInt(interaction.customId.split(":")[2]);
const bug = await BugModel.findOne({ bug_id: bugId });
if (!bug) {
await interaction.reply({
content: "❌ Bug report not found.",
flags: ["Ephemeral"],
});
return;
}
const canManage = await canManageBug(interaction, bug.user_id);
if (!canManage) {
await interaction.reply({
content: "❌ You don't have permission to delete this bug report.",
flags: ["Ephemeral"],
});
return;
}
await interaction.deferReply({ flags: ["Ephemeral"] });
try {
// delete associated media
const media = await MediaModel.find({ bug_id: bugId });
if (media.length > 0) {
await MediaModel.deleteMany({ bug_id: bugId });
logger.info(`deleted ${media.length} media files for bug #${bugId}`);
}
// delete associated message & thread
if (bug.message_id && bug.thread_id) {
const thread = await client.channels.fetch(bug.thread_id);
if (thread?.type !== ChannelType.PublicThread) return;
const message = await thread.fetchStarterMessage();
if (message) await message.delete();
try {
await thread.delete(
`bug report #${bugId} deleted by ${interaction.user.username}`,
);
} catch (threadError) {
logger.warn(`Failed to delete thread for bug #${bugId}:`, threadError);
}
}
// tombstone
bug.status = "closed";
bug.title = "[DELETED]";
bug.description = "[DELETED]";
await bug.save();
await interaction.editReply({
content: "✅ Bug report deleted successfully.",
});
logger.info(`Bug #${bugId} deleted by ${interaction.user.id}`);
} catch (error) {
logger.error(`Failed to delete bug #${bugId}:`, error);
await interaction.editReply({
content: "❌ Failed to delete bug report. Please try again later.",
});
}
}
|