Disord Bot根据用户请求分配角色

时间:2020-05-21 08:03:58

标签: javascript discord discord.js

我对构建自己的Bot还是很陌生的,到目前为止,我希望用户能够使用命令!Sub“ Their ID” ,然后我的机器人会提取信息从我的Web API中分配给他们的角色 例。 ID 1 =高级 ID 2 =超级高级

到目前为止,我已经知道了:

const request = require('request');

request('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', { json: true }, (err, res, body) => {
  if (err) { return console.log(err); }
  console.log(body.url);
  console.log(body.explanation);
});

现在,如果我要分配角色,它的外观如何...

谢谢!

1 个答案:

答案 0 :(得分:0)

根据我的理解,这就是你想发生的事情:

用户:“!Sub 1”
机器人:*为用户分配角色1 *

如果这就是您想要做的,那么我将按照以下步骤进行操作:

  • 您需要存储角色ID,以便以后分配它们。您还可以使用角色名称,但是您需要记住,如果不更新代码,就无法更改它。
  • 然后您需要使用GuildMember.roles.add()将角色添加到用户。

这是一个例子:

// I'll assume that you already have the command structure set up, so these variables will be set:
// - message: The message with the command (its content will be '!Sub 1' in this case)

let role = message.guild.roles.cache.find(r => r.name == 'Premium') // You can use you role's name here
let roleID = '1234...' // This would be your role ID

message.member.roles.add(role || roleID)

这就是添加角色的方法。您只需要根据用户输入的内容切换角色名称/ ID,这将是您的第一个参数。