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