Search code examples
javadiscord-jda

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


I tried to make a simple bot command which checks if the bot is working, and I encountered with the following error

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

Here is my bot main code:

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

    }

}

Test command code:

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

}

I searched for answers in the internet, and none really helped me.


Solution

  • Your issue is that you added new Commands() as your event listener. Your class is called testcommand so you have to do addEventListeners(new testcommand()). Commands is a class provided by JDA and used for the creation of application commands.