JDA Discord Bot Event Handler Duplicating Response Messages

Multiple Message Problem with JDA Bot

I’m having trouble with my Discord bot built using JDA. Every time I restart my application, the bot sends one additional duplicate message. So first run sends 1 message, second run sends 2 copies, third run sends 3 copies, and so on.

Main Bot Class

package com.mybot.discord;

import javax.security.auth.login.LoginException;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;

public class BotApplication {
    public static JDA botInstance;
    public static String commandPrefix = "!";
    
    public static void main(String[] args) throws LoginException {
        botInstance = JDABuilder.createDefault("bot_token_here").build();
        botInstance.getPresence().setStatus(OnlineStatus.ONLINE);
        Activity watching = Activity.watching("Naruto");
        botInstance.getPresence().setActivity(watching);
        
        MessageHandler handler = new MessageHandler();
        botInstance.addEventListener(handler);
    }
}

Event Handler Class

package com.mybot.discord;

import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class MessageHandler extends ListenerAdapter {
    public void onGuildMessageReceived(GuildMessageReceivedEvent msg) {
        String[] parts = msg.getMessage().getContentRaw().split("\\s+");
        
        if(parts[0].startsWith(BotApplication.commandPrefix)) {
            parts[0] = parts[0].substring(1);
            switch (parts[0]) {
            case "about":
                msg.getChannel().sendTyping().queue();
                msg.getChannel().sendMessage("Hello! I'm a helpful Discord bot").queue();
                break;
            case "welcome":
                msg.getChannel().sendTyping().queue();
                msg.getChannel().sendMessage(msg.getAuthor().getName()+" says hello to "+parts[1]).queue();
                break;
            default:
                msg.getChannel().sendTyping().queue();
                msg.getChannel().sendMessage("Sorry, I don't recognize that command yet").queue();
                break;
            }
        }
    }
}

The Issue

When I type !about the bot responds like:

  • “Hello! I’m a helpful Discord bot”
  • “Hello! I’m a helpful Discord bot”
  • “Hello! I’m a helpful Discord bot”
  • “Hello! I’m a helpful Discord bot”
  • “Hello! I’m a helpful Discord bot”

I think each time I restart the program it adds another event listener somehow. How can I fix this duplicate message problem?

You’re probably creating multiple JDA sessions without killing the old ones. Discord keeps the previous session alive when you restart your app, so event listeners stack up. I ran into this exact issue with my bot and fixed it with proper session management. Add botInstance.shutdownNow() to your main method so it actually terminates when your app closes. But here’s the real culprit - check if you’re running multiple bot instances at once. Happens all the time during development when you think you stopped the previous instance but it’s still running. Use your IDE’s task manager or check system processes to make sure only one is active. Pro tip: add a unique session ID to your bot’s status so you can easily spot if multiple instances are running.

This happens when your bot doesn’t shut down properly and stays connected to Discord’s gateway. Every restart creates a new connection while the old ones keep running, so you get multiple event handlers responding to the same message. Fix it by adding proper shutdown handling to your bot. Put this shutdown hook in your main method after creating the JDA instance: java Runtime.getRuntime().addShutdownHook(new Thread(() -> { if (botInstance != null) { botInstance.shutdown(); } })); Also try calling botInstance.awaitReady() after building to make sure the connection’s established before moving on. If you’re using an IDE for development, completely stop the previous instance before starting a new one. Some IDEs don’t kill the JVM process cleanly and leave ghost connections running.

your bot sessions aren’t ending when you restart - the discord gateway stays connected and new instances keep stacking listeners. i hit this same bug. check task manager for leftover java processes after you “stop” the bot and kill them manually before restarting. also throw system.exit(0) at the end of your main method to force cleanup.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.