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