blob: 40f99b984941b3e59eb1bf6484ded4fe711cf66f [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);
pineafan813bdf42022-07-24 10:39:10 +01008 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger
9 let overwrites = [{
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"
18 })
19 if (config.tickets.supportRole != null) {
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"
24 })
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"
36 })
37 } catch (e) {
38 return null
39 }
40 try {
41 await c.send(
42 {
43 content: (`<@${member.id}>` + (config.tickets.supportRole != null ? ` • <@&${config.tickets.supportRole}>` : "")),
44 allowedMentions: {
45 users: [member.id],
46 roles: (config.tickets.supportRole != null ? [config.tickets.supportRole] : [])
47 }
48 }
49 )
50 await c.send({ embeds: [new EmojiEmbed()
51 .setTitle("New Ticket")
52 .setDescription(
53 `Ticket created by a Moderator\n` +
54 `**Support type:** Appeal submission\n` + (reason != null ? `**Reason:**\n> ${reason}\n` : "") +
55 `**Ticket ID:** \`${c.id}\`\n` +
56 `Type \`/ticket close\` to close this ticket.`,
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"))
65 ])]})
66 let data = {
67 meta:{
68 type: 'ticketCreate',
69 displayName: 'Ticket Created',
70 calculateType: "ticketUpdate",
71 color: NucleusColors.green,
72 emoji: 'GUILD.TICKET.OPEN',
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())),
79 ticketChannel: entry(c.id, renderChannel(c)),
80 },
81 hidden: {
82 guild: guild.id
83 }
84 }
85 log(data);
86 } catch (e) { console.log(e); return null }
87 return c.id
88}
89
90export async function areTicketsEnabled(guild: string) {
91 let config = await client.database.guilds.read(guild);
92 return config.tickets.enabled;
93}