pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import Discord, { MessageActionRow, MessageButton } from "discord.js"; |
| 2 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 3 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 4 | import client from "../utils/client.js"; |
| 5 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 6 | export async function create( |
| 7 | guild: Discord.Guild, |
| 8 | member: Discord.User, |
| 9 | createdBy: Discord.User, |
| 10 | reason: string, |
| 11 | customReason?: string |
| 12 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | const config = await client.database.guilds.read(guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 14 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | const overwrites = [ |
| 16 | { |
| 17 | id: member, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 18 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | type: "member" |
| 20 | } |
| 21 | ] as Discord.OverwriteResolvable[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 22 | overwrites.push({ |
| 23 | id: guild.roles.everyone, |
| 24 | deny: ["VIEW_CHANNEL"], |
| 25 | type: "role" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 26 | }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 27 | if (config.tickets.supportRole !== null) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 28 | overwrites.push({ |
| 29 | id: guild.roles.cache.get(config.tickets.supportRole), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 30 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 31 | type: "role" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | let c; |
| 36 | try { |
| 37 | c = await guild.channels.create(member.username, { |
| 38 | type: "GUILD_TEXT", |
| 39 | topic: `${member.id} Active`, |
| 40 | parent: config.tickets.category, |
| 41 | nsfw: false, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 42 | permissionOverwrites: overwrites as Discord.OverwriteResolvable[], |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 43 | reason: "Creating ticket" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 45 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | return null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 47 | } |
| 48 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | await c.send({ |
| 50 | content: |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 51 | `<@${member.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : ""), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 52 | allowedMentions: { |
| 53 | users: [member.id], |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 54 | roles: config.tickets.supportRole !== null ? [config.tickets.supportRole] : [] |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 55 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | }); |
| 57 | await c.send({ |
| 58 | embeds: [ |
| 59 | new EmojiEmbed() |
| 60 | .setTitle("New Ticket") |
| 61 | .setDescription( |
| 62 | "Ticket created by a Moderator\n" + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 63 | `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` + |
| 64 | (reason !== null ? `**Reason:**\n> ${reason}\n` : "") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 65 | `**Ticket ID:** \`${c.id}\`\n` + |
| 66 | "Type `/ticket close` to close this ticket." |
| 67 | ) |
| 68 | .setStatus("Success") |
| 69 | .setEmoji("GUILD.TICKET.OPEN") |
| 70 | ], |
| 71 | components: [ |
| 72 | new MessageActionRow().addComponents([ |
| 73 | new MessageButton() |
| 74 | .setLabel("Close") |
| 75 | .setStyle("DANGER") |
| 76 | .setCustomId("closeticket") |
| 77 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 78 | ]) |
| 79 | ] |
| 80 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 81 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | type: "ticketCreate", |
| 84 | displayName: "Ticket Created", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 85 | calculateType: "ticketUpdate", |
| 86 | color: NucleusColors.green, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | emoji: "GUILD.TICKET.OPEN", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 88 | timestamp: new Date().getTime() |
| 89 | }, |
| 90 | list: { |
| 91 | ticketFor: entry(member.id, renderUser(member)), |
| 92 | createdBy: entry(createdBy.id, renderUser(createdBy)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame^] | 93 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | ticketChannel: entry(c.id, renderChannel(c)) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 95 | }, |
| 96 | hidden: { |
| 97 | guild: guild.id |
| 98 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 100 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | } catch (e) { |
| 102 | console.log(e); |
| 103 | return null; |
| 104 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 105 | return c.id; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | export async function areTicketsEnabled(guild: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | const config = await client.database.guilds.read(guild); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 110 | return config.tickets.enabled; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 111 | } |