110 lines
4.7 KiB
TypeScript
110 lines
4.7 KiB
TypeScript
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";
|
|
import { Color, Emoji } from "../tools/design";
|
|
import { getGuildChannel, getChannelPermission } from "../tools/discord";
|
|
|
|
const builder = new SlashCommandBuilder();
|
|
builder.setName("showsettings");
|
|
builder.setDescription("Show the current settings of this guild");
|
|
builder.setDMPermission(false);
|
|
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: {
|
|
guildID: interaction.guildId
|
|
}
|
|
});
|
|
|
|
const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
|
|
|
|
const embed = getDefaultEmbed();
|
|
embed.setTitle(`Settings from guild ${interaction.guild?.name || ""} (${interaction.guildId})`);
|
|
embed.setDescription("Thanks for using this bot");
|
|
embed.setColor(Color.INFORMING_BLUE);
|
|
embed.addFields({
|
|
name: "Logchannel",
|
|
value: settings.notificationChannelID ? `<#${settings.notificationChannelID}>` : "Not configured",
|
|
inline: true
|
|
}, {
|
|
name: "Words in Blocklist",
|
|
value: `${wordCount}/100`,
|
|
inline: true
|
|
}, {
|
|
name: `Preserve data on server leave is ${settings.preserveDataOnGuildLeave ? "active" : "inactive"}`,
|
|
value: settings.preserveDataOnGuildLeave ? "Your settings will be saved even if the bot gets kicked" : "Your settings will be deleted as soon as the bot leaves this server"
|
|
}, {
|
|
name: `${Emoji.WAVING} Found a bug? Want to request a feature? Say hello?`,
|
|
value: "Join the support server at https://go.astrogd.eu/discord"
|
|
});
|
|
|
|
const embeds = [];
|
|
embeds.push(embed);
|
|
|
|
const invalidLogChannelEmbed = getDefaultEmbed();
|
|
invalidLogChannelEmbed.setColor(Color.WARNING_YELLOW);
|
|
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)) {
|
|
embeds.push(invalidLogChannelEmbed);
|
|
}
|
|
}
|
|
|
|
if ((!logChannel || !logChannel.isTextBased()) && settings.notificationChannelID) {
|
|
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
|
|
}).catch(() => {});
|
|
}
|
|
|
|
export {
|
|
builder,
|
|
execute
|
|
} |