如何使用命令分配角色 - discord.js

时间:2021-02-19 10:53:35

标签: javascript node.js discord.js

我想为我的机器人提供使用命令分配角色的功能

例如,ID SENTENCE 1 I live with my family, and my dog and my cat 1 cow 1 rockstar 2 others 会赋予 +mod @user Mod 的角色。

我的 @user 中的代码:

main.js

我的 if(command == 'mod'){ client.commands.get('mod').execute(message, args); } 文件中的代码:

mod.js

我收到一条错误消息,指出该成员为空。我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

我不认为它说 member 是空的。 users 没有角色,members 有。因此,您需要获得第一个 mentioned member

module.exports = {
  name: 'mod',
  description: 'This command gives member the Mod role',
  async execute(message, args) {
    const member = message.mentions.members.first();
    if (!member) {
      return message.reply('you need to mention someone');
    }
    try {
      await member.roles.add('ROLE_ID');
      message.reply(`${member} is now a mod ?`);
    } catch (error) {
      console.log(error);
      message.reply('Oops, there was an error');
    }
  },
};