javascript Discord.js斜线命令.getMember()和.getChannel()返回空值,即使这些选项是必需的

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

编辑/解决方案:需要在开发者门户上启用公会成员意图,并且您的机器人客户端必须使用const client = new Client({ intents: [..., GatewayIntentBits.GuildMembers] });登录

我有一个Discord.js机器人,命令定义如下:

const { SlashCommandBuilder } = require("discord.js");

module.exports = {
    data: new SlashCommandBuilder()
        .setName('removerole')
        .setDescription("Remove a role from a user. Officer use only.")
        .addUserOption(option =>
            option.setName('target')
                .setDescription("The user whose role should be removed.")
                .setRequired(true))
        .addChannelOption(option =>
            option.setName('gamechannel')
                .setDescription("The channel that is associated with the role to be removed.")
                .setRequired(true)),

    async execute(interaction) {
        await interaction.deferReply({ ephemeral: true });

        //collect member
        const member = interaction.options.getMember('target');
        console.log(`member is ${member}`);
        console.log(`user is ${interaction.options.getUser('target')}`);
        
        //rest of function...
    }
}

当我输入这个...

......这是印刷的:

member is null
user is null

这个函数的其余部分依赖于这些变量,因为它试图读取一个null值,所以抛出了一个错误,斜杠命令选项是必需的,所以它们应该有一个值,但是getUser()和getMember()仍然返回null,我知道我遗漏了一些东西,但是我已经把它修改得尽可能接近指南,应该可以工作。
为什么值为空?

hi3rlvi2

hi3rlvi21#

确保您启用了所需的Intent,即GuildMembers和您正在使用的任何其他数据。
Which intents are available
什么是意图

相关问题