Search code examples
javadiscordbotschatbotdiscord-jda

Discord bot - JDAs Event Listener sending mutiple messages


I'm creating a discord bot with JDA but I'm a newbie, right now my bot is working, but it's sending multiple messages, I think it's sending one more every time I run the code, I think it's something about the event listener, but not sure and don't know how to solve it, could someone help me with this? thank you.

This is my main file:

package en.devck.dc;

import javax.security.auth.login.LoginException;

import net.dv8tion.jda.api.AccountType;
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;
import net.dv8tion.jda.api.entities.Activity.ActivityType;

public class devck {
    public static JDA jda;
    public static String prefix = "*";
    
    // Main method
    public static void main(String[] args) throws LoginException {
        jda = JDABuilder.createDefault("my token").build();
        jda.getPresence().setStatus(OnlineStatus.ONLINE);
        Activity act = Activity.of(ActivityType.WATCHING, "Cowboy Bebop");
        jda.getPresence().setActivity(act);
        
        Commands cmd = new Commands();
        jda.addEventListener(cmd);
        
    }
            
    
}

this is my commands file:

package en.devck.dc;

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

public class Commands extends ListenerAdapter{
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        String[] args = event.getMessage().getContentRaw().split("\\s+"); 
        
        if(args[0].startsWith(devck.prefix)) {
            args[0] = args[0].substring(1);
            switch (args[0]) {
            case "info":
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage("Hey there! There is a new bot over here").queue();
                break;
            case "greet":
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage(event.getAuthor().getName()+" is sending greetings to "+args[1]).queue();
                break;
            default:
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage("I did not understand your command... but I'm learning!").queue();
                break;
            }
            
        }
    }

}

When I type "*info" it says something like:

  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"
  • "Hey there! There is a new bot over here"

Solution

  • Depending on your IDE, you might have to stop the java application running.

    Eclipse: Top bar -> Debugging/Run tools -> Red Square (This might have a number showing how many instances are running at the same time)

    Intellij:

    • Intelij Debugger/Code Run

    How to prevent multiple bots running at the same time (Intellij Only):

    • Select runner config
    • Disallow multipl instances
    • Disable the "Allow Multiple Instances" in your run options.