[英]Discord.js Get the member in a guild by username
I was trying to get all the member's usernames on my discord server我试图在我的 discord 服务器上获取所有成员的用户名
However, it just gave back 8-9/25 members including 6 BOTs然而,它只退还了 8-9/25 个成员,包括 6 个 BOT
Here is my code这是我的代码
const list = client.guilds.cache.get(msg.guildId);
list.members.cache.each(r => {
console.log(r.user.username)
})
Besides that, I have also tried these ways除此之外,我还尝试了这些方法
const list = client.guilds.cache.get(msg.guildId);
list.members.cache.array().forEach(r => {
console.log(r.user.username)
})
const user = client.users.cache.find(u => u.username == '//The Given username')
const user = msg.guild.roles.cache.get('//The Main Role in my Server')
.members.map(m => m.user.username == '//The given username')
let user = client.users.cache.get('username', '//The given username');
But sometimes it just returned my and my BOT's username or undefined.但有时它只是返回我和我的 BOT 的用户名或未定义。
I'm trying to get all the members on the server and from that, I can find out the user has the same username with the given username, not tags or IDs我正在尝试获取服务器上的所有成员,从中,我可以发现用户与给定的用户名具有相同的用户名,而不是标签或 ID
I have turned on the Gateway Intents but it didn't help我已经打开了网关意图,但它没有帮助
Please help with my project, I really need all the suggestions from you请帮助我的项目,我真的需要你的所有建议
Use .fetch()
to cache all members, and return them.使用
.fetch()
缓存所有成员,并返回它们。 Make sure you have GUILD_MEMBERS
intent:确保您有
GUILD_MEMBERS
意图:
list.members.fetch().then(m => {
let members = m.cache.map(u => u.user.username)
console.log(members) //array of all members
//you can also use "m.cache.each(u => console.log(u.user.username))" to log each one individually
})
Or use async-await
或者使用
async-await
const m = await list.members.fetch()
let members = m.cache.map(u => u.user.username)
console.log(members)
you can get all ids of users in that server with this (I'm not sure)您可以使用此服务器获取该服务器中所有用户的 ID(我不确定)
const Guild = client.guilds.cache.get("server id");
const Members = Guild.members.cache.map(member => member.id);
console.log(Members);
or this或这个
const list = <Guild>.members.cache.keys();
list.forEach(id => console.log(id));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.