简体   繁体   English

如何解决在discord.js中从集合中获取用户的问题?

[英]How to fix user fetching from a collection in discord.js?

I'm trying to retrieve usernames from IDs in collection (Discord.js) 我正在尝试从集合(Discord.js)中的ID中检索用户名

I've tried fetching user with client.fetchUser(config.userID) 我尝试使用client.fetchUser(config.userID)获取用户

const adm = client.fetchUser(config.admins)
const embed = new Discord.RichEmbed()
.setTitle("Developers")
.setDescription("Usernames: \n"+adm.username)
message.channel.send(embed);

It outputs "userID1,userID2 is not a snowflake" 它输出“ userID1,userID2不是雪花”

The docs say that the parameter should be a snowflake Snowflake Ref 文档说该参数应该是雪花Snowflake Ref

From what I can see, you are passing in an array of snowflakes which won't work. 从我所看到的,您正在传递一系列雪花,这些雪花将不起作用。

The method returns a Promise of type User, I think you are trying to get all the users in one go. 该方法返回一个类型为User的Promise,我想您正在尝试一次性获取所有用户。

I have created a code snip which will fetch the users one by one from your snowflake array of Admins and then create a string for you to put in your message. 我创建了一个代码片段,它将从您的Admins雪花数组中逐个获取用户,然后创建一个字符串以供您放入消息中。

Remember to use this in an ASYNC method else the await will throw an error. 请记住在ASYNC方法中使用此方法,否则await将引发错误。

Edit : As mentioned in the comments, you need to initialise the adminUsers before you can push to it. 编辑:如注释中所述,您需要先初始化adminUsers,然后才能推送到它。

let adminUsers = []; // array of Users

for (let i = 0; i < config.admins.length; i++) {
  let currentUser = config.admins[i];
  let user = await client.fetchUser(currentUser);
  adminUsers.push(user);
}

let description = "Usernames: \n";

adminUsers.forEach(user => {
  description += `${user.username}\n`;
});

const embed = new Discord.RichEmbed()
  .setTitle("Developers")
  .setDescription(description)
message.channel.send(embed);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM