如何为新成员分配角色?

时间:2019-04-27 14:21:15

标签: node.js discord.js

这是我的代码:

client.on('guildMemberAdd', member => {
  const channel = member.guild.channels.find(ch => ch.name === 'welcome');
  if (!channel) return;

  channel.send(message.author.toString() + ', Welcome!');

  member.addRole(member.guild.roles.find("name", "Guest"));
});

我正在努力做到这一点,以便为新成员分配“来宾”角色,但这种方法不起作用。
这是错误消息:

(node:677408) DeprecationWarning: Collection#find: pass a function instead
<@321191321437995009>, Please repeat these numbers to me to verify that you are a human: 421792
(node:677408) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at item.request.gen.end (C:\Users\PlusTwenty\Documents\Bots\Welcome\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:79:15)
    at then (C:\Users\PlusTwenty\Documents\Bots\Welcome\node_modules\snekfetch\src\index.js:215:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:677408) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

1 个答案:

答案 0 :(得分:0)

这是您出问题的地方。

member.addRole(member.guild.roles.find("name", "Guest"));

相反,您需要在实际调用添加角色函数之前定义角色。 (不是真的,但是推荐)

var role = member.guild.roles.find("name", "Guild");

(如果您不这样做,可以随时这样做)

member.addRole(member.guild.roles.find("name","Guild").id);

即使在此之后,您也需要ID,因为添加角色功能只能接受Snowflakes

//The only reason I use var is because I can redefine it later
role = role.id;

然后,调用添加角色函数。

member.addRole(role);

完成!

如果看到有关DepreciationWarning的错误,可以忽略,但建议也使用以下代码行来定义角色:

var role = member.guild.roles.get(r=>["Guest"].includes(r.name));
role = role.id
//continue with code, but define role in member.AddRole

For more info, click here

编辑2:

该机器人缺少烫发。确保该机器人位于访客角色的顶部/上方。