【问题标题】:How to assign roles with a command - discord.js如何使用命令分配角色 - discord.js
【发布时间】:2021-05-22 08:43:02
【问题描述】:

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

例如,+mod @user 将赋予 @user Mod 角色。

代码在我的main.js:

if(command == 'mod'){
    client.commands.get('mod').execute(message, args);
}

我的mod.js 文件中的代码:

module.exports = {
    name: 'mod',
    description: "This command gives member the Mod role",
    execute(message, args){
        const member = message.mentions.users.first();
        member.roles.add('role ID xxxx');
    }
}

我收到一条错误消息,提示该成员为空。我做错了吗?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    我不认为它说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');
        }
      },
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-07
      • 2019-04-14
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多