From ddf28c1612bd2210f081ba7def8db92ffb9f6517 Mon Sep 17 00:00:00 2001 From: Lukas | AstroGD Date: Tue, 29 Nov 2022 03:04:29 +0100 Subject: [PATCH] Show missing permissions when using /showsettings --- src/commands/showSettings.ts | 40 ++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/commands/showSettings.ts b/src/commands/showSettings.ts index af03455..6e0f4f2 100644 --- a/src/commands/showSettings.ts +++ b/src/commands/showSettings.ts @@ -1,4 +1,5 @@ -import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; +import { ChatInputCommandInteraction, Guild, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; +import client from "../client"; import { Badword, database } from "../data"; import { getGuildSetting } from "../tools/data"; import getDefaultEmbed from "../tools/defaultEmbeds"; @@ -14,6 +15,12 @@ builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild); async function execute(interaction: ChatInputCommandInteraction): Promise { if (!interaction.inGuild()) throw new Error("Interaction was performed outside guild context"); + const guild = await new Promise((resolve) => { + client.guilds.fetch(interaction.guildId).catch(() => {resolve(null)}).then((guild) => {resolve(guild || null)}); + }); + + if (!guild) throw new Error("Guild is unavailable"); + const settings = await getGuildSetting(interaction.guildId); const wordCount = await database.getRepository(Badword).count({ where: { @@ -48,9 +55,14 @@ async function execute(interaction: ChatInputCommandInteraction): Promise const invalidLogChannelEmbed = getDefaultEmbed(); invalidLogChannelEmbed.setColor(Color.WARNING_YELLOW); - invalidLogChannelEmbed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} Logchannel is misconfigured!`); + invalidLogChannelEmbed.setTitle(`${Emoji.CHAT_DENY} Logchannel is misconfigured!`); invalidLogChannelEmbed.setDescription("The bot is unable to read and/or write in the configured logChannel. Please adjust your permissions."); + const missingPermissionEmbed = getDefaultEmbed(); + missingPermissionEmbed.setColor(Color.WARNING_YELLOW); + missingPermissionEmbed.setTitle(`${Emoji.CHAT_DENY} Bot is missing permissions to function properly!`); + missingPermissionEmbed.setDescription("Without these missing Permissions, the bot won't work properly and might lead to unexpected behavior"); + if (logChannel && logChannel.isTextBased()) { const permissions = await getChannelPermission(logChannel); if (!permissions || !permissions.has(PermissionFlagsBits.ViewChannel) || !permissions.has(PermissionFlagsBits.SendMessages)) { @@ -62,6 +74,30 @@ async function execute(interaction: ChatInputCommandInteraction): Promise embeds.push(invalidLogChannelEmbed); } + const member = await guild.members.fetchMe(); + if (!member.permissions.has(PermissionFlagsBits.ViewAuditLog)) { + missingPermissionEmbed.addFields({ + name: "View Audit Logs", + value: "With this permission the bot can determine who created or updated a channel with a blocked word" + }); + } + + if (!member.permissions.has(PermissionFlagsBits.ViewChannel)) { + missingPermissionEmbed.addFields({ + name: "View Channels", + value: "If the bot can't see a channel, it won't be able to detect blocked words in it" + }); + } + + if (!member.permissions.has(PermissionFlagsBits.ManageChannels)) { + missingPermissionEmbed.addFields({ + name: "Manage Channels", + value: "If the bot can't delete a channel, it can't protect your server from channels with blocked words" + }); + } + + if (missingPermissionEmbed.data.fields?.length || 0 > 0) embeds.push(missingPermissionEmbed); + interaction.reply({ embeds: embeds, ephemeral: true