从 ID 获取用户名 - Discord JDA

Get Name of User From ID - Discord JDA

我从 User.fromID(long id)

得到一个 User

User u = User.fromID(123456);

然后我想得到那个用户的名字。所以我使用 User.getName()

String name = u.getName();

但是我得到一个错误。

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.UnsupportedOperationException: This User instance only wraps an ID. Other operations are unsupported
    at net.dv8tion.jda.internal.entities.UserById.unsupported(UserById.java:78)
    at net.dv8tion.jda.internal.entities.UserById.getName(UserById.java:85)
    at bot.Profile.<init>(Profile.java:19)
    at files.Profiles.handleUser(Profiles.java:17)
    at bot.Chat.onMessageReceived(Chat.java:19)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:430)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:82)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:69)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:150)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:122)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:948)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:835)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:813)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:986)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

如何获取仅包含 IDUser 的名称?

感谢任何帮助。

您需要 JDA instance and use retrieveUserById

示例:

jda.retrieveUserById(userId)
   .map(User::getName)
   .queue(name -> {
   // use name here
       System.out.println("The user has the name " + name);
   });