TransWikia.com

onMessageReceived() not being called | Discord Bot

Stack Overflow Asked by L0raxeo C on February 7, 2021

I am making my first Discord bot with Java. I am using Gradle. Here is my code so far:

public class Main extends ListenerAdapter {
    public static void main(String[] args) throws LoginException {
        JDABuilder builder = new JDABuilder(AccountType.BOT);
        String token = "x";
        builder.setToken(token);
        builder.addEventListener(new Main());
        builder.buildAsync();
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        System.out.println("We received a message from " +
                event.getAuthor().getName() + ": " +
                event.getMessage().getContentDisplay()
        );

        if (event.getMessage().getContentRaw().equals("yomyman")) {
            event.getChannel().sendMessage("Pong!").queue();
        }
    }

}

When I execute the program, I do not receive any warnings and my bot goes online in my discord server. However for some reason when I type "yomyman" in any channel, nothing happens. Nothing gets printed to the console, nor does the bot say anything on the. On top of that the method onMessageReceived(); doesn’t get called at all. This is the only code I have in this project.

Additional information:

  • Gradle is being used
  • JDA is being used
  • No errors printed to the console

One Answer

You are facing an issue described in a discord developer support article:

WHY IS MY BOT NO LONGER WORKING SUDDENLY?

When the new gateway update was deployed, bots which weren't adequately prepared to specify gateway intents could have experienced a variety of issues. Common symptoms include:

  • The bot not responding anymore, despite being online
  • The bot cache being empty (only seeing a few users across all guilds)
  • A library on ready event not firing or timing out (happening mostly on discord.py and discord.js bots but other libraries might exhibit the same behavior)

If you are affected by this, this is because of 2 gateway changes:

  • You no longer have access to disabled privileged intents if you are not specifying intents
  • You can no longer request member info for multiple guilds at the same time

HOW DO I GET BACK UP AND RUNNING? UPDATING YOUR LIBRARY

First, you must make sure you're using a library version that can handle intents, and by extension no longer requests information on multiple guilds at once.

For discord.py, this means you need to update to v1.5 or higher.

For discord.js, this means you need to update to v12 or higher.

If you're unsure how to upgrade your bot to a new library release or are unclear regarding the intent compatibility of an alternative library, please see the library links provided in our Developer Community Resources.

SPECIFYING GATEWAY INTENTS

Please note that a variety of our gateway intents are not privileged, meaning they do not require specific switches to be flipped or whitelisted access to be requested. You can review the entirety of our available intents here.

Specifying which intents you'd like to receive varies based on your chosen library. Documentation on how to specify gateway intents in discord.js is available here. Documentation for specifying gateway intents in discord.py is available here.

So, you need to update JDA at first. I would recommend you to use the latest version for this. You can see the latest version code here:

After that, you should change the use of the constructor of JDABuilder as defined in the README of JDA like this:

JDABuilder builder=JDABuilder.createDefault("YOUR_TOKEN_HERE");

If you need privileged gateway intents, you also need to enable those both on the developer page and in your application.

Answered by dan1st on February 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP