blob: 0eca621677b9cd9327c2bf5d64cffd1be4b4deee [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 Grey11236ba2022-08-08 21:13:33 +010014 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
Skyler Grey75ea9172022-08-06 10:22:23 +010015 const overwrites = [
16 {
17 id: member,
Skyler Grey11236ba2022-08-08 21:13:33 +010018 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
Skyler Grey75ea9172022-08-06 10:22:23 +010019 type: "member"
20 }
21 ] as Discord.OverwriteResolvable[];
pineafan813bdf42022-07-24 10:39:10 +010022 overwrites.push({
23 id: guild.roles.everyone,
24 deny: ["VIEW_CHANNEL"],
25 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010026 });
pineafane23c4ec2022-07-27 21:56:27 +010027 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010028 overwrites.push({
29 id: guild.roles.cache.get(config.tickets.supportRole),
Skyler Grey11236ba2022-08-08 21:13:33 +010030 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
pineafan813bdf42022-07-24 10:39:10 +010031 type: "role"
pineafan63fc5e22022-08-04 22:04:10 +010032 });
pineafan813bdf42022-07-24 10:39:10 +010033 }
34
35 let c;
36 try {
37 c = await guild.channels.create(member.username, {
38 type: "GUILD_TEXT",
39 topic: `${member.id} Active`,
40 parent: config.tickets.category,
41 nsfw: false,
Skyler Grey75ea9172022-08-06 10:22:23 +010042 permissionOverwrites: overwrites as Discord.OverwriteResolvable[],
pineafan813bdf42022-07-24 10:39:10 +010043 reason: "Creating ticket"
pineafan63fc5e22022-08-04 22:04:10 +010044 });
pineafan813bdf42022-07-24 10:39:10 +010045 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010046 return null;
pineafan813bdf42022-07-24 10:39:10 +010047 }
48 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010049 await c.send({
50 content:
Skyler Grey11236ba2022-08-08 21:13:33 +010051 `<@${member.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : ""),
Skyler Grey75ea9172022-08-06 10:22:23 +010052 allowedMentions: {
53 users: [member.id],
Skyler Grey11236ba2022-08-08 21:13:33 +010054 roles: config.tickets.supportRole !== null ? [config.tickets.supportRole] : []
pineafan813bdf42022-07-24 10:39:10 +010055 }
Skyler Grey75ea9172022-08-06 10:22:23 +010056 });
57 await c.send({
58 embeds: [
59 new EmojiEmbed()
60 .setTitle("New Ticket")
61 .setDescription(
62 "Ticket created by a Moderator\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +010063 `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` +
64 (reason !== null ? `**Reason:**\n> ${reason}\n` : "") +
Skyler Grey75ea9172022-08-06 10:22:23 +010065 `**Ticket ID:** \`${c.id}\`\n` +
66 "Type `/ticket close` to close this ticket."
67 )
68 .setStatus("Success")
69 .setEmoji("GUILD.TICKET.OPEN")
70 ],
71 components: [
72 new MessageActionRow().addComponents([
73 new MessageButton()
74 .setLabel("Close")
75 .setStyle("DANGER")
76 .setCustomId("closeticket")
77 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
78 ])
79 ]
80 });
pineafan63fc5e22022-08-04 22:04:10 +010081 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010083 type: "ticketCreate",
84 displayName: "Ticket Created",
pineafan813bdf42022-07-24 10:39:10 +010085 calculateType: "ticketUpdate",
86 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010087 emoji: "GUILD.TICKET.OPEN",
pineafan813bdf42022-07-24 10:39:10 +010088 timestamp: new Date().getTime()
89 },
90 list: {
91 ticketFor: entry(member.id, renderUser(member)),
92 createdBy: entry(createdBy.id, renderUser(createdBy)),
Skyler Grey11236ba2022-08-08 21:13:33 +010093 created: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010094 ticketChannel: entry(c.id, renderChannel(c))
pineafan813bdf42022-07-24 10:39:10 +010095 },
96 hidden: {
97 guild: guild.id
98 }
pineafan63fc5e22022-08-04 22:04:10 +010099 };
pineafan813bdf42022-07-24 10:39:10 +0100100 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 } catch (e) {
102 console.log(e);
103 return null;
104 }
pineafan63fc5e22022-08-04 22:04:10 +0100105 return c.id;
pineafan813bdf42022-07-24 10:39:10 +0100106}
107
108export async function areTicketsEnabled(guild: string) {
pineafan63fc5e22022-08-04 22:04:10 +0100109 const config = await client.database.guilds.read(guild);
pineafan813bdf42022-07-24 10:39:10 +0100110 return config.tickets.enabled;
Skyler Grey75ea9172022-08-06 10:22:23 +0100111}