等待使用discordJDA的消息无法正常工作

时间:2018-12-27 09:02:14

标签: java discord

我目前正在使用Discord机器人。我遇到的一个问题是,我无法找出发送邮件后如何让机器人等待用户回复的方法。

我还尝试过阅读有关在此处使用RestAction的git文档:https://github.com/DV8FromTheWorld/JDA/wiki/7)-Using-RestAction,但似乎没有提及实现类似于discord.js的“ await”功能

我尝试通过编码来模仿这种效果:

public class EventHandler extends ListenerAdapter {

        private static final String PREFIX = "&";
        public static String[] args;


        public void sendMessage(String s, GuildMessageReceivedEvent event) {
            event
                    .getChannel()
                    .sendMessage(s)
                    .queue();
        }

        public void onGuildMessageReceived (GuildMessageReceivedEvent event) {

            args = event
                    .getMessage()
                    .getContentRaw()
                    .split(" "); 

        if (args[0].equalsIgnoreCase(PREFIX + "any_command")) {
            sendMessage("Type hello!");
            if (args[0].equalsIgnoreCase(PREFIX + "hello") {
               sendMessage("hello there!");
            }
        }
    }
}

主类:

import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;


public class Main {

    public static void main(String[] args) throws Exception {
        JDA jda = new JDABuilder(AccountType.BOT)
                .setToken("token goes here")
                .setAutoReconnect(true).build();

        try {
            jda.addEventListener(new EventHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
} 

这不会注册在给出提示后键入的hello命令。我最好的猜测是该条件永远不会满足,因为原始条件会覆盖即将出现的条件(args [0]已经是any_command) 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我建议使用JDA-Utilities(https://github.com/JDA-Applications/JDA-Utilities/)中的EventWaiter

快速查看源代码,看起来您需要这样的东西

EventWaiter waiter = new EventWaiter();
// SO wouldn't let me insert new lines for some reason.
waiter.waitForEvent(GuildMessageReceivedEvent.class, (event) -> event.getMessage().getContentRaw().equalsIgnoreCase("hello"), (event) -> event.getChannel().sendMessage("hello!").queue()));