简体   繁体   English

如何在 DiscordJS v13 上为用户添加角色?

[英]How can I add a role to a user on DiscordJS v13?

I am having problems adding a role to a user with DiscordJS v13.我在使用 DiscordJS v13 向用户添加角色时遇到问题。 Is there any way to do it?有什么办法吗? Whenever I add them, they're not defined.每当我添加它们时,它们都没有定义。 Here's the code to where I want to add the add role.这是我要添加添加角色的位置的代码。 I'm a beginner on discord.js so bear with me.我是 discord.js 的初学者,所以请耐心等待。 Thanks!谢谢!

    } else if (commandName === 'verification'){
        let code1 = 'A96B2UUH'
        const code = options.getString('code')

        if(code === code1){
            interaction.reply({
                content: 'The code has been accepted. Welcome to the hub!',
                ephemeral: true,
            })
            let role = message.guild.roles.cache.some(role => role.name === 'Equitability')
            message.member.roles.add(role)
        }else{
            interaction.reply({
                content: 'You entered the wrong code. Please contact the administrators to gain access.',
                ephemeral: true,
            })
        }
    }
})

Edit: I fixed it!编辑:我修好了! I changed some things and somehow it worked.我改变了一些东西,不知何故它起作用了。 Here's the code.这是代码。

bot.on('interactionCreate', async interaction => {
    if(!interaction.isCommand()) return

    const { options } = interaction

    if(interaction.commandName === 'verification'){
        let code1 = 'A96B2UUH'
        const code = options.getString('code')

        if(code === code1){
            await interaction.reply({
                content: 'You entered the correct code. Welcome to the hub!',
                ephemeral: true,
            })
            var guild = bot.guilds.cache.get('907259574342537236')
            const member = await guild.members.fetch(interaction.user.id)
            const role = await guild.roles.fetch('907261775622328352')
            member.roles.add(role)
        }else{
            await interaction.reply({
                content: 'The code you entered is incorrect. Contact the administrators to gain access the server.',
                ephemeral: true,
            })
        }
    }
})

Getting roles in Discord.js v13 requires them to be cached, so the best way to add a role you know exists is simply by ID.在 Discord.js v13 中获取角色需要缓存它们,因此添加您知道存在的角色的最佳方法是简单地通过 ID。

To get an ID for a role open Server Settings > Roles > Right click > "Copy ID"要获取角色的 ID,请打开服务器设置 > 角色 > 右键单击​​ >“复制 ID”

Then you can simply do message.member.roles.add("roleId");然后你可以简单地做message.member.roles.add("roleId");

If you're not completely certain whether a role exists (ie through user input) you can fetch all server roles (different to .cache.get ), then user .filter , however this doesn't seem to be what you're doing so I won't include it如果您不能完全确定角色是否存在(即通过用户输入),您可以获取所有服务器角色(与.cache.get不同),然后是用户.filter ,但这似乎不是您在做什么所以我不会包括它

You are using .some() which returns a Boolean ( true / false ).您正在使用.some()返回一个Booleantrue / false )。 You want the role object, so you use .find() which takes the same arguments, but returns the role object instead.你想要角色对象,所以你使用.find()它接受相同的参数,但返回角色对象。 Also, if the role is not cached, you can use RoleManager.fetch() to get it from the API另外,如果角色没有被缓存,你可以使用RoleManager.fetch()从 API 中获取它

await message.guild.roles.fetch() //optional - put it if the role is valid, but is not cached
let role = message.guild.roles.cache.find(role => role.name === 'Equitability')
message.member.roles.add(role)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM