blob: a974b2e2ceb8a605772ec7df35b29f136d862830 [file] [log] [blame]
pineafanad54d752022-04-18 19:01:43 +01001import Discord, { MessageButton, MessageActionRow } from "discord.js";
pineafan6702cef2022-06-13 17:52:37 +01002import client from "../../utils/client.js";
pineafanad54d752022-04-18 19:01:43 +01003import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
4import getEmojiByName from "../../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +01005
6export default async function (interaction) {
7 // @ts-ignore
PineappleFanb3dd83c2022-06-17 10:53:48 +01008 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger
pineafanad54d752022-04-18 19:01:43 +01009
pineafan6702cef2022-06-13 17:52:37 +010010 let config = await client.database.read(interaction.guild.id);
11 let thread = false; let threadChannel
12 if (interaction.channel instanceof Discord.ThreadChannel) thread = true; threadChannel = interaction.channel as Discord.ThreadChannel
pineafanad54d752022-04-18 19:01:43 +010013 let channel = (interaction.channel as Discord.TextChannel)
pineafan6702cef2022-06-13 17:52:37 +010014 if (!channel.parent || config.tickets.category != channel.parent.id || (thread ? (threadChannel.parent.parent.id != config.tickets.category) : false)) {
pineafanad54d752022-04-18 19:01:43 +010015 return interaction.reply({embeds: [new generateEmojiEmbed()
pineafan6fb3e072022-05-20 19:27:23 +010016 .setTitle("Deleting Ticket...")
pineafan6702cef2022-06-13 17:52:37 +010017 .setDescription("This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread.")
pineafanad54d752022-04-18 19:01:43 +010018 .setStatus("Danger")
19 .setEmoji("CONTROL.BLOCKCROSS")
20 ], ephemeral: true});
21 }
22 let status = channel.topic.split(" ")[1];
23 if (status == "Archived") {
pineafan6fb3e072022-05-20 19:27:23 +010024 await interaction.reply({embeds: [new generateEmojiEmbed()
25 .setTitle("Delete Ticket")
26 .setDescription("Your ticket is being deleted...")
pineafanad54d752022-04-18 19:01:43 +010027 .setStatus("Danger")
28 .setEmoji("GUILD.TICKET.CLOSE")
29 ]});
pineafan6fb3e072022-05-20 19:27:23 +010030 let data = {
31 meta:{
32 type: 'ticketDeleted',
33 displayName: 'Ticket Deleted',
34 calculateType: true,
35 color: NucleusColors.red,
36 emoji: 'GUILD.TICKET.CLOSE',
37 timestamp: new Date().getTime()
38 },
39 list: {
40 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
41 deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
42 closed: entry(new Date().getTime(), renderDelta(new Date().getTime()))
43 },
44 hidden: {
45 guild: interaction.guild.id
pineafanad54d752022-04-18 19:01:43 +010046 }
pineafan6fb3e072022-05-20 19:27:23 +010047 }
PineappleFanb3dd83c2022-06-17 10:53:48 +010048 log(data, client);
pineafan6fb3e072022-05-20 19:27:23 +010049 interaction.channel.delete();
pineafanad54d752022-04-18 19:01:43 +010050 return;
51 } else if (status == "Active") {
pineafan6fb3e072022-05-20 19:27:23 +010052 await interaction.reply({embeds: [new generateEmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010053 .setTitle("Close Ticket")
pineafan6fb3e072022-05-20 19:27:23 +010054 .setDescription("Your ticket is being closed...")
pineafanad54d752022-04-18 19:01:43 +010055 .setStatus("Warning")
56 .setEmoji("GUILD.TICKET.ARCHIVED")
57 ]});
pineafan6fb3e072022-05-20 19:27:23 +010058 let overwrites = [
59 {
60 id: channel.topic.split(" ")[0],
61 deny: ["VIEW_CHANNEL"],
62 type: "member"
63 },
64 {
65 id: interaction.guild.id,
66 deny: ["VIEW_CHANNEL"],
67 type: "role"
pineafan5be5eca2022-05-19 21:37:08 +010068 }
pineafan6fb3e072022-05-20 19:27:23 +010069 ] as Discord.OverwriteResolvable[];
70 if (config.tickets.supportRole != null) {
71 overwrites.push({
72 id: interaction.guild.roles.cache.get(config.tickets.supportRole),
73 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
74 type: "role"
75 })
76 }
77 channel.edit({permissionOverwrites: overwrites})
78 channel.setTopic(`${channel.topic.split(" ")[0]} Archived`);
79 let data = {
80 meta:{
81 type: 'ticketClosed',
82 displayName: 'Ticket Closed',
83 calculateType: true,
84 color: NucleusColors.yellow,
85 emoji: 'GUILD.TICKET.ARCHIVED',
86 timestamp: new Date().getTime()
87 },
88 list: {
89 ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)),
90 closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)),
91 closed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
92 ticketChannel: entry(channel.id, renderChannel(channel)),
93 },
94 hidden: {
95 guild: interaction.guild.id
pineafanad54d752022-04-18 19:01:43 +010096 }
pineafan6fb3e072022-05-20 19:27:23 +010097 }
PineappleFanb3dd83c2022-06-17 10:53:48 +010098 log(data, client);
pineafan6fb3e072022-05-20 19:27:23 +010099 await interaction.editReply({embeds: [new generateEmojiEmbed()
100 .setTitle("Close Ticket")
101 .setDescription("This ticket has been closed.\nType `/ticket close` again to delete it.")
102 .setStatus("Warning")
103 .setEmoji("GUILD.TICKET.ARCHIVED") // TODO:[Premium] Add a transcript option ||\----/|| <- the bridge we will cross when we come to it
104 ], components: [new MessageActionRow().addComponents([new MessageButton()
105 .setLabel("Delete")
106 .setStyle("DANGER")
107 .setCustomId("closeticket")
108 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
109 ])]});
pineafanad54d752022-04-18 19:01:43 +0100110 return;
111 }
112}
113
114async function purgeByUser(member, guild) {
pineafan6702cef2022-06-13 17:52:37 +0100115 let config = await client.database.read(guild.id);
pineafanad54d752022-04-18 19:01:43 +0100116 if (!config.tickets.category) return;
117 let tickets = guild.channels.cache.get(config.tickets.category);
pineafan5be5eca2022-05-19 21:37:08 +0100118 if (!tickets) return;
pineafanad54d752022-04-18 19:01:43 +0100119 let ticketChannels = tickets.children;
120 let deleted = 0
121 ticketChannels.forEach(element => {
122 if (element.type != "GUILD_TEXT") return;
123 if (element.topic.split(" ")[0] == member) {
124 element.delete();
125 deleted++
126 }
127 });
128 if (deleted) {
129 try {
130 const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
131 let data = {
132 meta:{
133 type: 'ticketPurge',
134 displayName: 'Tickets Purged',
135 calculateType: true,
136 color: NucleusColors.red,
137 emoji: 'GUILD.TICKET.DELETE',
138 timestamp: new Date().getTime()
139 },
140 list: {
141 ticketFor: entry(member, renderUser(member)),
142 deletedBy: entry(null, "Member left server"),
143 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
144 ticketsDeleted: deleted,
145 },
146 hidden: {
147 guild: guild.id
148 }
149 }
150 log(data, member.client);
151 } catch {}
152 }
153}
154
155export { purgeByUser }