all repos — stealth-developers @ 158b915b8f28f27e37e948b8f6ae783b0cadbccf

apps/bot/src/commands/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
 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
import { Subcommand } from "@sapphire/plugin-subcommands";
import { ApplyOptions } from "@sapphire/decorators";
import { container } from "@sapphire/framework";

import * as actions from "./actions";
import {
	ButtonStyle,
	ButtonBuilder,
	ChannelType,
	ContainerBuilder,
	TextDisplayBuilder,
} from "discord.js";
import { getGuildByDiscordId } from "@stealth-developers/db";
import { errorMessage, getTextChannel } from "@/lib";
import { Result } from "@sapphire/framework";
import { ActionRowBuilder } from "discord.js";

@ApplyOptions<Subcommand.Options>({
	description: "ticket-related commands",
})
export class TicketCommand extends Subcommand {
	public constructor(context: Subcommand.LoaderContext, options: Subcommand.Options) {
		super(context, {
			...options,
			name: "ticket",
			subcommands: [
				{ name: "new", chatInputRun: "createTicket" },
				{ name: "for", chatInputRun: "createTicketFor", preconditions: ["ModeratorCheck"] },
				{
					name: "actions",
					type: "group",
					entries: [
						{ name: "open", chatInputRun: "openTicket" },
						{ name: "close", chatInputRun: "closeTicket" },
						{ name: "add", chatInputRun: "addToTicket" },
						{ name: "claim", chatInputRun: "claimTicket" },
						{ name: "unclaim", chatInputRun: "unclaimTicket" },
						{ name: "delete", chatInputRun: "deleteTicket" },
					],
				},
				{
					name: "view",
					type: "group",
					entries: [
						{ name: "info", chatInputRun: "viewTicketInfo" },
						{ name: "transcript", chatInputRun: "viewTicketTranscript" },
						{ name: "list", chatInputRun: "listTickets" },
						{ name: "export", chatInputRun: "exportTickets" },
					],
				},
				{
					name: "manage",
					type: "group",
					entries: [
						{
							name: "transcript-channel",
							chatInputRun: "manageConfig",
							preconditions: ["ModeratorCheck"],
						},
						{
							name: "entrypoint-channel",
							chatInputRun: "manageConfig",
							preconditions: ["ModeratorCheck"],
						},
						{
							name: "category",
							chatInputRun: "manageConfig",
							preconditions: ["ModeratorCheck"],
						},
						{
							name: "prompt",
							chatInputRun: "manageConfig",
							preconditions: ["ModeratorCheck"],
						},
						{
							name: "send-prompt",
							chatInputRun: "sendPrompt",
							preconditions: ["ModeratorCheck"],
						},
						{
							name: "greeting",
							chatInputRun: "manageConfig",
							preconditions: ["ModeratorCheck"],
						},
					],
				},
			],
		});
	}

	public override registerApplicationCommands(registry: Subcommand.Registry) {
		registry.registerChatInputCommand((builder) =>
			builder
				.setName("ticket")
				.setDescription("root ticket command")
				.addSubcommand((command) => command.setName("new").setDescription("Open a new ticket"))
				.addSubcommand((command) =>
					command
						.setName("for")
						.setDescription("Open a ticket for a user")
						.addUserOption((option) =>
							option
								.setName("user")
								.setDescription("The user to open the ticket for")
								.setRequired(true),
						),
				)
				.addSubcommandGroup((subcommandGroup) =>
					subcommandGroup
						.setName("action")
						.setDescription("ticket actions")
						.addSubcommand((command) =>
							command.setName("open").setDescription("Reopen the ticket in the current channel"),
						)
						.addSubcommand((command) =>
							command
								.setName("close")
								.setDescription("Close the ticket in the current channel")
								.addStringOption((option) =>
									option
										.setName("reason")
										.setDescription("Public reason for closing the ticket")
										.setRequired(true),
								)
								.addStringOption((option) =>
									option
										.setName("private")
										.setDescription(
											"Private reason for closing the ticket, only visible to moderators",
										)
										.setRequired(false),
								)
								.addBooleanOption((option) =>
									option.setName("delete-channel").setDescription("Delete the channel immediately"),
								),
						),
				)
				.addSubcommandGroup((subcommandGroup) =>
					subcommandGroup
						.setName("manage")
						.setDescription("ticket management commands")
						.addSubcommand((command) =>
							command
								.setName("transcript-channel")
								.setDescription("Sets the channel for ticket transcripts")
								.addChannelOption((option) =>
									option
										.setName("channel")
										.setDescription("The channel to send ticket transcripts to")
										.setRequired(true)
										.addChannelTypes(ChannelType.GuildText),
								),
						)
						.addSubcommand((command) =>
							command
								.setName("entrypoint-channel")
								.setDescription("Sets the entry point channel for ticket creation")
								.addChannelOption((option) =>
									option
										.setName("channel")
										.setDescription("The channel to set as the entry point")
										.setRequired(true)
										.addChannelTypes(ChannelType.GuildText),
								),
						)
						.addSubcommand((command) =>
							command
								.setName("category")
								.setDescription("Sets the category for new tickets")
								.addChannelOption((option) =>
									option
										.setName("category")
										.setDescription("The category to create new tickets in")
										.setRequired(true)
										.addChannelTypes(ChannelType.GuildCategory),
								),
						)
						.addSubcommand((command) =>
							command
								.setName("prompt")
								.setDescription("Set the message sent in the entry point channel"),
						)
						.addSubcommand((command) =>
							command
								.setName("send-prompt")
								.setDescription("Send the prompt to the entry point channel"),
						)
						.addSubcommand((command) =>
							command.setName("greeting").setDescription("Set the message sent in new tickets"),
						),
				),
		);
	}

	public async createTicket(interaction: Subcommand.ChatInputCommandInteraction) {
		await actions.createTicket(interaction);
	}
	public async createTicketFor(interaction: Subcommand.ChatInputCommandInteraction) {
		await actions.createTicket(interaction);
	}

	public async manageConfig(interaction: Subcommand.ChatInputCommandInteraction) {
		await actions.manageConfig(interaction);
	}

	public async sendPrompt(interaction: Subcommand.ChatInputCommandInteraction) {
		if (!interaction.guildId)
			return errorMessage(interaction, "This command can only be used in a server.");
		const guild = await getGuildByDiscordId(interaction.guildId);
		if (!guild) return errorMessage(interaction, "Guild not configured.");

		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 button = new ButtonBuilder()
			.setLabel("Open a Ticket")
			.setStyle(ButtonStyle.Success)
			.setCustomId("open-ticket");

		const container = new ContainerBuilder()
			.addTextDisplayComponents(new TextDisplayBuilder().setContent(promptText))
			.addActionRowComponents(new ActionRowBuilder<ButtonBuilder>().addComponents(button));

		await channel.send({ flags: ["IsComponentsV2"], components: [container] });
		return interaction.reply({
			content: "Prompt sent!",
			flags: ["Ephemeral"],
		});
	}
}

container.stores.loadPiece({
	piece: TicketCommand,
	name: "ticket",
	store: "commands",
});