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"; |
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 { 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") |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 27 | await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 37 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 46 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 58 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 71 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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) { |
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("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) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 100 | let data = client.database.guilds.read(interaction.guild.id) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 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") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 105 | await client.database.guilds.append(interaction.guild.id, data) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 106 | } else { |
| 107 | await client.database.guilds.remove(interaction.guild.id, data) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 108 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 109 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger |
| 110 | try { |
| 111 | let data = { |
| 112 | meta:{ |
| 113 | type: 'logIgnoreUpdated', |
| 114 | displayName: 'Ignored Groups Changed', |
| 115 | calculateType: 'nucleusSettingsUpdated', |
| 116 | color: NucleusColors.yellow, |
| 117 | emoji: "CHANNEL.TEXT.EDIT", |
| 118 | timestamp: new Date().getTime() |
| 119 | }, |
| 120 | list: { |
| 121 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 122 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 123 | channel: entry(channel.id, renderChannel(channel)), |
| 124 | }, |
| 125 | hidden: { |
| 126 | guild: interaction.guild.id |
| 127 | } |
| 128 | } |
| 129 | log(data); |
| 130 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 136 | let member = (interaction.member as Discord.GuildMember) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 137 | 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] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | export { command }; |
| 142 | export { callback }; |
| 143 | export { check }; |