JDA 机器人未收听消息

JDA bot is not listening to messages

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

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”附近。我尝试了很多方法,例如重新排列我的代码或重写它,但似乎没有任何效果。

您没有在 sendMessage 返回的 MessageAction 上调用 queue()

Nothing happens when using X

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

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

来自 JDA Troubleshooting Wiki

您也从未注册过您的事件侦听器。并且您正在为 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
    }
}

而且你应该永远不要在任何地方泄露你的机器人令牌