JDA – 机器人监听其他机器人的反应

huangapple go评论67阅读模式
英文:

JDA - Bot listens to other bots reactions

问题

public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent e) {
    if (e.getUser().isBot()) return; // Ignore reactions from other bots

    e.retrieveMessage().queue(message -> {
        if (message.getAuthor().getIdLong() != YOUR_BOT_ID) return; // Only listen to messages from your own bot

        // Rest of your code...
    });
}

Replace YOUR_BOT_ID with the actual ID of your bot user. This modification ensures that the reaction listener only responds to reactions on messages sent by your own bot.

英文:

for some reason my bot listens to all other bots reactions.

Example: If I had a random giveaway bot in my server which uses reactions to join it, my bot will listen to this random bots reactions for some odd reason.

Here is my reactionListener;

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));
    }

How would I make it so my bot only listens to messages sent from my own bot?

答案1

得分: 1

你需要检查某人对之前的消息是否进行了反应,以确定该消息是否由你的机器人发送。
为此,您首先需要获取某人进行反应的消息。

Message msg = e.getChannel().retrieveMessageById(e.getMessageId()).complete();

然后您可以轻松地检查该消息是否由您的机器人发送:

if (msg.getAuthor().equals(e.getJDA().getSelfUser())) {
    // 某人进行反应的消息由您的机器人发送
}
英文:

You will have to check if the message that someone reacted to has been sent by your bot.
To do that you will have to get the Message that someone reacted to first.

Message msg = e.getChannel().retrieveMessageById(e.getMessageId()).complete();

Then you can easily check if that message has been sent by your bot:

if (msg.getAuthor().equals(e.getJDA().getSelfUser())) {
    // message that someone reacted to has been sent by your bot
}

答案2

得分: 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;

        // ...
    }
}
英文:

The below code will return if the user that reacted is not the currently running instance.

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;

        // ...
    }
}

huangapple
  • 本文由 发表于 2020年9月9日 07:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63802742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定