all repos — stealth-developers @ 262dd1c52f57211af79ea84b750286b62994ab0a

apps/bot/src/feats/tickets/modals.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
import { option } from "@purrkit/router";
import { Result } from "@sapphire/framework";
import db, {
	addTicketComment,
	editTicket,
	eq,
	getActorByDiscordIdAndGuildDiscordId,
	getGuildByDiscordId,
	getTicket,
	guilds,
} from "@stealth-developers/db";
import {
	CheckboxBuilder,
	LabelBuilder,
	MessageFlags,
	ModalBuilder,
	TextInputBuilder,
	TextInputStyle,
} from "discord.js";

import { kitten } from "@/client";
import { errorMessage, handleError, upsertUser } from "@/lib";
import { useGetTicketData } from "@/middleware";

import * as actions from "./actions";
import { hasAccessToTicket } from "./lib";

export const configTextModal = kitten.modal("config-text", {
	options: {
		key: option.string({ required: true }),
	},
	run: async (interaction, { key }) => {
		const value = interaction.fields.getTextInputValue("text-value");

		if (!interaction.guildId)
			return errorMessage(interaction, "This interaction can only be used in a server.");

		const guild = await getGuildByDiscordId(interaction.guildId);
		if (!guild)
			return errorMessage(interaction, "Failed to update configuration: Guild not found.");

		const [res] = await db
			.update(guilds)
			.set({ [key]: value })
			.where(eq(guilds.id, guild.id))
			.returning();

		if (!res) return errorMessage(interaction, "Failed to update configuration.");

		return interaction.reply({
			content: "Configuration updated successfully!",
			flags: [MessageFlags.Ephemeral],
		});
	},
});

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

		if (!hasAccessToTicket(actor, guild, ticket)) {
			return errorMessage(interaction, "You do not have access to this ticket");
		}

		const shouldDelete = actor.moderator ? interaction.fields.getCheckbox("delete") : false;

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

		const result = await Result.fromAsync(
			actions.closeTicket(actor, ticket.id, reasons, shouldDelete),
		);
		if (result.isErr())
			return handleError(interaction, result.unwrapErr(), "Failed to close ticket.");

		return interaction.followUp({ content: "Ticket closed successfully" });
	},
});

export const editReasonModal = kitten.modal("edit-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") || null,
			private: interaction.fields.getTextInputValue("reason:private") || null,
		};

		const result = await Result.fromAsync(
			editTicket(ticketId, {
				closeReason: reasons.public ? reasons.public : ticket.closeReason,
				privateReason: reasons.private ? reasons.private : ticket.privateReason,
			}),
		);
		if (result.isErr())
			return handleError(interaction, result.unwrapErr(), "Failed to edit ticket.");

		return interaction.followUp({ content: "Ticket edited successfully" });
	},
});

export const commentModal = kitten.modal("m-comment-modal", {
	options: {
		ticketId: option.string({ required: true }),
	},
	async run(interaction, { ticketId }) {
		if (!interaction.guild) return;

		const actor = await getActorByDiscordIdAndGuildDiscordId(
			interaction.user.id,
			interaction.guild.id,
		);
		if (!actor)
			return interaction.reply({ content: "You are not a moderator", flags: ["Ephemeral"] });

		await addTicketComment({
			actorId: actor.id,
			content: interaction.fields.getTextInputValue("comment"),
			ticketId,
		});

		await interaction.reply({ content: "Comment added successfully", flags: ["Ephemeral"] });
	},
});

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 deleteCheckbox = new LabelBuilder()
			.setLabel("Delete ticket channel")
			.setDescription("Delete the ticket channel after closing")
			.setCheckboxComponent(new CheckboxBuilder().setCustomId("delete").setDefault(false));

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

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

		if (isModerator) modal.addLabelComponents(deleteCheckbox);
		return modal;
	},
	edit: (id: string, reasons: { public?: string | null; private?: string | null }) => {
		const reasonLabel = new LabelBuilder()
			.setLabel("Reason")
			.setDescription("The reason for closing the ticket; this will be shared with the user")
			.setTextInputComponent(
				new TextInputBuilder()
					.setStyle(TextInputStyle.Paragraph)
					.setCustomId("reason:public")
					.setValue(reasons.public ?? "")
					.setRequired(false),
			);

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

		const modal = new ModalBuilder()
			.setCustomId(editReasonModal.id({ ticketId: id }))
			.setTitle("Edit ticket")
			.addLabelComponents([reasonLabel, privateReasonLabel]);

		return modal;
	},
	comment: (id: string) => {
		const commentLabel = new LabelBuilder()
			.setLabel("Comment")
			.setDescription("The comment to add to the ticket")
			.setTextInputComponent(
				new TextInputBuilder()
					.setStyle(TextInputStyle.Paragraph)
					.setCustomId("comment")
					.setRequired(true),
			);

		const modal = new ModalBuilder()
			.setCustomId(commentModal.id({ ticketId: id }))
			.setTitle("Add comment")
			.addLabelComponents(commentLabel);

		return modal;
	},
};

export const TicketModals = [closeTicketModal, configTextModal, commentModal, editReasonModal];