Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { getCommandMentionByName } from "./../utils/getCommandDataByName.js"; |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 2 | import Discord, { ActionRowBuilder, ButtonBuilder, OverwriteType, ChannelType, ButtonStyle } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 4 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 5 | import client from "../utils/client.js"; |
| 6 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 7 | export async function create( |
| 8 | guild: Discord.Guild, |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 9 | user: Discord.User, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 10 | createdBy: Discord.User, |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 11 | reason: string | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | customReason?: string |
| 13 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | const config = await client.database.guilds.read(guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 16 | const overwrites = [ |
| 17 | { |
| 18 | id: user, |
| 19 | allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"], |
| 20 | type: OverwriteType.Member |
| 21 | } |
| 22 | ] as unknown as Discord.OverwriteResolvable[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 23 | overwrites.push({ |
| 24 | id: guild.roles.everyone, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 25 | deny: ["ViewChannel"], |
| 26 | type: OverwriteType.Role |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 28 | if (config.tickets.supportRole !== null) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 29 | overwrites.push({ |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 30 | id: guild.roles.cache.get(config.tickets.supportRole)!, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 31 | allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"], |
| 32 | type: OverwriteType.Role |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 34 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 35 | const targetChannel: Discord.CategoryChannel | Discord.TextChannel = (await guild.channels.fetch( |
| 36 | config.tickets.category! |
| 37 | ))! as Discord.CategoryChannel | Discord.TextChannel; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 38 | |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 39 | let c: Discord.TextChannel | Discord.PrivateThreadChannel; |
| 40 | if (targetChannel.type === Discord.ChannelType.GuildCategory) { |
| 41 | const overwrites = [ |
| 42 | { |
| 43 | id: user, |
| 44 | allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"], |
| 45 | type: Discord.OverwriteType.Member |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 46 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 47 | ] as Discord.OverwriteResolvable[]; |
| 48 | overwrites.push({ |
| 49 | id: guild.roles.everyone, |
| 50 | deny: ["ViewChannel"], |
| 51 | type: Discord.OverwriteType.Role |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 52 | }); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 53 | if (config.tickets.supportRole !== null) { |
| 54 | overwrites.push({ |
| 55 | id: guild.roles.cache.get(config.tickets.supportRole)!, |
| 56 | allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"], |
| 57 | type: Discord.OverwriteType.Role |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | try { |
| 62 | c = await guild.channels.create({ |
| 63 | name: `${user.username.toLowerCase()}`, |
| 64 | type: ChannelType.GuildText, |
| 65 | topic: `${user.id} Active`, |
| 66 | parent: config.tickets.category, |
| 67 | nsfw: false, |
| 68 | permissionOverwrites: overwrites as Discord.OverwriteResolvable[], |
| 69 | reason: "Creating ticket" |
| 70 | }); |
| 71 | } catch (e) { |
| 72 | return null; |
| 73 | } |
| 74 | try { |
| 75 | await c.send({ |
| 76 | content: |
| 77 | `<@${user.id}>` + |
| 78 | (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : ""), |
| 79 | allowedMentions: { |
| 80 | users: [user.id], |
| 81 | roles: config.tickets.supportRole !== null ? [config.tickets.supportRole] : [] |
| 82 | } |
| 83 | }); |
| 84 | await c.send({ |
| 85 | embeds: [ |
| 86 | new EmojiEmbed() |
| 87 | .setTitle("New Ticket") |
| 88 | .setDescription( |
| 89 | "Ticket created by a Moderator\n" + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 90 | `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` + |
| 91 | (reason !== null ? `**Reason:**\n> ${reason}\n` : "") + |
| 92 | `**Ticket ID:** \`${c.id}\`\n` + |
| 93 | `Type ${getCommandMentionByName("ticket/close")} to close this ticket.` |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 94 | ) |
| 95 | .setStatus("Success") |
| 96 | .setEmoji("GUILD.TICKET.OPEN") |
| 97 | ], |
| 98 | components: [ |
| 99 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
| 100 | new ButtonBuilder() |
| 101 | .setLabel("Close") |
| 102 | .setStyle(ButtonStyle.Danger) |
| 103 | .setCustomId("closeticket") |
| 104 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 105 | ]) |
| 106 | ] |
| 107 | }); |
| 108 | } catch (e) { |
| 109 | return null; |
| 110 | } |
| 111 | } else { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 112 | c = (await targetChannel.threads.create({ |
| 113 | name: `${user.username} - ${user.id} - Active`, |
| 114 | autoArchiveDuration: 60 * 24 * 7, |
| 115 | type: Discord.ChannelType.PrivateThread, |
| 116 | reason: "Creating ticket" |
| 117 | })) as Discord.PrivateThreadChannel; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 118 | await c.members.add(user.id); |
| 119 | await c.members.add(createdBy.id); |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 120 | try { |
| 121 | await c.send({ |
| 122 | content: |
| 123 | `<@${user.id}>` + |
| 124 | (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : ""), |
| 125 | allowedMentions: { |
| 126 | users: [user.id], |
| 127 | roles: config.tickets.supportRole !== null ? [config.tickets.supportRole] : [] |
| 128 | } |
| 129 | }); |
| 130 | await c.send({ |
| 131 | embeds: [ |
| 132 | new EmojiEmbed() |
| 133 | .setTitle("New Ticket") |
| 134 | .setDescription( |
| 135 | "Ticket created by a Moderator\n" + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 136 | `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` + |
| 137 | (reason !== null ? `**Reason:**\n> ${reason}\n` : "") + |
| 138 | `**Ticket ID:** \`${c.id}\`\n` + |
| 139 | `Type ${getCommandMentionByName("ticket/close")} to close this ticket.` |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 140 | ) |
| 141 | .setStatus("Success") |
| 142 | .setEmoji("GUILD.TICKET.OPEN") |
| 143 | ], |
| 144 | components: [ |
| 145 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
| 146 | new ButtonBuilder() |
| 147 | .setLabel("Close") |
| 148 | .setStyle(ButtonStyle.Danger) |
| 149 | .setCustomId("closeticket") |
| 150 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 151 | ]) |
| 152 | ] |
| 153 | }); |
| 154 | } catch (e) { |
| 155 | return null; |
| 156 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 157 | } |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 158 | const data = { |
| 159 | meta: { |
| 160 | type: "ticketCreate", |
| 161 | displayName: "Ticket Created", |
| 162 | calculateType: "ticketUpdate", |
| 163 | color: NucleusColors.green, |
| 164 | emoji: "GUILD.TICKET.OPEN", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 165 | timestamp: Date.now() |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 166 | }, |
| 167 | list: { |
| 168 | ticketFor: entry(user.id, renderUser(user)), |
| 169 | createdBy: entry(createdBy.id, renderUser(createdBy)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 170 | created: entry(Date.now().toString(), renderDelta(Date.now())), |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 171 | ticketChannel: entry(c.id, renderChannel(c)) |
| 172 | }, |
| 173 | hidden: { |
| 174 | guild: guild.id |
| 175 | } |
| 176 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 177 | await log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 178 | return c.id; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | export async function areTicketsEnabled(guild: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 182 | const config = await client.database.guilds.read(guild); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 183 | return config.tickets.enabled; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 184 | } |