【问题标题】:How do I convert an id to a username? discord.js v12 [closed]如何将 id 转换为用户名? discord.js v12 [关闭]
【发布时间】:2020-10-14 00:36:29
【问题描述】:

我需要转换存储在数据库中的玩家 ID,并将其转换为他们的用户名以显示在排行榜上。我该怎么做呢?我在 discord.js v12 上。

我试过了:

fetchuser = message.guild.members.fetch(userid)
fetchuser = fetchuser.username

【问题讨论】:

  • 永远不要说“我尝试过的大多数方法都不起作用”。您很可能会得到那些不起作用的相同方法的答案,因为通常只有一两种方法可以做这样的事情。这既浪费了提问者的时间,也浪费了回答者的时间。相反,展示你尝试过的方法,至少其中一种,并询问为什么它不起作用。然后你会得到一个准确的答案(例如,这个已经过时了,改用这个 - 或者你写错了这部分,把这个移到这里 - 等等)

标签: javascript node.js discord discord.js


【解决方案1】:

您需要获取具有该 ID 的用户。然后,手动构建他们的标签

假设您的“id”存储在变量id 中,“client”是您的机器人。

const user = await client.users.fetch(id, { cache: true });
const userTag = `${user.username}#${user.discriminator}`;

【讨论】:

  • 这看起来很有希望,但返回 undefined。如果你有时间,你能帮忙吗?
  • 请发送您现在正在使用的代码。
  • (命令代码/获取用户的位置)
【解决方案2】:
const User = client.users.cache.get("UserID"); // Getting the user by ID.
if (User) { // Checking if the user exists.
    message.channel.send(User.tag) // The user exists.
} else {
    message.channel.send("User not found.") // The user doesn't exists or the bot couldn't find him.
};

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 2021-12-19
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2011-06-19
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多