simplykumo
simplykumo

Reputation: 1

JDA add reaction when reacted with specific reaction

I want to create a Discord Bot with JDA in Java which adds āœ” and āŒ when a user reacts with šŸ“ to a message and afterwards deletes the šŸ“ reaction.

package me.beemo.commands;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;


public class makesurvey extends ListenerAdapter {

    @Override
    public void onMessageReactionAdd(MessageReactionAddEvent event) {
        boolean check;

        if (event.getReaction().equals("šŸ“")) {
            //when message has šŸ“ then continue
            check = true;
        } else {
            //when message has not šŸ“ then abort
            check = false;
        }

        if (event.getReaction().equals("āœ…")){
            //when message has āœ… then abort
            check = false;
        } else {
            //when message has not āœ… then continue
            check = true;
        }

        if (event.getReaction().equals("āŒ")){
            //when message has āŒ then abort
            check = false;
        } else {
            //when message has not āŒ then continue
            check = true;
        }

        if (check == true) {
            Message message = event.getChannel().getHistory().getMessageById(event.getMessageId());
                message.removeReaction(Emoji.fromUnicode("U+274C"));
                message.addReaction(Emoji.fromUnicode("U+274C")).queue();
                message.addReaction(Emoji.fromUnicode("U+2705")).queue();
                message.removeReaction(Emoji.fromUnicode("U+1F4DD")).queue();
        } else {
            return;
        }
    }
}

But every time I add the šŸ“ reaction I get this error

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException: Cannot invoke "net.dv8tion.jda.api.entities.Message.removeReaction(net.dv8tion.jda.api.entities.emoji.Emoji)" because "message" is null
    at me.beemo.commands.makesurvey.onMessageReactionAdd(makesurvey.java:41)

I don't know why exactly the message is still null after getting the ID of the message

Upvotes: 0

Views: 502

Answers (1)

Minn
Minn

Reputation: 6131

You can use MessageChannel#addReactionById

event.getChannel().addReactionById(event.getMessageId(), emoji).queue();

Also, it looks like your use of the check variable is incorrect, it will only be false if the reaction is āŒ. Since you have an else that makes it true regardless of what other reaction it is, meaning your other 2 if/else checks are doing nothing at all.

The getReaction() also does not return a string, so your equality check can only be false regardless.

Upvotes: 0

Related Questions