pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from './../../../utils/defaultEmbeds.js'; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 2 | import { ChannelType } from 'discord-api-types'; |
| 3 | import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 5 | import confirmationMessage from "../../../utils/confirmationMessage.js"; |
| 6 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
| 7 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 8 | import { WrappedCheck } from "jshaiku"; |
| 9 | import client from "../../../utils/client.js"; |
| 10 | |
| 11 | const 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 | |
| 19 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 20 | let m; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 21 | m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 22 | if (interaction.options.getChannel("channel")) { |
| 23 | let channel |
| 24 | try { |
| 25 | channel = interaction.options.getChannel("channel") |
| 26 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 27 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 28 | .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 |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 35 | if (channel.guild.id !== interaction.guild.id) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 36 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 37 | .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) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 50 | if (confirmation.cancelled) return |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 51 | if (confirmation.success) { |
| 52 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id}) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 54 | 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 {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | } catch (e) { |
| 77 | console.log(e) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 78 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 79 | .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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 86 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 87 | .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; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | let data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 96 | let channel = data.logging.logs.channel; |
| 97 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 98 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 99 | .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 { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 112 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 113 | } catch(e) { break } |
| 114 | i.deferUpdate() |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 115 | if (i.component.customId === "clear") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 117 | if (clicks === 2) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 118 | clicks = 0; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 119 | await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 120 | channel = undefined; |
| 121 | } |
| 122 | } else { |
| 123 | break |
| 124 | } |
| 125 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 126 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 127 | .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 | |
| 141 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 142 | let member = (interaction.member as Discord.GuildMember) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 143 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command" |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
| 147 | export { command }; |
| 148 | export { callback }; |
| 149 | export { check }; |