blob: 1d3406dec95d2ebfd4e13866a12ca840ee5a75b9 [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
pineafanc1c18792022-08-03 21:41:36 +01006export async function create(guild: Discord.Guild, member: Discord.User, createdBy: Discord.User, reason: string, customReason?: string) {
pineafan63fc5e22022-08-04 22:04:10 +01007 const config = await client.database.guilds.read(guild.id);
8 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
9 const overwrites = [{
pineafan813bdf42022-07-24 10:39:10 +010010 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"
pineafan63fc5e22022-08-04 22:04:10 +010018 });
pineafane23c4ec2022-07-27 21:56:27 +010019 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010020 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"
pineafan63fc5e22022-08-04 22:04:10 +010024 });
pineafan813bdf42022-07-24 10:39:10 +010025 }
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"
pineafan63fc5e22022-08-04 22:04:10 +010036 });
pineafan813bdf42022-07-24 10:39:10 +010037 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010038 return null;
pineafan813bdf42022-07-24 10:39:10 +010039 }
40 try {
41 await c.send(
42 {
pineafane23c4ec2022-07-27 21:56:27 +010043 content: (`<@${member.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : "")),
pineafan813bdf42022-07-24 10:39:10 +010044 allowedMentions: {
45 users: [member.id],
pineafane23c4ec2022-07-27 21:56:27 +010046 roles: (config.tickets.supportRole !== null ? [config.tickets.supportRole] : [])
pineafan813bdf42022-07-24 10:39:10 +010047 }
48 }
pineafan63fc5e22022-08-04 22:04:10 +010049 );
pineafan813bdf42022-07-24 10:39:10 +010050 await c.send({ embeds: [new EmojiEmbed()
51 .setTitle("New Ticket")
52 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010053 "Ticket created by a Moderator\n" +
pineafanc1c18792022-08-03 21:41:36 +010054 `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` + (reason !== null ? `**Reason:**\n> ${reason}\n` : "") +
pineafan813bdf42022-07-24 10:39:10 +010055 `**Ticket ID:** \`${c.id}\`\n` +
pineafan63fc5e22022-08-04 22:04:10 +010056 "Type `/ticket close` to close this ticket."
pineafan813bdf42022-07-24 10:39:10 +010057 )
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"))
pineafan63fc5e22022-08-04 22:04:10 +010065 ])]});
66 const data = {
pineafan813bdf42022-07-24 10:39:10 +010067 meta:{
pineafan63fc5e22022-08-04 22:04:10 +010068 type: "ticketCreate",
69 displayName: "Ticket Created",
pineafan813bdf42022-07-24 10:39:10 +010070 calculateType: "ticketUpdate",
71 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010072 emoji: "GUILD.TICKET.OPEN",
pineafan813bdf42022-07-24 10:39:10 +010073 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())),
pineafan63fc5e22022-08-04 22:04:10 +010079 ticketChannel: entry(c.id, renderChannel(c))
pineafan813bdf42022-07-24 10:39:10 +010080 },
81 hidden: {
82 guild: guild.id
83 }
pineafan63fc5e22022-08-04 22:04:10 +010084 };
pineafan813bdf42022-07-24 10:39:10 +010085 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010086 } catch (e) { console.log(e); return null; }
87 return c.id;
pineafan813bdf42022-07-24 10:39:10 +010088}
89
90export async function areTicketsEnabled(guild: string) {
pineafan63fc5e22022-08-04 22:04:10 +010091 const config = await client.database.guilds.read(guild);
pineafan813bdf42022-07-24 10:39:10 +010092 return config.tickets.enabled;
93}