pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js"; |
| 2 | import { ChannelType } from "discord-api-types"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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 | ])); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 18 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 19 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
| 20 | const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Discord.Message; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 21 | if (interaction.options.getChannel("channel")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | let channel; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 23 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | channel = interaction.options.getChannel("channel"); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 25 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 26 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 27 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 28 | .setTitle("Log Channel") |
| 29 | .setDescription("The channel you provided is not a valid channel") |
| 30 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | ]}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 32 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | channel = channel as Discord.TextChannel; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 34 | if (channel.guild.id !== interaction.guild.id) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 35 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 36 | .setTitle("Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | .setDescription("You must choose a channel in this server") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 38 | .setStatus("Danger") |
| 39 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 40 | ]}); |
| 41 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 43 | .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) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | .send(true); |
| 49 | if (confirmation.cancelled) return; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 50 | if (confirmation.success) { |
| 51 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | 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 |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 70 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | }; |
| 72 | log(data); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 73 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | console.log(e); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 75 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | .setTitle("Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 77 | .setDescription("Something went wrong and the log channel could not be set") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 78 | .setStatus("Danger") |
| 79 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 80 | ], components: []}); |
| 81 | } |
| 82 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 83 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 84 | .setTitle("Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | .setDescription("No changes were made") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 86 | .setStatus("Success") |
| 87 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 88 | ], components: []}); |
| 89 | } |
| 90 | } |
| 91 | let clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 92 | const data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 93 | let channel = data.logging.logs.channel; |
| 94 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 96 | .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 { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 109 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 110 | } catch(e) { break; } |
| 111 | i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 112 | if (i.component.customId === "clear") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 113 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 114 | if (clicks === 2) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 115 | clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 116 | await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 117 | channel = undefined; |
| 118 | } |
| 119 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | break; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 123 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 124 | .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 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 137 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 138 | const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 139 | 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"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 141 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 142 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 143 | |
| 144 | export { command }; |
| 145 | export { callback }; |
| 146 | export { check }; |