blob: 448f26ad7baf80313f0573a7fc1a6a2fc89b3584 [file] [log] [blame]
pineafan6702cef2022-06-13 17:52:37 +01001import { ChannelType } from 'discord-api-types';
2import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
3import generateEmojiEmbed from "../../../utils/generateEmojiEmbed.js";
4import 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;
20 m = await interaction.reply({embeds: [new generateEmojiEmbed()
21 .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 {
30 return await interaction.editReply({embeds: [new generateEmojiEmbed()
31 .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) {
39 return interaction.editReply({embeds: [new generateEmojiEmbed()
40 .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 {
55 await client.database.write(interaction.guild.id, {"logging.logs.channel": channel.id})
56 } catch (e) {
57 console.log(e)
58 return interaction.editReply({embeds: [new generateEmojiEmbed()
59 .setTitle("Log Channel")
60 .setDescription(`Something went wrong and the log channel could not be set`)
61 .setStatus("Danger")
62 .setEmoji("CHANNEL.TEXT.DELETE")
63 ], components: []});
64 }
65 } else {
66 return interaction.editReply({embeds: [new generateEmojiEmbed()
67 .setTitle("Log Channel")
68 .setDescription(`No changes were made`)
69 .setStatus("Success")
70 .setEmoji("CHANNEL.TEXT.CREATE")
71 ], components: []});
72 }
73 }
74 let clicks = 0;
75 let data = await client.database.read(interaction.guild.id);
76 let channel = data.logging.logs.channel;
77 while (true) {
78 await interaction.editReply({embeds: [new generateEmojiEmbed()
79 .setTitle("Log channel")
80 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
81 .setStatus("Success")
82 .setEmoji("CHANNEL.TEXT.CREATE")
83 ], components: [new MessageActionRow().addComponents([new MessageButton()
84 .setCustomId("clear")
85 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
86 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
87 .setStyle("DANGER")
88 .setDisabled(!channel)
89 ])]});
90 let i;
91 try {
pineafanc6158ab2022-06-17 16:34:07 +010092 i = await m.awaitMessageComponent({time: 300000});
pineafan6702cef2022-06-13 17:52:37 +010093 } catch(e) { break }
94 i.deferUpdate()
95 if (i.component.customId == "clear") {
96 clicks += 1;
97 if (clicks == 2) {
98 clicks = 0;
99 await client.database.write(interaction.guild.id, {}, ["logging.logs.channel"])
100 channel = undefined;
101 }
102 } else {
103 break
104 }
105 }
106 await interaction.editReply({embeds: [new generateEmojiEmbed()
107 .setTitle("Log channel")
108 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
109 .setStatus("Success")
110 .setEmoji("CHANNEL.TEXT.CREATE")
111 .setFooter({text: "Message closed"})
112 ], components: [new MessageActionRow().addComponents([new MessageButton()
113 .setCustomId("clear")
114 .setLabel("Clear")
115 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
116 .setStyle("SECONDARY")
117 .setDisabled(true)
118 ])]});
119}
120
121const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
122 let member = (interaction.member as Discord.GuildMember)
123 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the `manage_server` permission to use this command"
124 return true;
125}
126
127export { command };
128export { callback };
129export { check };