-1

So I'm trying to build a bot for discord in java but whenever a command is executed twice,it doesn't work the second time. I know there must be a minuscule mistake or I am doing something wrong. For example: When !spotify is entered, the action takes place only when the bot is loaded the first time, and when you again type any !spotify, !minecraft, !origin nothing happens and there are no errors.

public class FileEvent extends ListenerAdapter {

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {



    /*All the paths are here*/
    String SPOTIFY_PATH = "enter path here";
    String MINECRAFT_PATH = "enter path here";
    String ORIGIN_PATH = "enter path here";

    /*All the file objects are here*/
    File Minecraft = new File(MINECRAFT_PATH);
    File Spotify = new File(SPOTIFY_PATH);
    File Origin = new File(ORIGIN_PATH);

    String message = event.getMessage().getContentRaw();

    if (message.equalsIgnoreCase("!spotify")) {
        Scanner ss = null;
        try {
            ss = new Scanner(Spotify);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String data_spotify = ss.nextLine().trim();

        event.getAuthor().openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessage("sup")).queue();
        ss.close();

        FileDelete.delete(data_spotify.trim(), SPOTIFY_PATH);
    }

    if (message.equalsIgnoreCase("!minecraft")) {
        Scanner sm = null;
        try {
            sm = new Scanner(Minecraft);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String data_minecraft = sm.nextLine().trim();


        event.getAuthor().openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessage("sup")).queue();
        sm.close();

        FileDelete.delete(data_minecraft.trim(), MINECRAFT_PATH);
    }

    if (message.equalsIgnoreCase("!origin")) {
        Scanner sn = null;
        try {
            sn = new Scanner(Origin);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String data_origin = sn.nextLine().trim();


        event.getAuthor().openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessage("sup")).queue();
        sn.close();

        FileDelete.delete(data_origin.trim(), ORIGIN_PATH);
    }


}

}

PrajwalK
  • 21
  • 1
  • 5
  • 2
    `try/catch` doesn't loop. – Nexevis Mar 06 '20 at 19:38
  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – dan1st Mar 06 '20 at 19:41
  • When you close a Scanner, it closes the underlying stream. – NomadMaker Mar 19 '20 at 16:54
  • Not your bug, but your three files start with capital letters. Java conventions have variables starting with lower case. – NomadMaker Mar 19 '20 at 16:57

1 Answers1

0

I'm going to be honest, I don't know where your code goes wrong, but I'm sure you should not chain together lots of commands. The whole command handling could be done much easier with JDA-Utilities.

If you want to create a new command, you should
1. Create a new java class
2. Make the newly created class an extension of the Command class like so:

   public class ExampleCommand extends Command { /* code here */ }
  1. Create constructor and set the desired properties for command.
  2. Override the execute method:
   @Override
   protected void execute(CommandEvent event) { /* code for the command here */ }
  1. Add the command to your client / jda.
   client.addCommands(new ExampleCommand());

You can find a complete discord bot example (JDA + JDA Utilities) here which is easy to read and modify.

lireoy
  • 36
  • 2