JDA Discord 添加对消息的反应

JDA Discord Add Reaction To Message

我在尝试对机器人发送的消息添加反应时遇到问题。这是我的代码和我在尝试添加反应时遇到的问题。

这个想法很简单。只是机器人发送的民意调查命令 + 对其添加反应,例如竖起大拇指和竖起大拇指:

“$poll This Is Example Poll”,创建此投票后,添加反应

代码 -

    @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 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)

您可以使用 flatMap:

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

而不是event.reply(announcementFormat)

您的代码当前尝试向用户发送的消息添加反应,而不是您的机器人发送的消息。但是,该消息当时已被删除,这就是您收到 Unknown Message 错误的原因。