blob: da0d1565a462c3959edf6ce1a6ba0572c5a44e74 [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
19const callback = async (interaction: CommandInteraction): Promise<any> => {
20 let m;
pineafane23c4ec2022-07-27 21:56:27 +010021 m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true});
pineafan6702cef2022-06-13 17:52:37 +010022 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010023 let channel;
pineafan6702cef2022-06-13 17:52:37 +010024 try {
pineafan63fc5e22022-08-04 22:04:10 +010025 channel = interaction.options.getChannel("channel");
pineafan6702cef2022-06-13 17:52:37 +010026 } catch {
pineafan4edb7762022-06-26 19:21:04 +010027 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010028 .setEmoji("CHANNEL.TEXT.DELETE")
29 .setTitle("Log Channel")
30 .setDescription("The channel you provided is not a valid channel")
31 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010032 ]});
pineafan6702cef2022-06-13 17:52:37 +010033 }
pineafan63fc5e22022-08-04 22:04:10 +010034 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010035 if (channel.guild.id !== interaction.guild.id) {
pineafan4edb7762022-06-26 19:21:04 +010036 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010037 .setTitle("Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010038 .setDescription("You must choose a channel in this server")
pineafan6702cef2022-06-13 17:52:37 +010039 .setStatus("Danger")
40 .setEmoji("CHANNEL.TEXT.DELETE")
41 ]});
42 }
pineafan63fc5e22022-08-04 22:04:10 +010043 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010044 .setEmoji("CHANNEL.TEXT.EDIT")
45 .setTitle("Log Channel")
46 .setDescription(`Are you sure you want to set the log channel to <#${channel.id}>?`)
47 .setColor("Warning")
48 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010049 .send(true);
50 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010051 if (confirmation.success) {
52 try {
pineafan63fc5e22022-08-04 22:04:10 +010053 await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id});
54 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
55 const data = {
56 meta:{
57 type: "logChannelUpdate",
58 displayName: "Log Channel Changed",
59 calculateType: "nucleusSettingsUpdated",
60 color: NucleusColors.yellow,
61 emoji: "CHANNEL.TEXT.EDIT",
62 timestamp: new Date().getTime()
63 },
64 list: {
65 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
66 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
67 channel: entry(channel.id, renderChannel(channel))
68 },
69 hidden: {
70 guild: channel.guild.id
pineafanda6e5342022-07-03 10:03:16 +010071 }
pineafan63fc5e22022-08-04 22:04:10 +010072 };
73 log(data);
pineafan6702cef2022-06-13 17:52:37 +010074 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010075 console.log(e);
pineafan4edb7762022-06-26 19:21:04 +010076 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010077 .setTitle("Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010078 .setDescription("Something went wrong and the log channel could not be set")
pineafan6702cef2022-06-13 17:52:37 +010079 .setStatus("Danger")
80 .setEmoji("CHANNEL.TEXT.DELETE")
81 ], components: []});
82 }
83 } else {
pineafan4edb7762022-06-26 19:21:04 +010084 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010085 .setTitle("Log Channel")
pineafan63fc5e22022-08-04 22:04:10 +010086 .setDescription("No changes were made")
pineafan6702cef2022-06-13 17:52:37 +010087 .setStatus("Success")
88 .setEmoji("CHANNEL.TEXT.CREATE")
89 ], components: []});
90 }
91 }
92 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +010093 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010094 let channel = data.logging.logs.channel;
95 while (true) {
pineafan4edb7762022-06-26 19:21:04 +010096 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010097 .setTitle("Log channel")
98 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
99 .setStatus("Success")
100 .setEmoji("CHANNEL.TEXT.CREATE")
101 ], components: [new MessageActionRow().addComponents([new MessageButton()
102 .setCustomId("clear")
103 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
104 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
105 .setStyle("DANGER")
106 .setDisabled(!channel)
107 ])]});
108 let i;
109 try {
pineafanc6158ab2022-06-17 16:34:07 +0100110 i = await m.awaitMessageComponent({time: 300000});
pineafan63fc5e22022-08-04 22:04:10 +0100111 } catch(e) { break; }
112 i.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100113 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100114 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100115 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100116 clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100117 await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]);
pineafan6702cef2022-06-13 17:52:37 +0100118 channel = undefined;
119 }
120 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100121 break;
pineafan6702cef2022-06-13 17:52:37 +0100122 }
123 }
pineafan4edb7762022-06-26 19:21:04 +0100124 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100125 .setTitle("Log channel")
126 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
127 .setStatus("Success")
128 .setEmoji("CHANNEL.TEXT.CREATE")
129 .setFooter({text: "Message closed"})
130 ], components: [new MessageActionRow().addComponents([new MessageButton()
131 .setCustomId("clear")
132 .setLabel("Clear")
133 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
134 .setStyle("SECONDARY")
135 .setDisabled(true)
136 ])]});
pineafan63fc5e22022-08-04 22:04:10 +0100137};
pineafan6702cef2022-06-13 17:52:37 +0100138
139const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100140 const member = (interaction.member as Discord.GuildMember);
141 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command";
pineafan6702cef2022-06-13 17:52:37 +0100142 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100143};
pineafan6702cef2022-06-13 17:52:37 +0100144
145export { command };
146export { callback };
147export { check };