PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame^] | 1 | import { getCommandMentionByName } from './../utils/getCommandMentionByName.js'; |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 2 | import Discord, { ActionRowBuilder, ButtonBuilder, OverwriteType, ChannelType, ButtonStyle } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 4 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 5 | import client from "../utils/client.js"; |
| 6 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 7 | export async function create( |
| 8 | guild: Discord.Guild, |
| 9 | member: Discord.User, |
| 10 | createdBy: Discord.User, |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 11 | reason: string | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | customReason?: string |
| 13 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | const config = await client.database.guilds.read(guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 16 | const overwrites = [{ |
| 17 | id: member, |
| 18 | allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"], |
| 19 | type: OverwriteType.Member |
| 20 | }] as unknown as Discord.OverwriteResolvable[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 21 | overwrites.push({ |
| 22 | id: guild.roles.everyone, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 23 | deny: ["ViewChannel"], |
| 24 | type: OverwriteType.Role |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 26 | if (config.tickets.supportRole !== null) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 27 | overwrites.push({ |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 28 | id: guild.roles.cache.get(config.tickets.supportRole)!, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 29 | allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"], |
| 30 | type: OverwriteType.Role |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | let c; |
| 35 | try { |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 36 | c = await guild.channels.create({ |
| 37 | name: member.username, |
| 38 | type: ChannelType.GuildText, |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 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` + |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame^] | 66 | `Type ${getCommandMentionByName("ticket/close")} to close this ticket.` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | ) |
| 68 | .setStatus("Success") |
| 69 | .setEmoji("GUILD.TICKET.OPEN") |
| 70 | ], |
| 71 | components: [ |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 72 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 73 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | .setLabel("Close") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 75 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 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)), |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 93 | created: entry((new Date().getTime()).toString(), 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 | } |