1

I am recently into discord.JDA and have been learning and trying to code my bot. I wanted to ask something about it as I was making the purge/delete command with the help of some source codes and my knowledge. My code does not give any error but my bot won't respond at all and also am using the latest version of JDA. I have also registered it in the main class. Would appreciate it if you could help me in any of the ways possible :D

here is my code:-


import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ClearCommand extends ListenerAdapter {
    public String prefix = "-";

    public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
        String[] args = event.getMessage().getContentRaw().split("\\s+");
        if (args[0].equalsIgnoreCase(prefix + "purge")) {
            if (args.length < 2) {
                EmbedBuilder error1 = new EmbedBuilder();
                error1.setColor(0x95142A);
                error1.setTitle("❌ You must specify # of messages to delete!");
                error1.setDescription("Usage: " + prefix + "purge [# of messages]");
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage((CharSequence) error1.build()).queue(m -> m.delete().queueAfter(3, TimeUnit.SECONDS));
            } else if (Integer.parseInt(args[0]) > 100 || Integer.parseInt(args[1]) < 1) {
                EmbedBuilder error2 = new EmbedBuilder();
                error2.setColor(0x95142A);
                error2.setTitle("❌ Only upto 100 messages can be deleted!");
                error2.setDescription("Usage : " + prefix + "purge [1-100]");
                event.getChannel().sendMessage((CharSequence) error2.build()).queue(m -> m.delete().queueAfter(3, TimeUnit.SECONDS));
            } else if (Integer.parseInt(args[0]) < 100 && Integer.parseInt(args[1]) > 1) {
                int values = Integer.parseInt(args[1]);
                event.getMessage().delete();
                List<Message> messages = event.getChannel().getHistory().retrievePast(values).complete();
                event.getTextChannel().deleteMessages(messages).queue();
                event.getChannel().sendMessage("✅ " + args[1].toString() + " messages deleted!").queue(m -> m.delete().queueAfter(3, TimeUnit.SECONDS));
            }
        }
    }
}











n1ckool
  • 11
  • 1
  • If you did sett up your bot correctly and you see it in green in your discord server , AND ,you don't see any exceptions/errors , it means simply all the "if" statements are false . So I suggest you debug your code, and you will see every line executing step by step, I assume you are using IntelIJ because you tagged it , so you can do it with one click . – Yasser CHENIK Jan 04 '22 at 13:41
  • yep! i did debug the whole code at once and there was no error, and yeah I I did setup my bot correctly in the developer portal THe only thing I cant understand that when I run the command "-purge 5", it just doesn't work for some reason – n1ckool Jan 04 '22 at 14:01
  • debug is not for finding errors , its just a fancy word of a "tool" that helps us fix some code that **already works** but not the way we want . if you want an example to see the important of debugging or just how to do it … [here is a video](https://youtu.be/-_tVIgImb5c?t=142) – Yasser CHENIK Jan 04 '22 at 14:39

0 Answers0