Search code examples
javadiscord-jda

JDA how to get a message by ID


Is there a way to get a message by only having his ID and his TextChannel ID ?

I found this :

Message message = TextChannel.getHistory().getMessageById(String id);

But it just throw an error : net.dv8tion.jda.api.exceptions.ErrorResponseException: 10008: Unknown Message


Solution

  • You can use MessageChannel#retrieveMessageById(id):

    channel.retrieveMessageById(id).queue((message) -> {
        // use the message here, its an async callback
        message.addReaction(reaction).queue();
        message.editMessage("bleh").queue();
        System.out.println("Message Content: " + message.getContentDisplay());
    }, new ErrorHandler().handle(ErrorResponse.UNKNOWN_MESSAGE, (e) -> {
        // this means the message doesn't exist
        channel.sendMessage("That message doesn't exist!").queue();
    }));
    

    Also worth looking at: