等待使用discordJDA的消息未按预期工作 [英] Awaiting for message using discordJDA not working as intended

查看:34
本文介绍了等待使用discordJDA的消息未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working on my discord bot. One problem I encountered is that I'm not able to find out how to allow the bot to wait for a user reply after a message is sent.

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

I also have tried reading the git documentation regarding using RestAction over here: https://github.com/DV8FromTheWorld/JDA/wiki/7)-Using-RestAction but it seems it does not mention anything about implementing an "await" function similar to discord.js

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

I tried coding to mimic such an effect:

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)任何帮助将不胜感激!

This doesn't register the hello command typed after the prompt given. My best guess would be that the condition is never met since the original condition overrides the upcoming one (args[0] is already any_command) Any help would be appreciated!

推荐答案

我建议使用JDA-Utilities(

I'd suggest the EventWaiter from JDA-Utilities (https://github.com/JDA-Applications/JDA-Utilities/)

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

Taking a quick look at the source, looks like you'll need something like this

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()));

这篇关于等待使用discordJDA的消息未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆