blob: 403010839f08f159b885f304c79eb13d486cc177 [file] [log] [blame]
pineafane23c4ec2022-07-27 21:56:27 +01001import { LoadingEmbed } from './../../../utils/defaultEmbeds.js';
pineafan6702cef2022-06-13 17:52:37 +01002import { ChannelType } from 'discord-api-types';
3import 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
13 .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 ]))
18
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")) {
23 let channel
24 try {
25 channel = interaction.options.getChannel("channel")
26 } 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")
32 ]})
33 }
34 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")
38 .setDescription(`You must choose a channel in this server`)
39 .setStatus("Danger")
40 .setEmoji("CHANNEL.TEXT.DELETE")
41 ]});
42 }
43 let confirmation = await new confirmationMessage(interaction)
44 .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)
49 .send(true)
pineafan02ba0232022-07-24 22:16:15 +010050 if (confirmation.cancelled) return
pineafan6702cef2022-06-13 17:52:37 +010051 if (confirmation.success) {
52 try {
pineafan4edb7762022-06-26 19:21:04 +010053 await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id})
pineafanda6e5342022-07-03 10:03:16 +010054 const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger
55 try {
56 let data = {
57 meta:{
58 type: 'logChannelUpdate',
59 displayName: 'Log Channel Changed',
60 calculateType: 'nucleusSettingsUpdated',
61 color: NucleusColors.yellow,
62 emoji: "CHANNEL.TEXT.EDIT",
63 timestamp: new Date().getTime()
64 },
65 list: {
66 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
67 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
68 channel: entry(channel.id, renderChannel(channel)),
69 },
70 hidden: {
71 guild: channel.guild.id
72 }
73 }
74 log(data);
75 } catch {}
pineafan6702cef2022-06-13 17:52:37 +010076 } catch (e) {
77 console.log(e)
pineafan4edb7762022-06-26 19:21:04 +010078 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010079 .setTitle("Log Channel")
80 .setDescription(`Something went wrong and the log channel could not be set`)
81 .setStatus("Danger")
82 .setEmoji("CHANNEL.TEXT.DELETE")
83 ], components: []});
84 }
85 } else {
pineafan4edb7762022-06-26 19:21:04 +010086 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010087 .setTitle("Log Channel")
88 .setDescription(`No changes were made`)
89 .setStatus("Success")
90 .setEmoji("CHANNEL.TEXT.CREATE")
91 ], components: []});
92 }
93 }
94 let clicks = 0;
pineafan4edb7762022-06-26 19:21:04 +010095 let data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010096 let channel = data.logging.logs.channel;
97 while (true) {
pineafan4edb7762022-06-26 19:21:04 +010098 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010099 .setTitle("Log channel")
100 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
101 .setStatus("Success")
102 .setEmoji("CHANNEL.TEXT.CREATE")
103 ], components: [new MessageActionRow().addComponents([new MessageButton()
104 .setCustomId("clear")
105 .setLabel(clicks ? "Click again to confirm" : "Reset channel")
106 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
107 .setStyle("DANGER")
108 .setDisabled(!channel)
109 ])]});
110 let i;
111 try {
pineafanc6158ab2022-06-17 16:34:07 +0100112 i = await m.awaitMessageComponent({time: 300000});
pineafan6702cef2022-06-13 17:52:37 +0100113 } catch(e) { break }
114 i.deferUpdate()
pineafane23c4ec2022-07-27 21:56:27 +0100115 if (i.component.customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100116 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100117 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100118 clicks = 0;
pineafane23c4ec2022-07-27 21:56:27 +0100119 await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"])
pineafan6702cef2022-06-13 17:52:37 +0100120 channel = undefined;
121 }
122 } else {
123 break
124 }
125 }
pineafan4edb7762022-06-26 19:21:04 +0100126 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100127 .setTitle("Log channel")
128 .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel")
129 .setStatus("Success")
130 .setEmoji("CHANNEL.TEXT.CREATE")
131 .setFooter({text: "Message closed"})
132 ], components: [new MessageActionRow().addComponents([new MessageButton()
133 .setCustomId("clear")
134 .setLabel("Clear")
135 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
136 .setStyle("SECONDARY")
137 .setDisabled(true)
138 ])]});
139}
140
141const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
142 let member = (interaction.member as Discord.GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100143 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command"
pineafan6702cef2022-06-13 17:52:37 +0100144 return true;
145}
146
147export { command };
148export { callback };
149export { check };