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