javascript Discord.js 问题与“.addRole”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50055133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:53:24  来源:igfitidea点击:

Discord.js Issue with ".addRole"

javascriptdiscorddiscord.jsroblox

提问by Persik

So, I have been having issues setting up a bot that roles a user to a role that includes emoticons. Example:

因此,我在设置一个机器人时遇到了问题,该机器人将用户角色设置为包含表情符号的角色。例子:

    const guildMember = message.member;
    guildMember.addRole('<@&439191493169643521>');

I've also tried:

我也试过:

    // content.js
    const guildMember = message.member;
    guildMember.addRole(config.n);

    // config.json
    {
        "n": "Fox"
    }

and also I've tried it without config.json, and just put the raw rank name, but it always doesn't work.

而且我也试过没有 config.json,只输入原始等级名称,但它总是不起作用。

This is the Console:

这是控制台:

      (node:15600) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Supplied parameter was neither a Role nor a Snowflake.
      (node:15600) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

回答by André

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Supplied parameter was neither a Role nor a Snowflake.

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Supplied parameter was neither a Role nor a Snowflake.

Well, Foxis not a Role Object and is also not a Snowflake (ID).
To add a role you need or a Object or a ID.

嗯,Fox不是角色对象,也不是雪花(ID)。
添加您需要的角色或对象或 ID。

If you want to use the ID, you will need to make the role mentionable and then escape the mention \@MyRole, then just copy the ID (it's only the numbers) and use it:

如果要使用 ID,则需要使角色可提及\@MyRole,然后转义提及,然后只需复制 ID(仅是数字)并使用它:

guildMember.addRole('439191493169643521');  

If you want to still go use the name of the role, you can do something like this:

如果你还想继续使用角色的名字,你可以这样做:

const role = message.guild.roles.find('name', 'MyRole');
guildMember.addRole(role);