0

I'm trying to make a Discord-Bot which allows you to ping certain servers. I want to ping a server (which is offline) until it comes back online. So far so good.
Now I want to add a "!stop" command which canceles this process because of whatever reason.

public class cmdping implements Commands{


public boolean called(String[] args, MessageReceivedEvent event) {
    return false;
}



public void action(String[] args, MessageReceivedEvent event) {
    // TODO Auto-generated method stub
    if((args.length == 3)) { 
    switch (args[2]) {

    //Pings the server until it comes back online (won't work while it's online).
    case "-t":
        try{
            int i = 0;
            int q = 0;
            InetAddress address = InetAddress.getByName(args[0]);
            event.getTextChannel().sendMessage("Pinging _" + args[0] + " _(**" + args[1] + "**)_ ....._\n ").queue();
            boolean reachable = address.isReachable(2500);
            if (reachable) {
                event.getTextChannel().sendMessage("Server is online.").queue();
                i++;
                q++;
            } else {
                while(!address.isReachable(2500)) {
                    event.getTextChannel().sendMessage("_"+args[0] + "_  (**"+ args[1] +"**) isn't communicating.").queue();
                    q++;

            }                               
            double outcome = 0;
            outcome = i/q*100;
            event.getTextChannel().sendMessage(q+" Packages were sent. The server responded to *" + i + ".  " +i+"/"+q+" --> **"+outcome+"%**").queue();
            i = 0;
            q=0;
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        break;
      default:
        event.getTextChannel().sendMessage("Wrong arguments.").queue();
        break;
    }
    } else {
        event.getTextChannel().sendMessage("Command is not complete.").queue();
    }


}

Here is the stopcmd class

public class cmdstop implements Commands {
public boolean called(String[] args, MessageReceivedEvent event) {
    // TODO Auto-generated method stub
    return false;
}
public void action(String[] args, MessageReceivedEvent event) {
    // TODO Auto-generated method stub
}
public void executed(boolean safe, MessageReceivedEvent event) {
    // TODO Auto-generated method stb
    System.out.println("[INFO] Command 'stop' just got used!");
}
public String help() {
    // TODO Auto-generated method stub
    return null;
} }

What would I have to do to implement the stop command? I've tried out multiple things already which didn't work out for me.

EDIT: CommandListener & CommandHandler

public class commandListener extends ListenerAdapter {

public void onMessageReceived(MessageReceivedEvent event) {

    if(event.getMessage().getContentRaw().startsWith(STATIC.PREFIX) && (event.getMessage().getAuthor().getId() != event.getJDA().getSelfUser().getId())) {
        commandHandler.handleCommand(CommandParser.parser(event.getMessage().getContentRaw(), event));
    }

} }

public class commandHandler {

public static final CommandParser parse = new CommandParser();
public static HashMap<String, Commands> commands = new HashMap<String, Commands>();

public static void handleCommand(CommandParser.commandContainer cmd) {
    if(commands.containsKey(cmd.invoke)) {
        boolean safe = commands.get(cmd.invoke).called(cmd.args, cmd.event);

        if (!safe) {
            commands.get(cmd.invoke).action(cmd.args, cmd.event);
            commands.get(cmd.invoke).executed(safe, cmd.event);
        } else {
            commands.get(cmd.invoke).executed(safe, cmd.event);
        }
    }
}}

Now there is another problem. During the while-loop it doesn't detect any other commands.

xFish
  • 23
  • 6
  • Why did you post the `cmdping` if you want help with `stopcmd`? And, what is the `cmdstop` supposed to do? You currently print that it got used. Is that all you want? – Elliott Frisch Feb 28 '18 at 15:47
  • you could just add a boolean into your while loop `while(!address.isReachable(2500) && continuePinging) ` and then have your cmdstop set that boolean to false – Kudu2 Feb 28 '18 at 15:53
  • @Kudu2 It seems like that my commandListener doesn't detect any other commands during the loop. – xFish Feb 28 '18 at 16:06
  • @ElliottFrisch I want to stop the loop via the stop command. But thanks to Kudu2 I just figured out that my commandListener doesn't detect any more commands during the loop, which brings me to the next problem. – xFish Feb 28 '18 at 16:07
  • instead of a while loop, you could use a TimerTask which isnt blocking. then to stop it you can cancel the task instead of doing the boolean thing. – Kudu2 Feb 28 '18 at 16:13
  • see https://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java Declare your timer at the start of your cmdPing class. Then initialize it inside your else statement with the repetition requirements. Then you can cancel it if the server comes online and also create a function that calls timer.cancel() – Kudu2 Feb 28 '18 at 16:18
  • @Kudu2 Thank you very much. It works now :) – xFish Feb 28 '18 at 16:51
  • @Kudu2 Okay, nevermind. When I want to ping another server after stopping a process before it throws "java.lang.IllegalStateException: Timer already cancelled." at me. I can't re-create the timer somehow at the start of the class. – xFish Feb 28 '18 at 17:06
  • when you set the timer in your else statement, you might need to first call `timer = new Timer();` and then you can run `timer.schedule(new TimerTask() { ... });` – Kudu2 Feb 28 '18 at 17:56

0 Answers0