线程 "main" java.lang.IllegalArgumentException 中的异常:侦听器必须实现 EventListener

Exception in thread "main" java.lang.IllegalArgumentException: Listener must implement EventListener

我尝试制作一个简单的机器人命令来检查机器人是否正常工作,但我遇到了以下错误

Exception in thread "main" java.lang.IllegalArgumentException: Listener must implement EventListener

这是我的机器人主要代码:

import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.events.message.GenericMessageEvent;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import org.jetbrains.annotations.NotNull;

import javax.security.auth.login.LoginException;

public class YoxMain {
    public static void main(String[] args) throws LoginException {
        JDA jda = JDABuilder.createDefault("token here")
                .setActivity(Activity.watching("the server"))
                .addEventListeners(new Commands())
                .build();

    }

}

测试命令代码:

import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class testcommand extends ListenerAdapter {
    public void onMessageReceived(net.dv8tion.jda.api.events.message.MessageReceivedEvent event) {
        if (event.getMessage().getContentRaw().equalsIgnoreCase("!test")) {
            event.getChannel().sendMessage("Test").queue();
        }
    }

}

我在网上搜索了答案,none真的帮了我。

您的问题是您添加了 new Commands() 作为您的事件侦听器。你的 class 叫做 testcommand 所以你必须做 addEventListeners(new testcommand())Commands是JDA提供的class,用于创建application commands