获取用户ID的用户名 – Discord JDA

huangapple go评论79阅读模式
英文:

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 - 一个EventListener发生了未捕获的异常
java.lang.UnsupportedOperationException: 此 User 实例仅包装了一个 ID,不支持其他操作
	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)

我如何在User仅包装ID的情况下获取其名称?

感谢任何帮助。

英文:

I get a User from User.fromID(long id)

User u = User.fromID(123456);

I then want to get the name of that user. So I use User.getName()

String name = u.getName();

But then I get an error.

[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.&lt;init&gt;(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)

How can I get the name of a User that only wraps an ID?

Any help is appreciated.

答案1

得分: 4

你需要一个JDA实例,并使用retrieveUserById替代。

示例:

jda.retrieveUserById(userId)
   .map(User::getName)
   .queue(name -> {
   // 在这里使用 name
       System.out.println("该用户的名字为 " + name);
   });
英文:

You need a JDA instance and use retrieveUserById instead.

Example:

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

huangapple
  • 本文由 发表于 2020年10月23日 11:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64493339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定