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