[英]Sending Direct Message through Java Discord Api
I want the bot to send a direct message but for the love of god,I cannot find the exact methods/functions to do so.All that is available is to send a message to the same server's channel我希望机器人直接发送消息,但为了上帝的爱,我找不到这样做的确切方法/功能。所有可用的方法是将消息发送到同一服务器的频道
event.getChannel().sendMessage("hello").queue();
I want the message to sent directly to the user or rather that a particular user gets the message.我希望消息直接发送给用户,或者特定用户收到消息。 please help!请帮忙!
You never have to guess at how to use a library - that's what documentation is for.您永远不必猜测如何使用库 - 这就是文档的用途。 Any library worth its salt has documentation listing every single class, method, and property you need to worry about.任何值得一提的库都有文档列出您需要担心的每个类、方法和属性。
A quick google search for "discord-jda docs" takes us to the javadoc: https://ci.dv8tion.net/job/JDA/javadoc/index.html快速谷歌搜索“discord-jda docs”将我们带到javadoc: https ://ci.dv8tion.net/job/JDA/javadoc/index.html
You want to send a message to a user, right?您想向用户发送消息,对吗? So let's use the search bar and find User
.所以让我们使用搜索栏并找到User
。 First result under Types is net.dv8tion.jda.api.entities.User
. Types 下的第一个结果是net.dv8tion.jda.api.entities.User
。 We're now at https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/User.html我们现在在https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/User.html
If you want to know how to do something with a user , we look at the Methods every User has.如果你想知道如何对用户做某事,我们看看每个用户拥有的方法。 Two catch my eye right away: User.hasPrivateChannel()
and User.openPrivateChannel()
.两个立刻引起了我的注意: User.hasPrivateChannel()
和User.openPrivateChannel()
。 We'll click the second one since it looks relevant.我们将单击第二个,因为它看起来很相关。
Lo and behold, the docs have example usage!瞧,文档有示例用法! I'll quote it below:我将在下面引用它:
// Send message without response handling public void sendMessage(User user, String content) { user.openPrivateChannel() .flatMap(channel -> channel.sendMessage(content)) .queue(); }
This seems pretty straightforward.这看起来很简单。 So the basic usage you're looking for (assuming event
is a MessageReceivedEvent
) is this:所以你正在寻找的基本用法(假设event
是MessageReceivedEvent
)是这样的:
event.getAuthor().openPrivateChannel().flatMap(channel -> channel.sendMessage("hello")).queue();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.