blob: 17d889ad87a8a8f2f55dcc70808ab68b16c4f0cf [file] [log] [blame]
pineafanad54d752022-04-18 19:01:43 +01001import Discord, { MessageButton, MessageActionRow } from "discord.js";
2import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
3import getEmojiByName from "../../utils/getEmojiByName.js";
4import readConfig from "../../utils/readConfig.js";
5
6export default async function (interaction) {
7 // @ts-ignore
8 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = interaction.client.logger
9
10 let config = await readConfig(interaction.guild.id);
11 let channel = (interaction.channel as Discord.TextChannel)
pineafan5be5eca2022-05-19 21:37:08 +010012 if (!channel.parent || config.tickets.category != channel.parent.id) {
pineafanad54d752022-04-18 19:01:43 +010013 return interaction.reply({embeds: [new generateEmojiEmbed()
pineafan4092b862022-05-20 19:27:23 +010014 .setTitle("Deleting Ticket...")
pineafan5be5eca2022-05-19 21:37:08 +010015 .setDescription("This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread.") // TODO bridge to cross later!
pineafanad54d752022-04-18 19:01:43 +010016 .setStatus("Danger")
17 .setEmoji("CONTROL.BLOCKCROSS")
18 ], ephemeral: true});
19 }
20 let status = channel.topic.split(" ")[1];
21 if (status == "Archived") {
pineafan4092b862022-05-20 19:27:23 +010022 await interaction.reply({embeds: [new generateEmojiEmbed()
23 .setTitle("Delete Ticket")
24 .setDescription("Your ticket is being deleted...")
pineafanad54d752022-04-18 19:01:43 +010025 .setStatus("Danger")
26 .setEmoji("GUILD.TICKET.CLOSE")
27 ]});
pineafan4092b862022-05-20 19:27:23 +010028 let data = {
29 meta:{
30 type: 'ticketDeleted',
31 displayName: 'Ticket Deleted',
32 calculateType: true,
33 color: NucleusColors.red,
34 emoji: 'GUILD.TICKET.CLOSE',
35 timestamp: new Date().getTime()
36 },
37 list: {
38 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
39 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
40 closed: entry(new Date().getTime(), renderDelta(new Date().getTime()))
41 },
42 hidden: {
43 guild: interaction.guild.id
pineafanad54d752022-04-18 19:01:43 +010044 }
pineafan4092b862022-05-20 19:27:23 +010045 }
46 log(data, interaction.client);
47 interaction.channel.delete();
pineafanad54d752022-04-18 19:01:43 +010048 return;
49 } else if (status == "Active") {
pineafan4092b862022-05-20 19:27:23 +010050 await interaction.reply({embeds: [new generateEmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010051 .setTitle("Close Ticket")
pineafan4092b862022-05-20 19:27:23 +010052 .setDescription("Your ticket is being closed...")
pineafanad54d752022-04-18 19:01:43 +010053 .setStatus("Warning")
54 .setEmoji("GUILD.TICKET.ARCHIVED")
55 ]});
pineafan4092b862022-05-20 19:27:23 +010056 let overwrites = [
57 {
58 id: channel.topic.split(" ")[0],
59 deny: ["VIEW_CHANNEL"],
60 type: "member"
61 },
62 {
63 id: interaction.guild.id,
64 deny: ["VIEW_CHANNEL"],
65 type: "role"
pineafan5be5eca2022-05-19 21:37:08 +010066 }
pineafan4092b862022-05-20 19:27:23 +010067 ] as Discord.OverwriteResolvable[];
68 if (config.tickets.supportRole != null) {
69 overwrites.push({
70 id: interaction.guild.roles.cache.get(config.tickets.supportRole),
71 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
72 type: "role"
73 })
74 }
75 channel.edit({permissionOverwrites: overwrites})
76 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
77 let data = {
78 meta:{
79 type: 'ticketClosed',
80 displayName: 'Ticket Closed',
81 calculateType: true,
82 color: NucleusColors.yellow,
83 emoji: 'GUILD.TICKET.ARCHIVED',
84 timestamp: new Date().getTime()
85 },
86 list: {
87 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
88 closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
89 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
90 ticketChannel: entry(channel.id, renderChannel(channel)),
91 },
92 hidden: {
93 guild: interaction.guild.id
pineafanad54d752022-04-18 19:01:43 +010094 }
pineafan4092b862022-05-20 19:27:23 +010095 }
96 log(data, interaction.client);
97 await interaction.editReply({embeds: [new generateEmojiEmbed()
98 .setTitle("Close Ticket")
99 .setDescription("This ticket has been closed.\nType `/ticket close` again to delete it.")
100 .setStatus("Warning")
101 .setEmoji("GUILD.TICKET.ARCHIVED") // TODO:[Premium] Add a transcript option ||\----/|| <- the bridge we will cross when we come to it
102 ], components: [new MessageActionRow().addComponents([new MessageButton()
103 .setLabel("Delete")
104 .setStyle("DANGER")
105 .setCustomId("closeticket")
106 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
107 ])]});
pineafanad54d752022-04-18 19:01:43 +0100108 return;
109 }
110}
111
112async function purgeByUser(member, guild) {
113 let config = await readConfig(guild.id);
114 if (!config.tickets.category) return;
115 let tickets = guild.channels.cache.get(config.tickets.category);
pineafan5be5eca2022-05-19 21:37:08 +0100116 if (!tickets) return;
pineafanad54d752022-04-18 19:01:43 +0100117 let ticketChannels = tickets.children;
118 let deleted = 0
119 ticketChannels.forEach(element => {
120 if (element.type != "GUILD_TEXT") return;
121 if (element.topic.split(" ")[0] == member) {
122 element.delete();
123 deleted++
124 }
125 });
126 if (deleted) {
127 try {
128 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
129 let data = {
130 meta:{
131 type: 'ticketPurge',
132 displayName: 'Tickets Purged',
133 calculateType: true,
134 color: NucleusColors.red,
135 emoji: 'GUILD.TICKET.DELETE',
136 timestamp: new Date().getTime()
137 },
138 list: {
139 ticketFor: entry(member, renderUser(member)),
140 deletedBy: entry(null, "Member left server"),
141 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
142 ticketsDeleted: deleted,
143 },
144 hidden: {
145 guild: guild.id
146 }
147 }
148 log(data, member.client);
149 } catch {}
150 }
151}
152
153export { purgeByUser }