blob: 61a94babc4ec6cc3848ccf9ca2a9f8656a6922a2 [file] [log] [blame]
TheCodedProf21c08592022-09-13 14:14:43 -04001import Discord, { ActionRowBuilder, ButtonBuilder, OverwriteType, ChannelType, ButtonStyle } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import 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,
pineafan62ce1922022-08-25 20:34:45 +010010 reason: string | null,
Skyler Grey75ea9172022-08-06 10:22:23 +010011 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;
TheCodedProf21c08592022-09-13 14:14:43 -040015 const overwrites = [{
16 id: member,
17 allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"],
18 type: OverwriteType.Member
19 }] as unknown as Discord.OverwriteResolvable[];
pineafan813bdf42022-07-24 10:39:10 +010020 overwrites.push({
21 id: guild.roles.everyone,
TheCodedProf21c08592022-09-13 14:14:43 -040022 deny: ["ViewChannel"],
23 type: OverwriteType.Role
pineafan63fc5e22022-08-04 22:04:10 +010024 });
pineafane23c4ec2022-07-27 21:56:27 +010025 if (config.tickets.supportRole !== null) {
pineafan813bdf42022-07-24 10:39:10 +010026 overwrites.push({
pineafan62ce1922022-08-25 20:34:45 +010027 id: guild.roles.cache.get(config.tickets.supportRole)!,
TheCodedProf21c08592022-09-13 14:14:43 -040028 allow: ["ViewChannel", "SendMessages", "AttachFiles", "AddReactions", "ReadMessageHistory"],
29 type: OverwriteType.Role
pineafan63fc5e22022-08-04 22:04:10 +010030 });
pineafan813bdf42022-07-24 10:39:10 +010031 }
32
33 let c;
34 try {
TheCodedProf21c08592022-09-13 14:14:43 -040035 c = await guild.channels.create({
36 name: member.username,
37 type: ChannelType.GuildText,
pineafan813bdf42022-07-24 10:39:10 +010038 topic: `${member.id} Active`,
39 parent: config.tickets.category,
40 nsfw: false,
Skyler Grey75ea9172022-08-06 10:22:23 +010041 permissionOverwrites: overwrites as Discord.OverwriteResolvable[],
pineafan813bdf42022-07-24 10:39:10 +010042 reason: "Creating ticket"
pineafan63fc5e22022-08-04 22:04:10 +010043 });
pineafan813bdf42022-07-24 10:39:10 +010044 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010045 return null;
pineafan813bdf42022-07-24 10:39:10 +010046 }
47 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010048 await c.send({
49 content:
Skyler Grey11236ba2022-08-08 21:13:33 +010050 `<@${member.id}>` + (config.tickets.supportRole !== null ? ` • <@&${config.tickets.supportRole}>` : ""),
Skyler Grey75ea9172022-08-06 10:22:23 +010051 allowedMentions: {
52 users: [member.id],
Skyler Grey11236ba2022-08-08 21:13:33 +010053 roles: config.tickets.supportRole !== null ? [config.tickets.supportRole] : []
pineafan813bdf42022-07-24 10:39:10 +010054 }
Skyler Grey75ea9172022-08-06 10:22:23 +010055 });
56 await c.send({
57 embeds: [
58 new EmojiEmbed()
59 .setTitle("New Ticket")
60 .setDescription(
61 "Ticket created by a Moderator\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +010062 `**Support type:** ${customReason ? customReason : "Appeal submission"}\n` +
63 (reason !== null ? `**Reason:**\n> ${reason}\n` : "") +
Skyler Grey75ea9172022-08-06 10:22:23 +010064 `**Ticket ID:** \`${c.id}\`\n` +
65 "Type `/ticket close` to close this ticket."
66 )
67 .setStatus("Success")
68 .setEmoji("GUILD.TICKET.OPEN")
69 ],
70 components: [
PineaFan100df682023-01-02 13:26:08 +000071 new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -040072 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010073 .setLabel("Close")
TheCodedProf21c08592022-09-13 14:14:43 -040074 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +010075 .setCustomId("closeticket")
76 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
77 ])
78 ]
79 });
pineafan63fc5e22022-08-04 22:04:10 +010080 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010082 type: "ticketCreate",
83 displayName: "Ticket Created",
pineafan813bdf42022-07-24 10:39:10 +010084 calculateType: "ticketUpdate",
85 color: NucleusColors.green,
pineafan63fc5e22022-08-04 22:04:10 +010086 emoji: "GUILD.TICKET.OPEN",
pineafan813bdf42022-07-24 10:39:10 +010087 timestamp: new Date().getTime()
88 },
89 list: {
90 ticketFor: entry(member.id, renderUser(member)),
91 createdBy: entry(createdBy.id, renderUser(createdBy)),
PineaFan100df682023-01-02 13:26:08 +000092 created: entry((new Date().getTime()).toString(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010093 ticketChannel: entry(c.id, renderChannel(c))
pineafan813bdf42022-07-24 10:39:10 +010094 },
95 hidden: {
96 guild: guild.id
97 }
pineafan63fc5e22022-08-04 22:04:10 +010098 };
pineafan813bdf42022-07-24 10:39:10 +010099 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 } catch (e) {
101 console.log(e);
102 return null;
103 }
pineafan63fc5e22022-08-04 22:04:10 +0100104 return c.id;
pineafan813bdf42022-07-24 10:39:10 +0100105}
106
107export async function areTicketsEnabled(guild: string) {
pineafan63fc5e22022-08-04 22:04:10 +0100108 const config = await client.database.guilds.read(guild);
pineafan813bdf42022-07-24 10:39:10 +0100109 return config.tickets.enabled;
Skyler Grey75ea9172022-08-06 10:22:23 +0100110}