Search code examples
javadiscorddiscord-jda

JDA Discord Add Reaction To Message


I have problem when I try to add reaction to message send by the bot. Here is my code and the problem I get when I try to add reaction.

The idea is simple. Just a poll command send by the bot + add reaction to it like thumbs up and thumbs down for example :

" $poll This Is Example Poll " and after this poll got created, add the reactions

CODE -

    @Override
    protected void execute(CommandEvent event) {
        if (this.check(event)) {
            return;
        }

        var args = event.getArgs().trim();
        if (args.isEmpty()) {
            this.incorrectUsage(event);
            return;
        }

        var announcementFormat = this.getConfig().getMessages().get("pool-format")
                .replaceAll("%message", args);


        event.reply(announcementFormat);
        event.getMessage().addReaction("✔").queue();
    }

ERROR -

ERROR net.dv8tion.jda.api.requests.RestAction - RestAction queue returned failure: [ErrorResponseException] 10008: Unknown Message
net.dv8tion.jda.api.exceptions.ContextException
        at net.dv8tion.jda.api.exceptions.ContextException.here(ContextException.java:54)
        at net.dv8tion.jda.api.requests.Request.<init>(Request.java:71)
        at net.dv8tion.jda.internal.requests.RestActionImpl.queue(RestActionImpl.java:197)
        at net.dv8tion.jda.api.requests.RestAction.queue(RestAction.java:563)
        at net.dv8tion.jda.api.requests.RestAction.queue(RestAction.java:529)
        at cf.lionsquad.lionminus.command.impl.PollCommand.execute(PollCommand.java:48)
        at com.jagrosh.jdautilities.command.Command.run(Command.java:323)
        at com.jagrosh.jdautilities.command.impl.CommandClientImpl.onMessageReceived(CommandClientImpl.java:557)
        at com.jagrosh.jdautilities.command.impl.CommandClientImpl.onEvent(CommandClientImpl.java:445)
        at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
        at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:82)
        at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:69)
        at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:150)
        at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:122)
        at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
        at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:948)
        at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:835)
        at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:813)
        at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:986)
        at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
        at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
        at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
        at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
        at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
        at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
        at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

Solution

  • Update: You have to wrap the emoji using Emoji.fromUnicode in JDA 5. So the new code is message.addReaction(Emoji.fromUnicode("✔")).


    You can use flatMap:

    event.getChannel().sendMessage(announcementFormat)
         .flatMap(message -> message.addReaction("✔"))
         .queue();
    

    Instead of event.reply(announcementFormat).

    Your code currently tries to add a reaction to the message sent by the user rather than the message sent by your bot. However, that message was already deleted at that point which is why you get an Unknown Message error.