Check for Logchannel permissions before accepting it
This commit is contained in:
parent
3a6641be86
commit
a8cd924e18
@ -1,8 +1,8 @@
|
||||
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel } from "discord.js";
|
||||
import getDefaultEmbed, { getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { database, GuildSetting } from "../data";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
import { getGuildChannel, getChannelPermission } from "../tools/discord";
|
||||
import { Emoji } from "../tools/design";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
@ -20,7 +20,7 @@ builder.addChannelOption((option) => {
|
||||
|
||||
async function resetNotificationChannel(guildSetting: GuildSetting, interaction: ChatInputCommandInteraction): Promise<void> {
|
||||
const logChannel = guildSetting.notificationChannelID ? await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID) : null;
|
||||
|
||||
|
||||
guildSetting.notificationChannelID = null;
|
||||
await database.getRepository(GuildSetting).save(guildSetting);
|
||||
|
||||
@ -31,7 +31,7 @@ async function resetNotificationChannel(guildSetting: GuildSetting, interaction:
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
|
||||
|
||||
if (logChannel && logChannel.isTextBased()) {
|
||||
logChannel.send({
|
||||
embeds: [logEmbed]
|
||||
@ -55,6 +55,20 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
if (!optionVal) return await resetNotificationChannel(guildSetting, interaction);
|
||||
|
||||
const channel = getTextBasedChannel(optionVal);
|
||||
if (channel.isDMBased()) return;
|
||||
|
||||
const permissions = await getChannelPermission(channel);
|
||||
if (!permissions || !permissions.has(PermissionFlagsBits.ViewChannel) || !permissions.has(PermissionFlagsBits.SendMessages)) {
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`Bot doesn't have permission to view and/or write in channel <#${channel.id}>`);
|
||||
interaction.reply({
|
||||
embeds: [ embed ],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (guildSetting.notificationChannelID) {
|
||||
const oldLogChannel = await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID);
|
||||
const embed = getDefaultEmbed();
|
||||
@ -84,14 +98,14 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
});
|
||||
|
||||
channel.send({
|
||||
embeds: [ embed ]
|
||||
embeds: [embed]
|
||||
}).catch();
|
||||
|
||||
const reply = getSuccessEmbed();
|
||||
reply.setDescription(`Log channel was set to <#${channel.id}>`);
|
||||
|
||||
interaction.reply({
|
||||
embeds: [ reply ],
|
||||
embeds: [reply],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user