blob: 10a588743613699df7ae8f812cfc59cb9d504340 [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)
53 if (confirmation.success) {
54 try {
pineafan4edb7762022-06-26 19:21:04 +010055 await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id})
pineafanda6e5342022-07-03 10:03:16 +010056 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
57 try {
58 let data = {
59 meta:{
60 type: 'logChannelUpdate',
61 displayName: 'Log Channel Changed',
62 calculateType: 'nucleusSettingsUpdated',
63 color: NucleusColors.yellow,
64 emoji: "CHANNEL.TEXT.EDIT",
65 timestamp: new Date().getTime()
66 },
67 list: {
68 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
69 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
70 channel: entry(channel.id, renderChannel(channel)),
71 },
72 hidden: {
73 guild: channel.guild.id
74 }
75 }
76 log(data);
77 } catch {}
pineafan6702cef2022-06-13 17:52:37 +010078 } catch (e) {
79 console.log(e)
pineafan4edb7762022-06-26 19:21:04 +010080 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010081 .setTitle("Log Channel")
82 .setDescription(`Something went wrong and the log channel could not be set`)
83 .setStatus("Danger")
84 .setEmoji("CHANNEL.TEXT.DELETE")
85 ], components: []});
86 }
87 } else {
pineafan4edb7762022-06-26 19:21:04 +010088 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010089 .setTitle("Log Channel")
90 .setDescription(`No changes were made`)
91 .setStatus("Success")
92 .setEmoji("CHANNEL.TEXT.CREATE")
93 ], components: []});
94 }
95 }
96 let clicks = 0;
pineafan4edb7762022-06-26 19:21:04 +010097 let data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010098 let channel = data.logging.logs.channel;
99 while (true) {
pineafan4edb7762022-06-26 19:21:04 +0100100 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100101 .setTitle("Log channel")
102 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
103 .setStatus("Success")
104 .setEmoji("CHANNEL.TEXT.CREATE")
105 ], components: [new MessageActionRow().addComponents([new MessageButton()
106 .setCustomId("clear")
107 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
108 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
109 .setStyle("DANGER")
110 .setDisabled(!channel)
111 ])]});
112 let i;
113 try {
pineafanc6158ab2022-06-17 16:34:07 +0100114 i = await m.awaitMessageComponent({time: 300000});
pineafan6702cef2022-06-13 17:52:37 +0100115 } catch(e) { break }
116 i.deferUpdate()
117 if (i.component.customId == "clear") {
118 clicks += 1;
119 if (clicks == 2) {
120 clicks = 0;
pineafan4edb7762022-06-26 19:21:04 +0100121 await client.database.guilds.write(interaction.guild.id, {}, ["logging.logs.channel"])
pineafan6702cef2022-06-13 17:52:37 +0100122 channel = undefined;
123 }
124 } else {
125 break
126 }
127 }
pineafan4edb7762022-06-26 19:21:04 +0100128 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100129 .setTitle("Log channel")
130 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
131 .setStatus("Success")
132 .setEmoji("CHANNEL.TEXT.CREATE")
133 .setFooter({text: "Message closed"})
134 ], components: [new MessageActionRow().addComponents([new MessageButton()
135 .setCustomId("clear")
136 .setLabel("Clear")
137 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
138 .setStyle("SECONDARY")
139 .setDisabled(true)
140 ])]});
141}
142
143const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
144 let member = (interaction.member as Discord.GuildMember)
pineafanda6e5342022-07-03 10:03:16 +0100145 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command"
pineafan6702cef2022-06-13 17:52:37 +0100146 return true;
147}
148
149export { command };
150export { callback };
151export { check };