![](/img/trans.png)
[英]A way to send Direct Message to all Server Members? Java Discord Api
[英]Sending Direct Message through Java Discord Api
我希望機器人直接發送消息,但為了上帝的愛,我找不到這樣做的確切方法/功能。所有可用的方法是將消息發送到同一服務器的頻道
event.getChannel().sendMessage("hello").queue();
我希望消息直接發送給用戶,或者特定用戶收到消息。 請幫忙!
您永遠不必猜測如何使用庫 - 這就是文檔的用途。 任何值得一提的庫都有文檔列出您需要擔心的每個類、方法和屬性。
快速谷歌搜索“discord-jda docs”將我們帶到javadoc: https ://ci.dv8tion.net/job/JDA/javadoc/index.html
您想向用戶發送消息,對嗎? 所以讓我們使用搜索欄並找到User
。 Types 下的第一個結果是net.dv8tion.jda.api.entities.User
。 我們現在在https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/User.html
如果你想知道如何對用戶做某事,我們看看每個用戶擁有的方法。 兩個立刻引起了我的注意: User.hasPrivateChannel()
和User.openPrivateChannel()
。 我們將單擊第二個,因為它看起來很相關。
瞧,文檔有示例用法! 我將在下面引用它:
// Send message without response handling public void sendMessage(User user, String content) { user.openPrivateChannel() .flatMap(channel -> channel.sendMessage(content)) .queue(); }
這看起來很簡單。 所以你正在尋找的基本用法(假設event
是MessageReceivedEvent
)是這樣的:
event.getAuthor().openPrivateChannel().flatMap(channel -> channel.sendMessage("hello")).queue();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.