Show missing permissions when using /showsettings

This commit is contained in:
Lukas | AstroGD 2022-11-29 03:04:29 +01:00
parent 5fb9ad1e12
commit ddf28c1612
Signed by: AstroGD
GPG Key ID: 82A5E6C236C535AA

View File

@ -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<void> {
if (!interaction.inGuild()) throw new Error("Interaction was performed outside guild context");
const guild = await new Promise<null | Guild>((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<void>
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<void>
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