javascript TypeError:无法读取未定义的属性(阅读“has”)

dm7nw8vv  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(256)

我尝试在Discord.js v14上执行清除命令,但它一直显示以下错误:

TypeError: Cannot read properties of undefined (reading 'has')

下面是代码:

const { SlashCommandBuilder, Embed, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('clear')
        .setDescription("Delete messages")
        .addIntegerOption(option => option.setName('messages').setDescription('Messages to delete').setRequired(true)),
    async execute(interaction) {
        const messages = interaction.options.getInteger('messages');
        
        if(!interaction.user.id.permissions.has(PermissionsBitField.Flags.ManageMessages)){
            interaction.reply({ content: "You can't send messages!", ephemeral: true} )
        }
        
    }
}
dsf9zpds

dsf9zpds1#

interaction.user.id提供了一个字符串,其中包含运行斜杠命令的用户的ID。要检查权限,您需要用户的GuildMember数据,您可以使用interaction.member访问该数据。因此,您的修复代码如下所示:

const { SlashCommandBuilder, Embed, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('clear')
        .setDescription("Delete messages")
        .addIntegerOption(option => option.setName('messages').setDescription('Messages to delete').setRequired(true)),
    async execute(interaction) {
        const messages = interaction.options.getInteger('messages');
        
        if(!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)){
            interaction.reply({ content: "You can't send messages!", ephemeral: true} )
        }
        
    }
}

相关问题