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 | |
| 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")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | let channel; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 24 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | channel = interaction.options.getChannel("channel"); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | ]}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 33 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | .setDescription("You must choose a channel in this server") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 39 | .setStatus("Danger") |
| 40 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 41 | ]}); |
| 42 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | .send(true); |
| 50 | if (confirmation.cancelled) return; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 51 | if (confirmation.success) { |
| 52 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | 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 |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 71 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | }; |
| 73 | log(data); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 74 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | console.log(e); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 76 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 77 | .setTitle("Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | .setDescription("Something went wrong and the log channel could not be set") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 79 | .setStatus("Danger") |
| 80 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 81 | ], components: []}); |
| 82 | } |
| 83 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 84 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 85 | .setTitle("Log Channel") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 86 | .setDescription("No changes were made") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 87 | .setStatus("Success") |
| 88 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 89 | ], components: []}); |
| 90 | } |
| 91 | } |
| 92 | let clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | const data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 94 | let channel = data.logging.logs.channel; |
| 95 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 96 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 97 | .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 { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 110 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | } catch(e) { break; } |
| 112 | i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 113 | if (i.component.customId === "clear") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 114 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 115 | if (clicks === 2) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 117 | await client.database.guilds.write(interaction.guild.id, null, ["logging.logs.channel"]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 118 | channel = undefined; |
| 119 | } |
| 120 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | break; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 124 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 125 | .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 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 137 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 138 | |
| 139 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 140 | 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"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 142 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 144 | |
| 145 | export { command }; |
| 146 | export { callback }; |
| 147 | export { check }; |