首页 > 解决方案 > 如何修复从 discord.js 中的集合中获取用户的问题?

问题描述

我正在尝试从集合中的 ID 中检索用户名(Discord.js)

我尝试使用 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);

它输出“userID1,userID2 不是雪花”

标签: javascriptnode.jscollectionsdiscord.js

解决方案


文档说参数应该是雪花Snowflake Ref

从我所见,您传递了一系列不起作用的雪花。

该方法返回 User 类型的 Promise,我认为您正在尝试一次性获取所有用户。

我创建了一个代码片段,它将从您的管理员雪花数组中一个一个地获取用户,然后创建一个字符串供您输入您的消息。

请记住在 ASYNC 方法中使用它,否则await会引发错误。

编辑:如评论中所述,您需要先初始化 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);

推荐阅读