src/interactions/commands/tickets/_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 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 |
import type { Attachment, Ticket } from "@/database";
import { text } from "@/utils/discord/components";
import { getPublicUrl } from "@/utils/s3";
import { generateTranscript } from "@/utils/tickets/transcripts";
import {
ActionRowBuilder,
AttachmentBuilder,
ButtonBuilder,
ButtonStyle,
CheckboxBuilder,
ContainerBuilder,
LabelBuilder,
ModalBuilder,
SeparatorBuilder,
TextDisplayBuilder,
TextInputBuilder,
TextInputStyle,
} from "discord.js";
// -- constants ------------------------------------------------------------------------------------
export const PAGINATION_SIZE = 10;
export const MODAL_IDS = {
message: "ticket:message",
reason: "ticket:reason",
} as const;
export const INPUT_IDS = {
REASON: "ticket:reason_input",
PRIVATE_REASON: "ticket:private_reason_input",
} as const;
// -- modals ---------------------------------------------------------------------------------------
export function getTicketReasonModal(staff = false) {
const modal = new ModalBuilder()
.setCustomId(MODAL_IDS.reason)
.setTitle("Close Ticket");
const pubReason = new LabelBuilder()
.setLabel("Public Reason")
.setDescription(
staff
? "Provide a reason for closing the ticket - this will be visible to the user"
: "Provide a reason for closing the ticket",
)
.setTextInputComponent(
new TextInputBuilder()
.setCustomId(INPUT_IDS.REASON)
.setPlaceholder("The issue was resolved.")
.setStyle(TextInputStyle.Paragraph)
.setMaxLength(2000)
.setRequired(false),
);
const privateReason = new LabelBuilder()
.setLabel("Private Reason")
.setDescription(
"Provide a reason for closing the ticket - this will only be visible to staff",
)
.setTextInputComponent(
new TextInputBuilder()
.setCustomId(INPUT_IDS.PRIVATE_REASON)
.setPlaceholder("The issue was resolved.")
.setStyle(TextInputStyle.Paragraph)
.setMaxLength(2000)
.setRequired(false),
);
const deleteGroup = new LabelBuilder()
.setLabel("Immediately delete channel")
.setCheckboxComponent(
new CheckboxBuilder()
.setCustomId("ticket:delete_checkbox")
.setDefault(false),
);
modal.addLabelComponents(pubReason);
if (staff) modal.addLabelComponents(privateReason, deleteGroup);
return modal;
}
// -- container builders ---------------------------------------------------------------------------
export function constructTicketContainer(
ticket: Ticket,
attachmentsString: string,
_context: "closed" | "view" = "closed",
isPublic = true,
) {
const closedByReporter = ticket.closedBy === "reporter";
const closedByString = ticket.closedAt
? closedByReporter
? "reporter"
: `<@${ticket.closedBy}> (staff)`
: "N/A";
const reasons = {
publicReason: ticket.closeReason ?? undefined,
privateReason: ticket.privateReason ?? undefined,
};
const reasonContructor = (title: string, reason?: string) =>
reason
? `### ${title} Reason\n${reason}`
: `### ${title} Reason\n-# No ${title.toLowerCase()} reason provided.`;
const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel("View Transcript")
.setStyle(ButtonStyle.Link)
.setURL(`https://tickets.vt3e.cat/tickets/${ticket.id}`),
);
if (isPublic) {
buttons.addComponents(
new ButtonBuilder()
.setCustomId(`ticket:thank:${ticket.id}`)
.setLabel("Thank your moderator")
.setEmoji("💖")
.setStyle(ButtonStyle.Primary),
);
}
const wasBehalfTicket = ticket.openedBy !== ticket.authorId;
const reasonText = ticket.topic
? `for reason: ${ticket.topic}`
: "without a provided reason";
const behalfText = wasBehalfTicket
? `**Opened By**: <@${ticket.openedBy}> ${reasonText}`
: null;
const container = new ContainerBuilder()
.addTextDisplayComponents(
text(
[
`## Ticket #${ticket.id} (${ticket.anonymousId}) - ${ticket.status}`,
`**Closed By**: ${closedByString}`,
behalfText,
].join("\n"),
),
)
.addSeparatorComponents(new SeparatorBuilder().setDivider(false))
.addTextDisplayComponents(
text(
[
reasonContructor("Public", reasons?.publicReason),
isPublic ? null : reasonContructor("Private", reasons?.privateReason),
]
.filter(Boolean)
.join("\n"),
),
)
.addSeparatorComponents(
new SeparatorBuilder().setDivider(false).setSpacing(2),
)
.addTextDisplayComponents(
text(
[
attachmentsString.length > 0
? `-# **Attachments:** ${attachmentsString}`
: "-# **Attachments:** no attachments",
]
.filter(Boolean)
.join("\n"),
),
)
.addActionRowComponents(buttons);
return container;
}
export async function getTicketContainer(
ticket: Ticket,
context: "closed" | "view" = "closed",
isPublic = true,
) {
const [transcriptText, attachments] = await generateTranscript(ticket);
const file = new AttachmentBuilder(Buffer.from(transcriptText, "utf-8"), {
name: `transcript-${ticket.id}.txt`,
});
const attachmentsString = attachments
.map(
(att: Attachment, idx: number) =>
`[Attachment #${idx + 1} (${att.fileName.split(".").slice(-1)[0].toUpperCase()})](<${getPublicUrl(att.key)}>)`,
)
.join(", ");
return {
container: constructTicketContainer(
ticket,
attachmentsString,
context,
isPublic,
),
transcriptText,
attachments,
files: [file],
};
}
|