blob: 4d4a392b9f9ec37d33c78502cf5a2a66a2a2a6a6 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
2import { ChannelType } from "discord-api-types";
pineafan6702cef2022-06-13 17:52:37 +01003import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01005import confirmationMessage from "../../../utils/confirmationMessage.js";
6import getEmojiByName from "../../../utils/getEmojiByName.js";
7import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
8import { WrappedCheck } from "jshaiku";
9import client from "../../../utils/client.js";
10
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("channel")
14 .setDescription("Sets or shows the log channel")
15 .addChannelOption(option => option.setName("channel").setDescription("The channel to set the log channel to").addChannelTypes([
16 ChannelType.GuildNews, ChannelType.GuildText
17 ]));
pineafan6702cef2022-06-13 17:52:37 +010018
pineafanbd02b4a2022-08-05 22:01:38 +010019const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
20 const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Discord.Message;
pineafan6702cef2022-06-13 17:52:37 +010021 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010022 let channel;
pineafan6702cef2022-06-13 17:52:37 +010023 try {
pineafan63fc5e22022-08-04 22:04:10 +010024 channel = interaction.options.getChannel("channel");
pineafan6702cef2022-06-13 17:52:37 +010025 } catch {
pineafan4edb7762022-06-26 19:21:04 +010026 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010027 .setEmoji("CHANNEL.TEXT.DELETE")
28 .setTitle("Log Channel")
29 .setDescription("The channel you provided is not a valid channel")
30 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010031 ]});
pineafan6702cef2022-06-13 17:52:37 +010032 }
pineafan63fc5e22022-08-04 22:04:10 +010033 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010034 if (channel.guild.id !== interaction.guild.id) {
pineafan4edb7762022-06-26 19:21:04 +010035 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010036 .setTitle("Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010037 .setDescription("You must choose a channel in this server")
pineafan6702cef2022-06-13 17:52:37 +010038 .setStatus("Danger")
39 .setEmoji("CHANNEL.TEXT.DELETE")
40 ]});
41 }
pineafan63fc5e22022-08-04 22:04:10 +010042 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010043 .setEmoji("CHANNEL.TEXT.EDIT")
44 .setTitle("Log Channel")
45 .setDescription(`Are you sure you want to set the log channel to <#${channel.id}>?`)
46 .setColor("Warning")
47 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010048 .send(true);
49 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010050 if (confirmation.success) {
51 try {
pineafan63fc5e22022-08-04 22:04:10 +010052 await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id});
53 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
54 const data = {
55 meta:{
56 type: "logChannelUpdate",
57 displayName: "Log Channel Changed",
58 calculateType: "nucleusSettingsUpdated",
59 color: NucleusColors.yellow,
60 emoji: "CHANNEL.TEXT.EDIT",
61 timestamp: new Date().getTime()
62 },
63 list: {
64 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
65 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
66 channel: entry(channel.id, renderChannel(channel))
67 },
68 hidden: {
69 guild: channel.guild.id
pineafanda6e5342022-07-03 10:03:16 +010070 }
pineafan63fc5e22022-08-04 22:04:10 +010071 };
72 log(data);
pineafan6702cef2022-06-13 17:52:37 +010073 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010074 console.log(e);
pineafan4edb7762022-06-26 19:21:04 +010075 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010076 .setTitle("Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010077 .setDescription("Something went wrong and the log channel could not be set")
pineafan6702cef2022-06-13 17:52:37 +010078 .setStatus("Danger")
79 .setEmoji("CHANNEL.TEXT.DELETE")
80 ], components: []});
81 }
82 } else {
pineafan4edb7762022-06-26 19:21:04 +010083 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010084 .setTitle("Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010085 .setDescription("No changes were made")
pineafan6702cef2022-06-13 17:52:37 +010086 .setStatus("Success")
87 .setEmoji("CHANNEL.TEXT.CREATE")
88 ], components: []});
89 }
90 }
91 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +010092 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010093 let channel = data.logging.logs.channel;
94 while (true) {
pineafan4edb7762022-06-26 19:21:04 +010095 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010096 .setTitle("Log channel")
97 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
98 .setStatus("Success")
99 .setEmoji("CHANNEL.TEXT.CREATE")
100 ], components: [new MessageActionRow().addComponents([new MessageButton()
101 .setCustomId("clear")
102 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
103 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
104 .setStyle("DANGER")
105 .setDisabled(!channel)
106 ])]});
107 let i;
108 try {
pineafanc6158ab2022-06-17 16:34:07 +0100109 i = await m.awaitMessageComponent({time: 300000});
pineafan63fc5e22022-08-04 22:04:10 +0100110 } catch(e) { break; }
111 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100112 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100113 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100114 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100115 clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100116 await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]);
pineafan6702cef2022-06-13 17:52:37 +0100117 channel = undefined;
118 }
119 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100120 break;
pineafan6702cef2022-06-13 17:52:37 +0100121 }
122 }
pineafan4edb7762022-06-26 19:21:04 +0100123 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100124 .setTitle("Log channel")
125 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
126 .setStatus("Success")
127 .setEmoji("CHANNEL.TEXT.CREATE")
128 .setFooter({text: "Message closed"})
129 ], components: [new MessageActionRow().addComponents([new MessageButton()
130 .setCustomId("clear")
131 .setLabel("Clear")
132 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
133 .setStyle("SECONDARY")
134 .setDisabled(true)
135 ])]});
pineafan63fc5e22022-08-04 22:04:10 +0100136};
pineafan6702cef2022-06-13 17:52:37 +0100137
pineafanbd02b4a2022-08-05 22:01:38 +0100138const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100139 const member = (interaction.member as Discord.GuildMember);
140 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan6702cef2022-06-13 17:52:37 +0100141 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100142};
pineafan6702cef2022-06-13 17:52:37 +0100143
144export { command };
145export { callback };
146export { check };