Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getMessageById returns null

This code supposed to add reactions on a specific message using ID it got from slash-command. It gets the channel’s and message’s IDs perfectly but when it goes to searching for the message to add reactions the message is null. I believe the problem is in messageReactionAdd() method. The method gets all it needs to add reactions.

package org.rathercruel.bot.commands.roles;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.util.ArrayList;
import java.util.List;

public class SetRoleMessage extends ListenerAdapter {

    @Override
    public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
        if (event.getName().equalsIgnoreCase("set-message-reactions")) {
            String channelID = event.getChannel().getId();
            String messageID = event.getOption("message-id").getAsString();

            Guild guild = event.getGuild();

            List<Emoji> newEmojiList = new ArrayList<>();

            newEmojiList.add(Emoji.fromUnicode("U+1F5FD")); // Statue of Liberty [0]
            newEmojiList.add(Emoji.fromUnicode("U+26A1")); // Voltage
            newEmojiList.add(Emoji.fromUnicode("U+1F6B0")); // Glass of water
            newEmojiList.add(Emoji.fromUnicode("U+1F4BB")); // computer
            newEmojiList.add(Emoji.fromUnicode("U+1F413")); // Rooster
            newEmojiList.add(Emoji.fromUnicode("U+1F414")); // Chicken
            newEmojiList.add(Emoji.fromUnicode("U+1F480")); // Skull [6]

            // call the problem method
            messageReactionAdd(event, channelID, messageID, newEmojiList);
        }
    }

    private void messageReactionAdd(SlashCommandInteractionEvent event, String channelID, String messageID, List<Emoji> emojiList) {
        Guild guild = event.getGuild();
        TextChannel channel = guild.getTextChannelById(channelID);
        Message message = channel.getHistory().getMessageById(messageID);

        if (message != null) {
            for (int i = 0; i < emojiList.size(); i++) {
                message.addReaction(emojiList.get(i)).queue();
            }
        // Returns this code:
        } else {
            event.reply("Unknown message.").setEphemeral(true).queue();
        }
    }
}

I’ve tried to use different variables such as String and long but result is the same in both cases. Also I’ve tried to find the message inside the onSlashCommandInteraction() method.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This line will always return null, for every message id:

channel.getHistory().getMessageById(messageID)

As per the documentation:

From getHistory

Creates a new MessageHistory object for each call of this method

From MessageHistory

If a Message with the provided id has not already been retrieved (thus, doesn’t exist in this MessageHistory object), then this method returns null.

Since you haven’t called any of the retrieve methods, such as retrievePast, the retrieved message cache is empty.

You don’t want to retrieve the message anyway, since you can add reactions without the message object.

channel.addReactionById(messageId, emoji).queue();

See addReactionById.

If you do want to retrieve a message, use retrieveMessageById instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading