如何从Discord.js中的用户名中找到用户ID? [英] How to find a User ID from a Username in Discord.js?
问题描述
我有一个带有Discord用户名(即JohnDoe#1234)的json文件(localJSON.json),需要从这些用户名中获取用户ID才能添加角色.我在网上查看过的每个地方的rMember值都为'undefined'或'null'.验证了添加角色的代码在以字符串形式提供用户ID时可以使用,但是找不到从用户名获取用户ID的方法.如何使用Discord.js从用户名中获取用户的ID?
I have a json file (localJSON.json) with Discord usernames (i.e. JohnDoe#1234) and need to get the User IDs from these usernames in order to have a role added. Every place I have looked online has resulted with either an 'undefined' or 'null' value for rMember. Verified that the code to add a role works when given a User ID as a string, but can't find how to get a User ID from a username. How do I get a user's ID from their Username using Discord.js?
localJSON.json
[
{
"discordName": "JohnDoe#1234"
},
{
"discordName": "MarySue#5678"
}
]
function addRole(discordUsername, gameName, message){
var roleName = "";
//Switch statement to assign roleName to a valid guild role based on argument
var userID = discordUsername.id; //Pseudo code, Need to accomplish this
var rMember = message.guild.members.get(userID); //Needs UserID as string
var gRole = message.guild.roles.find((role) => role.name == roleName);
if (!rMember) { //if member not in server
message.channel.send(rMember + " is not in the server!");
} else { //assign role
rMember.addRole(gRole);
}
}
async run(message, args){
...
for (var i = 0; i < localJSON.length; i++) {
var currentEntry = localJSON[i];
var currrentUserName = currentEntry.discordName;
addRole(currrentUserName, args, message); //addRole(discordUsername, gameName, message);
}
}
推荐答案
您要这样做
client.users.cache.find(u => u.tag === 'Someone#1234').id
Discord.js v12现在使用 .cache
,因此您必须在缓存上运行find,并且v12还删除了 Collection#find(key,value)
Collection#find(data =&>; data.key === value)
的
Discord.js v12 uses .cache
now, so you have to run find on the cache, and v12 also removes Collection#find(key, value)
in favor of Collection#find(data => data.key === value)
.
这篇关于如何从Discord.js中的用户名中找到用户ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!