blob: bdc5e07238d0e826ca26750f253eb9c94e6a6718 [file] [log] [blame]
PineaFan9b2ac4d2023-01-18 14:41:07 +00001import { getCommandMentionByName } from './../utils/getCommandMentionByName.js';
TheCodedProf21c08592022-09-13 14:14:43 -04002import Discord, { ActionRowBuilder, ButtonBuilder, OverwriteType, ChannelType, ButtonStyle } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01003import EmojiEmbed from "../utils/generateEmojiEmbed.js";
pineafan813bdf42022-07-24 10:39:10 +01004import getEmojiByName from "../utils/getEmojiByName.js";
5import client from "../utils/client.js";
6
Skyler Grey75ea9172022-08-06 10:22:23 +01007export async function create(
8 guild: Discord.Guild,
9 member: Discord.User,
10 createdBy: Discord.User,
pineafan62ce1922022-08-25 20:34:45 +010011 reason: string | null,
Skyler Grey75ea9172022-08-06 10:22:23 +010012 customReason?: string
13) {
pineafan63fc5e22022-08-04 22:04:10 +010014 const config = await client.database.guilds.read(guild.id);
Skyler Grey11236ba2022-08-08 21:13:33 +010015 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
TheCodedProf21c08592022-09-13 14:14:43 -040016 const overwrites = [{
17 id: member,
18 allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"],
19 type: OverwriteType.Member
20 }] as unknown as Discord.OverwriteResolvable[];
pineafan813bdf42022-07-24 10:39:10 +010021 overwrites.push({
22 id: guild.roles.everyone,
TheCodedProf21c08592022-09-13 14:14:43 -040023 deny: ["ViewChannel"],
24 type: OverwriteType.Role
pineafan63fc5e22022-08-04 22:04:10 +010025 });
pineafane23c4ec2022-07-27 21:56:27 +010026 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010027 overwrites.push({
pineafan62ce1922022-08-25 20:34:45 +010028 id: guild.roles.cache.get(config.tickets.supportRole)!,
TheCodedProf21c08592022-09-13 14:14:43 -040029 allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"],
30 type: OverwriteType.Role
pineafan63fc5e22022-08-04 22:04:10 +010031 });
pineafan813bdf42022-07-24 10:39:10 +010032 }
33
34 let c;
35 try {
TheCodedProf21c08592022-09-13 14:14:43 -040036 c = await guild.channels.create({
37 name: member.username,
38 type: ChannelType.GuildText,
pineafan813bdf42022-07-24 10:39:10 +010039 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` +
PineaFan9b2ac4d2023-01-18 14:41:07 +000066 `Type ${getCommandMentionByName("ticket/close")} to close this ticket.`
Skyler Grey75ea9172022-08-06 10:22:23 +010067 )
68 .setStatus("Success")
69 .setEmoji("GUILD.TICKET.OPEN")
70 ],
71 components: [
PineaFan100df682023-01-02 13:26:08 +000072 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -040073 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010074 .setLabel("Close")
TheCodedProf21c08592022-09-13 14:14:43 -040075 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +010076 .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)),
PineaFan100df682023-01-02 13:26:08 +000093 created: entry((new Date().getTime()).toString(), 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}