如何从用户的 ID 中获取用户

时间:2021-04-27 02:07:06

标签: java discord

我的 Discord 机器人中有一家商店,可以让您用硬币购买物品,此物品会导致机器人向我发送消息,但每次尝试向我发送消息时都会出错,另一个问题是如果不同的公会我的机器人找不到我,我需要一个替代方案。

最理想的方法:让用户使用他们的 ID 而不需要公会。

guild.getMemberById("348136128932610058").getUser().openPrivateChannel().queue(schannel -> schannel.sendMessage(user.getName() + " just bought a strange fragment (feature)").queue());                      
java.lang.NullPointerException
at checken.chen.App.onMessageReceived(App.java:152)
at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:428)
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:152)
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:954)
at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:841)
at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:819)
at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:992)
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)

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以只使用 openPrivateChannelById

guild.getJDA().openPrivateChannelById("348136128932610058")
  .flatMap(schannel -> schannel.sendMessage(user.getName() + " just bought a strange fragment (feature)"))
  .queue();

该频道包含带有 PrivateChannel#getUser 的用户。

或者,您可以以相同的方式使用 retrieveUserById 查找用户。