blob: aacc2e784ab8b8abc20f142b7bd3a8512b0eed87 [file] [log] [blame]
pineafan6702cef2022-06-13 17:52:37 +01001import { ChannelType } from 'discord-api-types';
2import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01003import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01004import confirmationMessage from "../../../utils/confirmationMessage.js";
5import getEmojiByName from "../../../utils/getEmojiByName.js";
6import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
7import { WrappedCheck } from "jshaiku";
8import client from "../../../utils/client.js";
9
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("channel")
13 .setDescription("Sets or shows the log channel")
14 .addChannelOption(option => option.setName("channel").setDescription("The channel to set the log channel to").addChannelTypes([
15 ChannelType.GuildNews, ChannelType.GuildText
16 ]))
17
18const callback = async (interaction: CommandInteraction): Promise<any> => {
19 let m;
pineafan4edb7762022-06-26 19:21:04 +010020 m = await interaction.reply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010021 .setTitle("Loading")
22 .setStatus("Danger")
23 .setEmoji("NUCLEUS.LOADING")
24 ], ephemeral: true, fetchReply: true});
25 if (interaction.options.getChannel("channel")) {
26 let channel
27 try {
28 channel = interaction.options.getChannel("channel")
29 } catch {
pineafan4edb7762022-06-26 19:21:04 +010030 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010031 .setEmoji("CHANNEL.TEXT.DELETE")
32 .setTitle("Log Channel")
33 .setDescription("The channel you provided is not a valid channel")
34 .setStatus("Danger")
35 ]})
36 }
37 channel = channel as Discord.TextChannel
38 if (channel.guild.id != interaction.guild.id) {
pineafan4edb7762022-06-26 19:21:04 +010039 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010040 .setTitle("Log Channel")
41 .setDescription(`You must choose a channel in this server`)
42 .setStatus("Danger")
43 .setEmoji("CHANNEL.TEXT.DELETE")
44 ]});
45 }
46 let confirmation = await new confirmationMessage(interaction)
47 .setEmoji("CHANNEL.TEXT.EDIT")
48 .setTitle("Log Channel")
49 .setDescription(`Are you sure you want to set the log channel to <#${channel.id}>?`)
50 .setColor("Warning")
51 .setInverted(true)
52 .send(true)
pineafan02ba0232022-07-24 22:16:15 +010053 if (confirmation.cancelled) return
pineafan6702cef2022-06-13 17:52:37 +010054 if (confirmation.success) {
55 try {
pineafan4edb7762022-06-26 19:21:04 +010056 await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id})
pineafanda6e5342022-07-03 10:03:16 +010057 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
58 try {
59 let data = {
60 meta:{
61 type: 'logChannelUpdate',
62 displayName: 'Log Channel Changed',
63 calculateType: 'nucleusSettingsUpdated',
64 color: NucleusColors.yellow,
65 emoji: "CHANNEL.TEXT.EDIT",
66 timestamp: new Date().getTime()
67 },
68 list: {
69 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
70 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
71 channel: entry(channel.id, renderChannel(channel)),
72 },
73 hidden: {
74 guild: channel.guild.id
75 }
76 }
77 log(data);
78 } catch {}
pineafan6702cef2022-06-13 17:52:37 +010079 } catch (e) {
80 console.log(e)
pineafan4edb7762022-06-26 19:21:04 +010081 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010082 .setTitle("Log Channel")
83 .setDescription(`Something went wrong and the log channel could not be set`)
84 .setStatus("Danger")
85 .setEmoji("CHANNEL.TEXT.DELETE")
86 ], components: []});
87 }
88 } else {
pineafan4edb7762022-06-26 19:21:04 +010089 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010090 .setTitle("Log Channel")
91 .setDescription(`No changes were made`)
92 .setStatus("Success")
93 .setEmoji("CHANNEL.TEXT.CREATE")
94 ], components: []});
95 }
96 }
97 let clicks = 0;
pineafan4edb7762022-06-26 19:21:04 +010098 let data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010099 let channel = data.logging.logs.channel;
100 while (true) {
pineafan4edb7762022-06-26 19:21:04 +0100101 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100102 .setTitle("Log channel")
103 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
104 .setStatus("Success")
105 .setEmoji("CHANNEL.TEXT.CREATE")
106 ], components: [new MessageActionRow().addComponents([new MessageButton()
107 .setCustomId("clear")
108 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
109 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
110 .setStyle("DANGER")
111 .setDisabled(!channel)
112 ])]});
113 let i;
114 try {
pineafanc6158ab2022-06-17 16:34:07 +0100115 i = await m.awaitMessageComponent({time: 300000});
pineafan6702cef2022-06-13 17:52:37 +0100116 } catch(e) { break }
117 i.deferUpdate()
118 if (i.component.customId == "clear") {
119 clicks += 1;
120 if (clicks == 2) {
121 clicks = 0;
pineafan4edb7762022-06-26 19:21:04 +0100122 await client.database.guilds.write(interaction.guild.id, {}, ["logging.logs.channel"])
pineafan6702cef2022-06-13 17:52:37 +0100123 channel = undefined;
124 }
125 } else {
126 break
127 }
128 }
pineafan4edb7762022-06-26 19:21:04 +0100129 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100130 .setTitle("Log channel")
131 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
132 .setStatus("Success")
133 .setEmoji("CHANNEL.TEXT.CREATE")
134 .setFooter({text: "Message closed"})
135 ], components: [new MessageActionRow().addComponents([new MessageButton()
136 .setCustomId("clear")
137 .setLabel("Clear")
138 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
139 .setStyle("SECONDARY")
140 .setDisabled(true)
141 ])]});
142}
143
144const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
145 let member = (interaction.member as Discord.GuildMember)
pineafanda6e5342022-07-03 10:03:16 +0100146 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command"
pineafan6702cef2022-06-13 17:52:37 +0100147 return true;
148}
149
150export { command };
151export { callback };
152export { check };