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