我从User.fromID(long id)
那里获得了用户
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)
如何获取仅包装 ID 的用户的名称?
感谢您的帮助。
答案 0 :(得分:1)
您需要一个JDA实例,而使用retrieveUserById。
示例:
jda.retrieveUserById(userId)
.map(User::getName)
.queue(name -> {
// use name here
System.out.println("The user has the name " + name);
});