apps/bot/src/feats/tickets/commands.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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
import { option } from "@purrkit/router";
import { Result } from "@sapphire/result";
import { ButtonBuilder, ContainerBuilder, TextDisplayBuilder, ActionRowBuilder } from "discord.js";
import { getTicketByChannelId, getTicketByGuild, getTicketByUser } from "@stealth-developers/db";
import { errorMessage, getTextChannel, handleError, logger, PublicError } from "@/lib";
import { hasAccessToTicket } from "@/lib/tickets";
import { useGetData } from "@/middleware";
import { TicketButtonPresets } from "./buttons";
import * as actions from "./actions";
import { manageConfig } from "./config";
import { TicketModalPresets } from "./modals";
const useGetTicketData = useGetData.use(async (interaction, { guild }) => {
if (interaction.isChatInputCommand()) {
const ticketId = interaction.options.getNumber("ticket", false);
if (ticketId && guild) {
const ticket = await getTicketByGuild(guild.id, ticketId);
if (!ticket) return {};
return { ticket };
}
}
if (!interaction.channel) return {};
const ticket = await getTicketByChannelId(interaction.channel.id);
if (!ticket) return {};
if (!ticket.channelId) return { ticket };
const ticketChannel = await getTextChannel(ticket.channelId, true);
return { ticket, ticketChannel };
});
const ticketCommand = useGetTicketData.command("ticket", {
description: "Ticket-related commands",
});
ticketCommand.subcommand("new", {
description: "Open a new ticket.",
async run(interaction) {
if (!interaction.guild)
return errorMessage(interaction, "You must run this command in a guild");
const func = actions.createTicket(interaction.member ?? interaction.user, interaction.guild);
const ticketResult = await Result.fromAsync(async () => func);
if (ticketResult.isErr()) {
const error = ticketResult.unwrapErr();
if (error instanceof PublicError) {
return errorMessage(interaction, error.message);
} else {
console.error(error);
return errorMessage(interaction, "An unexpected error occurred while creating the ticket.");
}
}
const { channelId } = ticketResult.unwrap();
return interaction.reply({
content: `Opened ticket <#${channelId}>.`,
flags: ["Ephemeral"],
});
},
});
ticketCommand.subcommand("for", {
description: "Open a new ticket on behalf of someone else",
options: {
user: option.user("The user to open the ticket for", { required: true }),
reason: option.string("The reason for opening the ticket"),
},
async run(interaction, args) {
if (!interaction.guild)
return errorMessage(interaction, "You must run this command in a guild");
const func = actions.createTicket(interaction.member ?? interaction.user, interaction.guild, {
for: args.user,
reason: args.reason,
});
const ticketResult = await Result.fromAsync(async () => func);
if (ticketResult.isErr()) {
const error = ticketResult.unwrapErr();
if (error instanceof PublicError) {
return errorMessage(interaction, error.message);
} else {
logger.error(error);
return errorMessage(interaction, "An unexpected error occurred while creating the ticket.");
}
}
const { channelId } = ticketResult.unwrap();
return interaction.reply({
content: `Opened ticket <#${channelId}>.`,
flags: ["Ephemeral"],
});
},
});
ticketCommand.subcommand("view", {
description: "View a ticket",
options: {
ticket: option.number("The ticket's ID", { required: true }),
},
async run(interaction, args, { guild, actor, user }) {
if (guild && actor) {
const ticket = await getTicketByGuild(guild.id, args.ticket);
if (!ticket) return errorMessage(interaction, `Ticket with ID ${args.ticket} not found.`);
if (!hasAccessToTicket(actor, guild, ticket)) return;
const container = actions.getTicketContainer(ticket, !actor.moderator);
await interaction.reply({ components: [container], flags: ["Ephemeral", "IsComponentsV2"] });
return;
}
if (user) {
const ticket = await getTicketByUser(user.id, args.ticket);
if (!ticket) return errorMessage(interaction, `Ticket with ID ${args.ticket} not found.`);
const container = actions.getTicketContainer(ticket, false);
await interaction.reply({ components: [container], flags: ["Ephemeral", "IsComponentsV2"] });
return;
}
await errorMessage(interaction, "We couldn't find a ticket with that ID.");
},
});
ticketCommand.group(
"actions",
{
description: "Perform ticket actions",
},
(group) => {
group.subcommand("close", {
description: "Close the ticket",
async run(interaction) {
if (!interaction.guild)
return errorMessage(interaction, "This command can only be used in a server.");
const modal = await actions.triggerCloseTicket(
interaction.user,
interaction.guild,
interaction.channelId,
);
await interaction.showModal(modal);
},
});
group.subcommand("reopen", {
description: "Reopen a ticket",
async run(interaction, _, { actor, ticket, ticketChannel, guild }) {
if (!ticket || !ticketChannel || !actor || !guild)
return errorMessage(interaction, "Failed to find ticket");
const result = await Result.fromAsync(
actions.reopenTicket({ ticket, ticketChannel, actor, guild }),
);
if (result.isErr()) return handleError(interaction, result.unwrapErr());
await interaction.reply({
content: `Ticket has been reopened.`,
flags: ["Ephemeral"],
});
},
});
group.subcommand("claim", {
description: "Claim the ticket",
async run(interaction, _, { actor, ticket, ticketChannel, guild }) {
if (!ticket || !ticketChannel || !actor || !guild)
return errorMessage(interaction, "Failed to find ticket");
const result = await Result.fromAsync(
actions.claimTicket({ ticket, ticketChannel, actor, guild }),
);
if (result.isErr()) return handleError(interaction, result.unwrapErr());
await interaction.reply({
content: "Ticket claimed!",
flags: ["Ephemeral"],
});
},
});
group.subcommand("unclaim", {
description: "Unclaim a ticket",
async run(interaction, _, { actor, ticket, ticketChannel, guild }) {
if (!ticket || !ticketChannel || !actor || !guild)
return errorMessage(interaction, "Failed to find ticket");
const result = await Result.fromAsync(
actions.unclaimTicket({ ticket, ticketChannel, actor, guild }),
);
if (result.isErr()) return handleError(interaction, result.unwrapErr());
await interaction.reply({
content: `You have unclaimed ticket #${ticket.localId}.`,
flags: ["Ephemeral"],
});
},
});
group.subcommand("comment", {
description: "Add a comment to the ticket",
options: {
ticket: option.number("The ID of the ticket", { required: false }),
comment: option.string("The comment to add", { required: false }),
},
async run(interaction, { comment }, { actor, ticket, ticketChannel, guild }) {
if (!ticket || !ticketChannel || !actor || !guild)
return errorMessage(interaction, "Failed to find ticket");
if (!actor.moderator)
return errorMessage(interaction, "You must be a moderator to use this command");
if (comment) {
const result = await Result.fromAsync(
actions.addComment({ actor, ticket, guild }, comment),
);
if (result.isErr()) return handleError(interaction, result.unwrapErr());
await interaction.reply({
content: `Comment added to ticket.`,
flags: ["Ephemeral"],
});
return;
}
await interaction.showModal(TicketModalPresets.comment(ticket.id));
},
});
group.subcommand("edit", {
description: "Edit the reasons for closing a ticket",
options: {
ticket: option.number("The ticket to edit", { required: false }),
},
async run(interaction, _, { actor, ticket }) {
if (!actor) {
return errorMessage(interaction, "This command can only be used in a server.");
} else if (!actor.moderator) {
return errorMessage(interaction, "You do not have permission to use this command.");
} else if (!ticket) {
return errorMessage(interaction, "Ticket not found.");
}
await interaction.showModal(
TicketModalPresets.edit(ticket.id, {
public: ticket.closeReason,
private: ticket.privateReason,
}),
);
},
});
// TODO)) clean
},
);
ticketCommand.group(
"manage",
{
description: "Manage ticket settings",
},
(group) => {
// channels & categories
group.subcommand("transcript-channel", {
description: "Set the channel where transcripts will be sent",
options: {
channel: option.channel("The channel to send transcripts to", { required: true }),
},
async run(interaction, _, { actor }) {
if (!actor?.moderator)
return errorMessage(interaction, "You must be a moderator to run this command");
await manageConfig(interaction);
},
});
group.subcommand("entrypoint-channel", {
description: "Set the channel where the ticket prompt will be sent",
options: {
channel: option.channel("The channel to send the ticket prompt to", { required: true }),
},
async run(interaction, _, { actor }) {
if (!actor?.moderator)
return errorMessage(interaction, "You must be a moderator to run this command");
await manageConfig(interaction);
},
});
group.subcommand("category", {
description: "Sets the category for new tickets",
options: {
category: option.channel("The category to create new tickets in", { required: true }),
},
async run(interaction, _, { actor }) {
if (!actor?.moderator)
return errorMessage(interaction, "You must be a moderator to run this command");
await manageConfig(interaction);
},
});
// text
group.subcommand("prompt", {
description: "Set the message sent in the entry point channel",
async run(interaction, _, { actor }) {
if (!actor?.moderator)
return errorMessage(interaction, "You must be a moderator to run this command");
await manageConfig(interaction);
},
});
group.subcommand("greeting", {
description: "Set the message sent in new tickets",
async run(interaction, _, { actor }) {
if (!actor?.moderator)
return errorMessage(interaction, "You must be a moderator to run this command");
await manageConfig(interaction);
},
});
// actions
group.subcommand("send-prompt", {
description: "Send the prompt to the entry point channel",
async run(interaction, _, { guild, actor }) {
if (!interaction.guild || !guild)
return errorMessage(interaction, "You must run this command in a guild");
if (!actor?.moderator)
return errorMessage(interaction, "You must be a moderator to run this command");
const commandString = (sub: string) => `</ticket manage ${sub}:${interaction.commandId}>`;
const channelId = guild.ticketPromptChannel;
const promptText = guild.ticketPrompt;
if (!channelId)
return errorMessage(
interaction,
`Prompt chnanel not configured. Use ${commandString("entrypoint-channel")} to set it up.`,
);
if (!promptText)
return errorMessage(
interaction,
`Ticket prompt not configured. Use ${commandString("prompt")} to set it up.`,
);
const channelRes = await Result.fromAsync(() => getTextChannel(channelId));
if (channelRes.isErr()) {
const err = channelRes.unwrapErr();
const errorMessageText = err instanceof Error ? err.message : "Unknown error";
return errorMessage(interaction, `Failed to fetch prompt channel: ${errorMessageText}`);
}
const channel = channelRes.unwrap();
if (!channel.isSendable()) {
return errorMessage(
interaction,
"I don't have permission to send messages in the prompt channel.",
);
}
const container = new ContainerBuilder()
.addTextDisplayComponents(new TextDisplayBuilder().setContent(promptText))
.addActionRowComponents(
new ActionRowBuilder<ButtonBuilder>().addComponents(TicketButtonPresets.create()),
);
await channel.send({ flags: ["IsComponentsV2"], components: [container] });
return interaction.reply({
content: "Prompt sent!",
flags: ["Ephemeral"],
});
},
});
},
);
export default ticketCommand;
|