【问题标题】:Verify Slash command in Discord.JS V14在 Discord.JS V14 中验证斜杠命令
【发布时间】:2022-11-26 05:44:57
【问题描述】:

我正在执行验证命令,但我一直遇到错误。

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('verify')
        .setDescription('Select a member to verify')
        .addUserOption(option =>
            option
            .setName('user')
            .setDescription('The member to verify')
            .setRequired(true))
        .setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
        .setDMPermission(false),
    async execute(interaction){
        
        const role = interaction.guild.roles.cache.get('984119311851466822')
        const target = interaction.options.getUser('user')

        if(!role) {
            console.log("Role doesnt exist");
            return;
        }
        target.roles
          .add(role)
          .then((r) =>
            interaction.reply({
                content: `The ${role} was assigned to ${target}`,
                ephemeral: false,
            })
          )
          .catch((err) =>
            interaction.reply({
                content: `Something went wrong. Try again`,
                ephemeral: false,
            })
          )
        
    }
}

我得到的错误是:

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

我尝试登录 role 和 target 并获得了它们各自的 ID。

Bot 也具有所有必需的权限。

【问题讨论】:

    标签: discord discord.js verify


    【解决方案1】:

    如果您登录target 并获得了一个用户 ID,那么您就不能真正向它添加角色(因为它是一个雪花字符串)。也许先尝试获取用户?

    如果您愿意等待某些行,您还可以更简洁地编写执行函数。

    【讨论】:

    • 哎呀,对不起。我没有充分阅读提供的代码。用户没有角色,成员有。 Finbar 提供了正确的答案。
    猜你喜欢
    • 2022-12-05
    • 2022-01-07
    • 2021-11-09
    • 2022-01-03
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2021-12-15
    • 2021-06-08
    相关资源
    最近更新 更多