node.js - 如何通过 client.users 集合中的用户名查找用户

标签 node.js discord.js

我想按名称赋予该用户角色。 所以我想通过用户名获取id。

let role = message.guild.roles.find('name', 'TESTrole');
let id = client.users.get("name", "TESTname"); 
let member = message.member.guild.members.get(id);
member.addRole(role);

最佳答案

您可以使用find()方法。

let user = client.users.find("username", "TESTname");
//OR
let user = client.users.find(user => user.username == "TESTname");
//once you've found the user you can get the id (or you can write .id after the find method)
let id = user.id;

关于node.js - 如何通过 client.users 集合中的用户名查找用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51229193/

相关文章:

javascript - "client.on()"函数到底是如何工作的?

node.js - 如何使用 node-inspector 调试 nodeunit

node.js - Twilio - 创建新号码后需要从应用程序动态设置 Webhook

node.js - 与 Mongoose 相比,MongoDB 原生 Node.js 驱动程序的性能很糟糕,我做错了什么?

javascript - Discord.js:检查机器人权限

javascript - TypeError: this.inputMedia.pipe 不是函数

javascript - DiscordJS 中的 MessageCollector 不起作用

javascript - res.setHeader ("Set-Cookie", ...) 没有在 Node/Express 中设置 cookie

javascript - 使用 ES6 导入模块和 pkg

javascript - 时间.分钟和时间.秒未定义