NodeJS 为什么我在Discord.js中尝试使用我的ban命令时会出现“缺少权限”错误?

cqoc49vn  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(136)

我正在编写一个ban命令,但是当我在我的discord服务器上测试它并尝试它时,它不起作用,所以我添加了一行额外的代码,只是为了在控制台上记录错误消息,它说DiscordAPIError[50013]:缺少权限,即使在我添加我的机器人到服务器之前,我给了它管理员权限,所以他可以做任何事情,但它仍然出错,我没有看到我的代码有任何错误,即使是ESlint。

const {SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, PermissionFlagsBits, ActionRowBuilder} = require('discord.js');
const chalk = require('chalk');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ban')
        .setDescription('Bans a member')
        .addMentionableOption(option =>
            option
                .setName('target-user')
                .setDescription('The user you want to ban.')
                .setRequired(true))
        .addStringOption(option =>
            option
                .setName('reason')
                .setDescription("Reason for the user's ban."))
        .setDMPermission(false  )
        .setDefaultMemberPermissions(PermissionFlagsBits.BanMembers),
    async execute(interaction) {
        const targetuser = interaction.options.getMentionable('target-user', true);
        const Reason = interaction.options.getString('reason') ?? 'No reason provided';

        const confirm = new ButtonBuilder()
            .setCustomId('confirm')
            .setLabel('Yes')
            .setStyle(ButtonStyle.Danger);
        
        const cancel = new ButtonBuilder()
            .setCustomId('cancel')
            .setLabel('No')
            .setStyle(ButtonStyle.Secondary);

        const row = new ActionRowBuilder()
            .addComponents(confirm, cancel);
        
        const response = await interaction.reply({
            content: `Are you sure you want to ban this user?`,
            ephemeral: true,
            components: [row],
        });

        try {
            const confirmation = await response.awaitMessageComponent({time: 60_000, ComponentType: 2});

            if (confirmation.customId === 'confirm') {
                const embed = new EmbedBuilder()
                    .setTitle('User Banned!')
                    .setDescription(`User ${targetuser} has been banned by ${interaction.user}.`)
                    .addFields(
                        {name: 'Reason', value: Reason},
                    )
                    .setFooter({text: 'craig tucker'})
                    .setTimestamp();
                await interaction.guild.members.ban(targetuser, {reason: Reason});
                await interaction.channel.send({embeds: [embed]});
                await interaction.editReply({content: `User ${targetuser} banned successfully.`, components: [],});
            }
        } catch (error) {
            await interaction.editReply({
                content: "Confirmation not received within 1 minute, cancelling",
                components: [],
            });
            console.log(chalk.red(error));
        }
    },
};

错误发生在我点击确认禁令的按钮后,所以我尝试修改他的事件文件上的interactionCreate事件:

const {Events} = require("discord.js");
const chalk = require('chalk');

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (interaction.isChatInputCommand()) {
            const command = interaction.client.commands.get(interaction.commandName);

            if (!command) {
                console.log(chalk.red(`command ${interaction.commandName} does not exist.`));
                return;
            }

            try {
                await command.execute(interaction);
            } catch (error) {
                console.log(chalk.red(`error while trying execute "${interaction.commandName}" (command): ${error}`));
            }
        }  
    },
};

但根据discord.js指南,它非常好(由于与按钮和斜杠命令的交互是相同的),然后我检查了我的index.js文件,并试图通过添加GatewayIntentBits.AutoModerationExecution,来修改我的客户端intents变量,但仍然在控制台出错。

yacmzcpb

yacmzcpb1#

可能引发此错误的原因:

  • 机器人试图禁止自己
  • 机器人试图禁止公会所有者
  • 机器人是公会的主人
  • 机器人最高角色不高于目标最高角色
  • 机器人没有禁止公会成员的权限。

源1
来源2

相关问题