JDA机器人未收听消息 [英] JDA bot is not listening to messages

查看:46
本文介绍了JDA机器人未收听消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个非常简单的discord机器人,这是我第一次使用Java(使用IntelliJ IDE)制作机器人.它已登录并可以正常联机,但是不会收到我在公会中发送的任何消息.代码如下:

I'm trying to make a very simple discord bot and it is my first time making one in java (with the IntelliJ IDE). It logs in and goes online correctly, but won't receive any messages that I send in the guild. The code is below:

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws Exception{
        JDABuilder bot = new JDABuilder(AccountType.BOT);
        String token = "token";
        bot.setToken(token);
        bot.build();
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        System.out.println("message received");
        event.getChannel().sendMessage("reeeeeeee");
        super.onMessageReceived(event);
    }
}

我认为有缺陷的部分在"public void onMessageReceived"周围的某处.我已经尝试了很多事情,例如重新排列代码或重写代码,但是似乎没有任何效果.

I think that the flawed part is somewhere around "public void onMessageReceived". I have tried many things such as rearranging my code or rewriting it, but nothing seems to work.

推荐答案

您没有在 sendMessage返回的 MessageAction 上调用 queue()代码>.

You didn't call queue() on the MessageAction returned by sendMessage.

使用X时没有任何反应

在JDA中,我们通过使用常见的RestAction类来使用异步速率限制处理.当您具有诸如channel.sendMessage("hello"))之类的代码时;或message.delete();实际上没有任何反应.这是因为sendMessage(...)和delete()都返回RestAction实例.由于该类只是执行请求的中间步骤,因此您在这里没有完成.在这里,您可以决定使用异步queue()(推荐)或Submit()或阻止complete()(不推荐).

In JDA we make use of async rate-limit handling through the use of the common RestAction class. When you have code such as channel.sendMessage("hello"); or message.delete(); nothing actually happens. This is because both sendMessage(...) as well as delete() return a RestAction instance. You are not done here since that class is only an intermediate step to executing your request. Here you can decide to use async queue() (recommended) or submit() or the blocking complete() (not recommended).

您可能会注意到queue()返回void.这是因为它是异步的,而是使用回调.了解更多

You might notice that queue() returns void. This is because it's async and uses callbacks instead. Read More

从JDA疑难解答Wiki

您也从未注册过事件监听器.而且您正在使用JDABuilder弃用的构造函数.

You also never registered your event listener. And you're using the deprecated constructor for JDABuilder.

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws Exception{
        JDABuilder.createDefault(token) // don't use the deprecated constructor
                  .addEventListeners(new Main()) // register your listener
                  .build();
    }

    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        System.out.println("message received");
        event.getChannel().sendMessage("reeeeeeee").queue(); // call queue
    }
}

并且您应该永远不要在任何地方泄漏您的机器人令牌

这篇关于JDA机器人未收听消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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