blob: 41f949c36637e042984d6ee2454a428f23890e23 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import Discord, { MessageActionRow, MessageButton } from "discord.js";
2import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan813bdf42022-07-24 10:39:10 +01003import getEmojiByName from "../utils/getEmojiByName.js";
4import client from "../utils/client.js";
5
Skyler Grey75ea9172022-08-06 10:22:23 +01006export async function create(
7 guild: Discord.Guild,
8 member: Discord.User,
9 createdBy: Discord.User,
10 reason: string,
11 customReason?: string
12) {
pineafan63fc5e22022-08-04 22:04:10 +010013 const config = await client.database.guilds.read(guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010014 const {
15 log,
16 NucleusColors,
17 entry,
18 renderUser,
19 renderChannel,
20 renderDelta
21 } = client.logger;
22 const overwrites = [
23 {
24 id: member,
25 allow: [
26 "VIEW_CHANNEL",
27 "SEND_MESSAGES",
28 "ATTACH_FILES",
29 "ADD_REACTIONS",
30 "READ_MESSAGE_HISTORY"
31 ],
32 type: "member"
33 }
34 ] as Discord.OverwriteResolvable[];
pineafan813bdf42022-07-24 10:39:10 +010035 overwrites.push({
36 id: guild.roles.everyone,
37 deny: ["VIEW_CHANNEL"],
38 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010039 });
pineafane23c4ec2022-07-27 21:56:27 +010040 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010041 overwrites.push({
42 id: guild.roles.cache.get(config.tickets.supportRole),
Skyler Grey75ea9172022-08-06 10:22:23 +010043 allow: [
44 "VIEW_CHANNEL",
45 "SEND_MESSAGES",
46 "ATTACH_FILES",
47 "ADD_REACTIONS",
48 "READ_MESSAGE_HISTORY"
49 ],
pineafan813bdf42022-07-24 10:39:10 +010050 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010051 });
pineafan813bdf42022-07-24 10:39:10 +010052 }
53
54 let c;
55 try {
56 c = await guild.channels.create(member.username, {
57 type: "GUILD_TEXT",
58 topic: `${member.id} Active`,
59 parent: config.tickets.category,
60 nsfw: false,
Skyler Grey75ea9172022-08-06 10:22:23 +010061 permissionOverwrites: overwrites as Discord.OverwriteResolvable[],
pineafan813bdf42022-07-24 10:39:10 +010062 reason: "Creating ticket"
pineafan63fc5e22022-08-04 22:04:10 +010063 });
pineafan813bdf42022-07-24 10:39:10 +010064 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010065 return null;
pineafan813bdf42022-07-24 10:39:10 +010066 }
67 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010068 await c.send({
69 content:
70 `<@${member.id}>` +
71 (config.tickets.supportRole !== null
72 ? ` • <@&${config.tickets.supportRole}>`
73 : ""),
74 allowedMentions: {
75 users: [member.id],
76 roles:
77 config.tickets.supportRole !== null
78 ? [config.tickets.supportRole]
79 : []
pineafan813bdf42022-07-24 10:39:10 +010080 }
Skyler Grey75ea9172022-08-06 10:22:23 +010081 });
82 await c.send({
83 embeds: [
84 new EmojiEmbed()
85 .setTitle("New Ticket")
86 .setDescription(
87 "Ticket created by a Moderator\n" +
88 `**Support type:** ${
89 customReason
90 ? customReason
91 : "Appeal submission"
92 }\n` +
93 (reason !== null
94 ? `**Reason:**\n> ${reason}\n`
95 : "") +
96 `**Ticket ID:** \`${c.id}\`\n` +
97 "Type `/ticket close` to close this ticket."
98 )
99 .setStatus("Success")
100 .setEmoji("GUILD.TICKET.OPEN")
101 ],
102 components: [
103 new MessageActionRow().addComponents([
104 new MessageButton()
105 .setLabel("Close")
106 .setStyle("DANGER")
107 .setCustomId("closeticket")
108 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
109 ])
110 ]
111 });
pineafan63fc5e22022-08-04 22:04:10 +0100112 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100114 type: "ticketCreate",
115 displayName: "Ticket Created",
pineafan813bdf42022-07-24 10:39:10 +0100116 calculateType: "ticketUpdate",
117 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +0100118 emoji: "GUILD.TICKET.OPEN",
pineafan813bdf42022-07-24 10:39:10 +0100119 timestamp: new Date().getTime()
120 },
121 list: {
122 ticketFor: entry(member.id, renderUser(member)),
123 createdBy: entry(createdBy.id, renderUser(createdBy)),
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 created: entry(
125 new Date().getTime(),
126 renderDelta(new Date().getTime())
127 ),
pineafan63fc5e22022-08-04 22:04:10 +0100128 ticketChannel: entry(c.id, renderChannel(c))
pineafan813bdf42022-07-24 10:39:10 +0100129 },
130 hidden: {
131 guild: guild.id
132 }
pineafan63fc5e22022-08-04 22:04:10 +0100133 };
pineafan813bdf42022-07-24 10:39:10 +0100134 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 } catch (e) {
136 console.log(e);
137 return null;
138 }
pineafan63fc5e22022-08-04 22:04:10 +0100139 return c.id;
pineafan813bdf42022-07-24 10:39:10 +0100140}
141
142export async function areTicketsEnabled(guild: string) {
pineafan63fc5e22022-08-04 22:04:10 +0100143 const config = await client.database.guilds.read(guild);
pineafan813bdf42022-07-24 10:39:10 +0100144 return config.tickets.enabled;
Skyler Grey75ea9172022-08-06 10:22:23 +0100145}