pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 1 | import { ChannelType } from 'discord-api-types'; |
| 2 | import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 4 | import confirmationMessage from "../../../utils/confirmationMessage.js"; |
| 5 | import getEmojiByName from "../../../utils/getEmojiByName.js"; |
| 6 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 7 | import { WrappedCheck } from "jshaiku"; |
| 8 | import client from "../../../utils/client.js"; |
| 9 | |
| 10 | const 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 | |
| 18 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 19 | let m; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 20 | m = await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 30 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 39 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 55 | await client.database.guilds.write(interaction.guild.id, {"logging.logs.channel": channel.id}) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 56 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger |
| 57 | try { |
| 58 | let data = { |
| 59 | meta:{ |
| 60 | type: 'logChannelUpdate', |
| 61 | displayName: 'Log Channel Changed', |
| 62 | calculateType: 'nucleusSettingsUpdated', |
| 63 | color: NucleusColors.yellow, |
| 64 | emoji: "CHANNEL.TEXT.EDIT", |
| 65 | timestamp: new Date().getTime() |
| 66 | }, |
| 67 | list: { |
| 68 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 69 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 70 | channel: entry(channel.id, renderChannel(channel)), |
| 71 | }, |
| 72 | hidden: { |
| 73 | guild: channel.guild.id |
| 74 | } |
| 75 | } |
| 76 | log(data); |
| 77 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 78 | } catch (e) { |
| 79 | console.log(e) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 80 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 81 | .setTitle("Log Channel") |
| 82 | .setDescription(`Something went wrong and the log channel could not be set`) |
| 83 | .setStatus("Danger") |
| 84 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 85 | ], components: []}); |
| 86 | } |
| 87 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 88 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 89 | .setTitle("Log Channel") |
| 90 | .setDescription(`No changes were made`) |
| 91 | .setStatus("Success") |
| 92 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 93 | ], components: []}); |
| 94 | } |
| 95 | } |
| 96 | let clicks = 0; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 97 | let data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 98 | let channel = data.logging.logs.channel; |
| 99 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 100 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 101 | .setTitle("Log channel") |
| 102 | .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel") |
| 103 | .setStatus("Success") |
| 104 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 105 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 106 | .setCustomId("clear") |
| 107 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 108 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 109 | .setStyle("DANGER") |
| 110 | .setDisabled(!channel) |
| 111 | ])]}); |
| 112 | let i; |
| 113 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 114 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 115 | } catch(e) { break } |
| 116 | i.deferUpdate() |
| 117 | if (i.component.customId == "clear") { |
| 118 | clicks += 1; |
| 119 | if (clicks == 2) { |
| 120 | clicks = 0; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 121 | await client.database.guilds.write(interaction.guild.id, {}, ["logging.logs.channel"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 122 | channel = undefined; |
| 123 | } |
| 124 | } else { |
| 125 | break |
| 126 | } |
| 127 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 128 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 129 | .setTitle("Log channel") |
| 130 | .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel") |
| 131 | .setStatus("Success") |
| 132 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 133 | .setFooter({text: "Message closed"}) |
| 134 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 135 | .setCustomId("clear") |
| 136 | .setLabel("Clear") |
| 137 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 138 | .setStyle("SECONDARY") |
| 139 | .setDisabled(true) |
| 140 | ])]}); |
| 141 | } |
| 142 | |
| 143 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 144 | let member = (interaction.member as Discord.GuildMember) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 145 | 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] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | export { command }; |
| 150 | export { callback }; |
| 151 | export { check }; |