blob: 0162523cd150b7e6bc2b26cf6575241ed4ab7deb [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import Discord, { MessageActionRow, MessageButton } from 'discord.js';
2import EmojiEmbed from '../utils/generateEmojiEmbed.js';
3import getEmojiByName from "../utils/getEmojiByName.js";
4import client from "../utils/client.js";
5
6export async function create(guild: Discord.Guild, member: Discord.User, createdBy: Discord.User, reason: string) {
7 let config = await client.database.guilds.read(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 overwrites.push({
16 id: guild.roles.everyone,
17 deny: ["VIEW_CHANNEL"],
18 type: "role"
19 })
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 )
51 await c.send({ embeds: [new EmojiEmbed()
52 .setTitle("New Ticket")
53 .setDescription(
54 `Ticket created by a Moderator\n` +
55 `**Support type:** Appeal submission\n` + (reason != null ? `**Reason:**\n> ${reason}\n` : "") +
56 `**Ticket ID:** \`${c.id}\`\n` +
57 `Type \`/ticket close\` to close this ticket.`,
58 )
59 .setStatus("Success")
60 .setEmoji("GUILD.TICKET.OPEN")
61 ], components: [new MessageActionRow().addComponents([new MessageButton()
62 .setLabel("Close")
63 .setStyle("DANGER")
64 .setCustomId("closeticket")
65 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
66 ])]})
67 let data = {
68 meta:{
69 type: 'ticketCreate',
70 displayName: 'Ticket Created',
71 calculateType: "ticketUpdate",
72 color: NucleusColors.green,
73 emoji: 'GUILD.TICKET.OPEN',
74 timestamp: new Date().getTime()
75 },
76 list: {
77 ticketFor: entry(member.id, renderUser(member)),
78 createdBy: entry(createdBy.id, renderUser(createdBy)),
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 }
86 log(data);
87 } catch (e) { console.log(e); return null }
88 return c.id
89}
90
91export async function areTicketsEnabled(guild: string) {
92 let config = await client.database.guilds.read(guild);
93 return config.tickets.enabled;
94}