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"; |
| 3 | import generateEmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
| 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; |
| 20 | m = await interaction.reply({embeds: [new generateEmojiEmbed() |
| 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 { |
| 30 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 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) { |
| 39 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 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 { |
| 55 | await client.database.write(interaction.guild.id, {"logging.logs.channel": channel.id}) |
| 56 | } catch (e) { |
| 57 | console.log(e) |
| 58 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 59 | .setTitle("Log Channel") |
| 60 | .setDescription(`Something went wrong and the log channel could not be set`) |
| 61 | .setStatus("Danger") |
| 62 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 63 | ], components: []}); |
| 64 | } |
| 65 | } else { |
| 66 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 67 | .setTitle("Log Channel") |
| 68 | .setDescription(`No changes were made`) |
| 69 | .setStatus("Success") |
| 70 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 71 | ], components: []}); |
| 72 | } |
| 73 | } |
| 74 | let clicks = 0; |
| 75 | let data = await client.database.read(interaction.guild.id); |
| 76 | let channel = data.logging.logs.channel; |
| 77 | while (true) { |
| 78 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 79 | .setTitle("Log channel") |
| 80 | .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel") |
| 81 | .setStatus("Success") |
| 82 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 83 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 84 | .setCustomId("clear") |
| 85 | .setLabel(clicks ? "Click again to confirm" : "Reset channel") |
| 86 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 87 | .setStyle("DANGER") |
| 88 | .setDisabled(!channel) |
| 89 | ])]}); |
| 90 | let i; |
| 91 | try { |
| 92 | i = await m.awaitMessageComponent({time: 600000}); |
| 93 | } catch(e) { break } |
| 94 | i.deferUpdate() |
| 95 | if (i.component.customId == "clear") { |
| 96 | clicks += 1; |
| 97 | if (clicks == 2) { |
| 98 | clicks = 0; |
| 99 | await client.database.write(interaction.guild.id, {}, ["logging.logs.channel"]) |
| 100 | channel = undefined; |
| 101 | } |
| 102 | } else { |
| 103 | break |
| 104 | } |
| 105 | } |
| 106 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 107 | .setTitle("Log channel") |
| 108 | .setDescription(channel ? `Your log channel is currently set to <#${channel}>` : "This server does not have a log channel") |
| 109 | .setStatus("Success") |
| 110 | .setEmoji("CHANNEL.TEXT.CREATE") |
| 111 | .setFooter({text: "Message closed"}) |
| 112 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 113 | .setCustomId("clear") |
| 114 | .setLabel("Clear") |
| 115 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 116 | .setStyle("SECONDARY") |
| 117 | .setDisabled(true) |
| 118 | ])]}); |
| 119 | } |
| 120 | |
| 121 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 122 | let member = (interaction.member as Discord.GuildMember) |
| 123 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the `manage_server` permission to use this command" |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | export { command }; |
| 128 | export { callback }; |
| 129 | export { check }; |