Reputation: 1
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
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