pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 1 | import { ChannelType } from 'discord-api-types'; |
| 2 | import Discord, { CommandInteraction } from "discord.js"; |
| 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 4 | import generateEmojiEmbed from "../../../utils/generateEmojiEmbed.js"; |
| 5 | import { WrappedCheck } from "jshaiku"; |
| 6 | import confirmationMessage from '../../../utils/confirmationMessage.js'; |
| 7 | import keyValueList from '../../../utils/generateKeyValueList.js'; |
| 8 | import client from '../../../utils/client.js'; |
| 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("ignore") |
| 13 | .setDescription("Sets which users, channels and roles should be ignored") |
| 14 | .addStringOption(o => o.setName("action").setDescription("Add or remove from the list").addChoices([ |
| 15 | ["Add", "add"], ["Remove", "remove"] |
| 16 | ]).setRequired(true)) |
| 17 | .addChannelOption(o => o.setName("addchannel").setDescription("Add a channel that should be ignored").addChannelTypes([ |
| 18 | ChannelType.GuildText, ChannelType.GuildVoice, ChannelType.GuildNews, ChannelType.GuildPublicThread, ChannelType.GuildPrivateThread, ChannelType.GuildNewsThread |
| 19 | ])) |
| 20 | .addUserOption(o => o.setName("adduser").setDescription("Add a user that should be ignored")) |
| 21 | .addRoleOption(o => o.setName("addrole").setDescription("Add a role that should be ignored")) |
| 22 | |
| 23 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 24 | let channel = interaction.options.getChannel("addchannel") |
| 25 | let user = interaction.options.getUser("adduser") |
| 26 | let role = interaction.options.getRole("addrole") |
| 27 | await interaction.reply({embeds: [new generateEmojiEmbed() |
| 28 | .setTitle("Loading") |
| 29 | .setStatus("Danger") |
| 30 | .setEmoji("NUCLEUS.LOADING") |
| 31 | ], ephemeral: true, fetchReply: true}); |
| 32 | if (channel || user || role) { |
| 33 | if (channel) { |
| 34 | try { |
| 35 | channel = interaction.guild.channels.cache.get(channel.id) |
| 36 | } catch { |
| 37 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 38 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 39 | .setTitle("Logs > Ignore") |
| 40 | .setDescription("The channel you provided is not a valid channel") |
| 41 | .setStatus("Danger") |
| 42 | ]}) |
| 43 | } |
| 44 | channel = channel as Discord.TextChannel |
| 45 | if (channel.guild.id != interaction.guild.id) { |
| 46 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 47 | .setTitle("Logs > Ignore") |
| 48 | .setDescription(`You must choose a channel in this server`) |
| 49 | .setStatus("Danger") |
| 50 | .setEmoji("CHANNEL.TEXT.DELETE") |
| 51 | ]}); |
| 52 | } |
| 53 | } |
| 54 | if (user) { |
| 55 | try { |
| 56 | user = interaction.guild.members.cache.get(user.id).user |
| 57 | } catch { |
| 58 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 59 | .setEmoji("USER.DELETE") |
| 60 | .setTitle("Logs > Ignore") |
| 61 | .setDescription("The user you provided is not a valid user") |
| 62 | .setStatus("Danger") |
| 63 | ]}) |
| 64 | } |
| 65 | user = user as Discord.User |
| 66 | } |
| 67 | if (role) { |
| 68 | try { |
| 69 | role = interaction.guild.roles.cache.get(role.id) |
| 70 | } catch { |
| 71 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 72 | .setEmoji("ROLE.DELETE") |
| 73 | .setTitle("Logs > Ignore") |
| 74 | .setDescription("The role you provided is not a valid role") |
| 75 | .setStatus("Danger") |
| 76 | ]}) |
| 77 | } |
| 78 | role = role as Discord.Role |
| 79 | if (role.guild.id != interaction.guild.id) { |
| 80 | return interaction.editReply({embeds: [new generateEmojiEmbed() |
| 81 | .setTitle("Logs > Ignore") |
| 82 | .setDescription(`You must choose a role in this server`) |
| 83 | .setStatus("Danger") |
| 84 | .setEmoji("ROLE.DELETE") |
| 85 | ]}); |
| 86 | } |
| 87 | } |
| 88 | let changes = {} |
| 89 | if (channel) changes["channel"] = channel.id |
| 90 | if (user) changes["user"] = user.id |
| 91 | if (role) changes["role"] = role.id |
| 92 | let confirmation = await new confirmationMessage(interaction) |
| 93 | .setEmoji("NUCLEUS.COMMANDS.IGNORE") |
| 94 | .setTitle("Logs > Ignore") |
| 95 | .setDescription(keyValueList(changes) |
| 96 | + `Are you sure you want to **${interaction.options.getString("action") == "add" ? "add" : "remove"}** these to the ignore list?`) |
| 97 | .setColor("Warning") |
| 98 | .send(true) |
| 99 | if (confirmation.success) { |
| 100 | let data = client.database.read(interaction.guild.id) |
| 101 | if (channel) data.logging.logs.ignore.channels.concat([channel.id]) |
| 102 | if (user) data.logging.logs.ignore.users.concat([user.id]) |
| 103 | if (role) data.logging.logs.ignore.roles.concat([role.id]) |
| 104 | if (interaction.options.getString("action") == "add") { |
| 105 | await client.database.append(interaction.guild.id, data) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 112 | let member = (interaction.member as Discord.GuildMember) |
| 113 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the `manage_server` permission to use this command" |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | export { command }; |
| 118 | export { callback }; |
| 119 | export { check }; |