all repos — stealth-developers @ 887aac5ef1dba4a82a93fdda22415f76f5416da3

apps/bot/src/components/modals/tickets/index.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
import { TextInputBuilder, LabelBuilder, ModalBuilder } from "discord.js";
import { TextInputStyle } from "discord-api-types/v9";
import { option } from "@purrkit/router";

import { editTicket, getTicket } from "@stealth-developers/db";
import { kitten } from "@/client";
import { errorMessage, upsertUser } from "@/lib";

export const closeTicketModal = kitten.modal("close-ticket", {
	options: {
		ticketId: option.string({ required: true }),
	},
	run: async (interaction, { ticketId }) => {
		await interaction.deferReply({ flags: ["Ephemeral"] });
		if (!interaction.guild)
			return errorMessage(interaction, "You must run this command in a guild");

		const ticket = await getTicket(ticketId);
		if (!ticket) return errorMessage(interaction, "Ticket not found");

		const user = await upsertUser(interaction.user, interaction.guild);
		if (!user) return errorMessage(interaction, "Failed to upsert user");

		const reasons = {
			public: interaction.fields.getTextInputValue("reason:public"),
			private: user.moderator ? interaction.fields.getTextInputValue("reason:private") : undefined,
		};

		const result = await editTicket(ticketId, {
			closedAt: new Date(),
			closeReason: reasons.public,
			privateReason: reasons.private,
			closedBy: user.id,
			status: "closed",
		});

		if (!result) return errorMessage(interaction, "Failed to close ticket");
		return interaction.followUp({ content: "Ticket closed successfully" });
	},
});

export const TicketModals = [closeTicketModal];

export const TicketModalPresets = {
	close: (id: string, isModerator: boolean) => {
		const description = isModerator
			? "The reason for closing the ticket; this will be shared with the user"
			: "The reason for closing the ticket";

		const reasonLabel = new LabelBuilder()
			.setLabel("Reason")
			.setDescription(description)
			.setTextInputComponent(
				new TextInputBuilder()
					.setStyle(TextInputStyle.Paragraph)
					.setCustomId("reason:public")
					.setRequired(true),
			);

		const privateReasonLabel = new LabelBuilder()
			.setLabel("Private Reason")
			.setDescription("The reason for closing the ticket.")
			.setTextInputComponent(
				new TextInputBuilder()
					.setStyle(TextInputStyle.Paragraph)
					.setCustomId("reason:private")
					.setRequired(isModerator),
			);

		const labels = isModerator ? [reasonLabel, privateReasonLabel] : [reasonLabel];

		const modal = new ModalBuilder()
			.setCustomId(closeTicketModal.id({ ticketId: id }))
			.setTitle("Close ticket")
			.addLabelComponents(...labels);

		return modal;
	},
};