由于某种原因,我的机器人会听取其他所有机器人的反应。
示例:如果我的服务器中有一个随机赠品机器人,该机器人使用反应将其加入,则出于某种奇怪的原因,我的机器人将监听此随机机器人的反应。
这是我的responseListener;
public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent e) {
if (e.getUser().isBot()) return;
e.retrieveMessage().queue(message -> {
if (message.getAuthor().getIdLong() != ProjectWar.getInstance().getDiscord().getJda().getSelfUser().getIdLong()) return;
if (!e.getMember().hasPermission(Permission.ADMINISTRATOR)) {
DiscordUtils.noPerm(e.getMember(), message);
e.getChannel().removeReactionById(e.getMessageId(), e.getReactionEmote().getName(), e.getUser()).queue();
return;
}
for (MessageEmbed embed : message.getEmbeds()) {
for (MessageEmbed.Field field : embed.getFields()) {
switch (field.getName()) {
case "Player":
this.reporter = field.getValue();
break;
case "FactionOne":
factionOne = field.getValue();
break;
case "FactionTwo":
this.factionTwo = field.getValue();
break;
case "ID":
this.ID = Integer.parseInt(field.getValue());
break;
case "Reason":
this.reason = field.getValue();
break;
case "State":
this.state = field.getValue();
break;
case "Date":
this.date = ProjectWar.getInstance().parse(field.getValue());
break;
}
}
}
WarReport report = new WarReport(reporter, factionOne, factionTwo, reason, (ID + 1), date, state);
MessageReaction reaction = message.getReactions().stream()
.filter(r -> r.getReactionEmote().getName().equals(e.getReactionEmote().getName()))
.findFirst()
.orElse(null);
if (reaction != null) {
if (reaction.getCount() > 2) {
e.getChannel().removeReactionById(e.getMessageId(), e.getReactionEmote().getName(), e.getUser()).queue();
}
}
//todo: convert to class with emotes.
if (e.getReactionEmote().getName().equals("\uD83D\uDDD1")) {
ReportManager.removeReport(report);
e.getChannel().deleteMessageById(e.getMessageId()).queue();
} else if (e.getReactionEmote().getName().equals("❌")) {
ProjectWar.getInstance().getDiscord().editStatus(e.getMessageId(), e.getChannel(), false);
ReportManager.setReportState(report, false);
} else if (e.getReactionEmote().getName().equals("✅")) {
ProjectWar.getInstance().getDiscord().editStatus(e.getMessageId(), e.getChannel(), true);
ReportManager.setReportState(report, true);
}
});
}
// no perm method
public static void noPerm(Member member, Message message) {
EmbedBuilder embedBuilder = new EmbedBuilder().setColor(Color.RED).setTitle("Missing permission!");
embedBuilder.addField("", "You're not allowed to do that <@" + member.getId() + ">!", false);
message.getChannel().sendMessage(embedBuilder.build()).queue(message1 -> message1.delete().queueAfter(5, TimeUnit.SECONDS));
}
我该怎么做,使我的机器人只监听我自己的机器人发送的消息?
答案 0 :(得分:1)
您将必须检查您的漫游器是否已发送某人做出回应的消息。 为此,您必须先获得某人做出反应的消息。
Message msg = e.getChannel().retrieveMessageById(e.getMessageId()).complete();
然后,您可以轻松检查该消息是否已由您的漫游器发送:
if (msg.getAuthor().equals(e.getJDA().getSelfUser())) {
// message that someone reacted to has been sent by your bot
}
答案 1 :(得分:0)
如果做出反应的用户不是当前正在运行的实例,则以下代码将返回。
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Example extends ListenerAdapter {
@Override
public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
if (event.getUser().getIdLong() != event.getJDA().getSelfUser().getIdLong()) return;
// ...
}
}