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