EventWaiter - Java Discord API

Posted

技术标签:

【中文标题】EventWaiter - Java Discord API【英文标题】: 【发布时间】:2018-03-04 21:13:52 【问题描述】:

这是一个奇怪的具体问题,但我正在尝试让 JDA Utilities 中的 EventWaiter 工作。 (https://github.com/jagrosh/ExampleBot/tree/master/src/main/java/com/jagrosh/examplebot)

我设置了一个方法并且超时部分有效,但服务员似乎没有像预期的那样接收任何发送到 discord 的新消息,我不知道为什么。这是我所拥有的:

public static void askQuestions(int numLeft, MessageReceivedEvent event, String level) 
            if(numLeft==0) 
                Writer.listWords(event);
                Writer.clearWords();
                return;
            
            //Get random line from text file and store in text.
            text = Reader.fileReader(level);
            //Get the question 
            word = getQuestion(text);
            //Get the answer
            ans = getAnswer(text, event);
            //Write word to txt file to print out later at end of game. 
            Writer.playedWordsWrite(word, ans);
            //Create image of word and send to discord
            imageCreator(word, event);

            //Set up waiter to get players' answers. Mention user who gets it right and then call the method again.
            waiter.waitForEvent(MessageReceivedEvent.class, 
                //Make sure it's the right answer in the same channel
                e -> e.getChannel().equals(event.getChannel()) && e.getMessage().getRawContent().equals(ans),
                //Respond, inserting the name they listed into the response
                e -> 
                    e.getChannel().sendMessage(e.getAuthor().getAsMention()+ " got it right!").queue();
                    askQuestions(numLeft-1, e, level);
                ,

                //If the user takes more than 10 seconds, time out
                7, TimeUnit.SECONDS, () -> 
                    event.getTextChannel().sendMessage("Correct answer: "+ans).queue();
                    askQuestions(numLeft-1, event, level);
                
            );
        

【问题讨论】:

【参考方案1】:

最好的做法是不要存储 JDA 对象,例如通道,因为将它们的 ID 比较为 long 会更好。这可能就是问题所在。尝试e.getChannel().getIdLong() == event.getChannel().getIdLong() 否则,我猜它是您的方法之一,例如imageCreatorplayedWordsWrite

【讨论】:

以上是关于EventWaiter - Java Discord API的主要内容,如果未能解决你的问题,请参考以下文章

试图理解 Java 中的时间戳(Discord JDA 和 Mongodb)

用户反应后删除消息

lastmessageID 始终为空,除了我

Discord bot没有回复消息[重复]

获取活动语音连接的数量

createReactionCollector 在 discord.js 中不起作用