如何在 JDA 中将 DM 发送给有响应的人
How to send a DM to someone WITH response in JDA
我正在我的 Discord Bot 中编写一个反馈功能,当有人离开时,应该向他们发送 DM 消息询问他们离开的原因。
event.getUser().openPrivateChannel()
.flatMap(channel -> channel.sendMessage("Hello, we are sorry you're leaving "+event.getGuild().getName()+", if you don't mind, please tell us why you left or leave any other feedback here, it'll help us improve the server and improve experience for you if you re-join again in the future.\n\nThank you ❤."))
.queue();
上面的代码是负责发送的,我尝试在私有频道中创建状态机但是没有成功:
import bot.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Feedback extends ListenerAdapter {
private final long channelId, authorId;
public Feedback(MessageChannel channel, User author) {
this.channelId = channel.getIdLong();
this.authorId = author.getIdLong();
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.getAuthor().isBot()) return;
if (event.getAuthor().getIdLong() != authorId) return;
if (event.getChannel().getIdLong() != channelId) return;
MessageChannel channel = event.getChannel();
String content = event.getMessage().getContentRaw();
event.getChannel().sendMessage("Thanks for your feedback!").queue();
EmbedBuilder feedback = new EmbedBuilder();
feedback.setTitle("Automated System Operations - Leaving Feedback");
feedback.addField("Feedback", content, false);
feedback.setColor(0xC90004);
feedback.setAuthor(event.getAuthor().getAsTag()+" - "+event.getAuthor().getId(), "https://support.discord.com/hc/en-us/articles/209572128-How-do-I-log-out-", event.getAuthor().getAvatarUrl());
feedback.setImage("https://media.discordapp.net/attachments/894913784823566428/896323821336948736/unknown.png?width=384&height=192");
Main.jda.getGuildById("894913620868202506").getTextChannelById("896322509874540545").sendMessage(feedback.build()).queue();
}
}
我有这个事件状态机通道,但我不知道如何在 DM 中addListener
。
接受任何帮助<3
您永远不必猜测如何使用库 - 这就是文档的用途。任何称职的图书馆都有文档列出每个 class、方法和您需要担心的 属性。
快速 google 搜索“discord-jda 文档”将我们带到 JavaDoc:https://ci.dv8tion.net/job/JDA/javadoc/index.html
您想向用户发送消息,对吗?因此,让我们使用搜索栏查找 User。类型下的第一个结果是 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();
您可以使用 JDA#addEventListener 添加状态机事件侦听器:
event.getUser().openPrivateChannel().flatMap(channel -> {
event.getJDA().addEventListener(new Feedback(channel, event.getUser()));
return channel.sendMessage("hello");
}).queue();
我建议您在收到 event.getJDA().removeEventListener(this);
的响应后删除您的事件侦听器
我正在我的 Discord Bot 中编写一个反馈功能,当有人离开时,应该向他们发送 DM 消息询问他们离开的原因。
event.getUser().openPrivateChannel()
.flatMap(channel -> channel.sendMessage("Hello, we are sorry you're leaving "+event.getGuild().getName()+", if you don't mind, please tell us why you left or leave any other feedback here, it'll help us improve the server and improve experience for you if you re-join again in the future.\n\nThank you ❤."))
.queue();
上面的代码是负责发送的,我尝试在私有频道中创建状态机但是没有成功:
import bot.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Feedback extends ListenerAdapter {
private final long channelId, authorId;
public Feedback(MessageChannel channel, User author) {
this.channelId = channel.getIdLong();
this.authorId = author.getIdLong();
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.getAuthor().isBot()) return;
if (event.getAuthor().getIdLong() != authorId) return;
if (event.getChannel().getIdLong() != channelId) return;
MessageChannel channel = event.getChannel();
String content = event.getMessage().getContentRaw();
event.getChannel().sendMessage("Thanks for your feedback!").queue();
EmbedBuilder feedback = new EmbedBuilder();
feedback.setTitle("Automated System Operations - Leaving Feedback");
feedback.addField("Feedback", content, false);
feedback.setColor(0xC90004);
feedback.setAuthor(event.getAuthor().getAsTag()+" - "+event.getAuthor().getId(), "https://support.discord.com/hc/en-us/articles/209572128-How-do-I-log-out-", event.getAuthor().getAvatarUrl());
feedback.setImage("https://media.discordapp.net/attachments/894913784823566428/896323821336948736/unknown.png?width=384&height=192");
Main.jda.getGuildById("894913620868202506").getTextChannelById("896322509874540545").sendMessage(feedback.build()).queue();
}
}
我有这个事件状态机通道,但我不知道如何在 DM 中addListener
。
接受任何帮助<3
您永远不必猜测如何使用库 - 这就是文档的用途。任何称职的图书馆都有文档列出每个 class、方法和您需要担心的 属性。
快速 google 搜索“discord-jda 文档”将我们带到 JavaDoc:https://ci.dv8tion.net/job/JDA/javadoc/index.html
您想向用户发送消息,对吗?因此,让我们使用搜索栏查找 User。类型下的第一个结果是 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();
您可以使用 JDA#addEventListener 添加状态机事件侦听器:
event.getUser().openPrivateChannel().flatMap(channel -> {
event.getJDA().addEventListener(new Feedback(channel, event.getUser()));
return channel.sendMessage("hello");
}).queue();
我建议您在收到 event.getJDA().removeEventListener(this);