Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

java - onMessageReceived() not being called | Discord Bot

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
question from:https://stackoverflow.com/questions/65835461/onmessagereceived-not-being-called-discord-bot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...