使用stop-command打破while循环

时间:2018-02-28 15:42:50

标签: java while-loop discord

我正在尝试创建一个Discord-Bot,它允许您ping某些服务器。我想ping一台服务器(离线),直到它恢复在线状态。到目前为止一切顺利。
现在我想添加一个“!stop”命令,它会因为任何原因而取消此过程。

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();
    }


}

这是stopcmd类

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;
} }

执行stop命令需要做什么?我已经尝试了一些对我来说没有用的东西。

编辑: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);
        }
    }
}}

现在还有另一个问题。在while循环期间,它不会检测任何其他命令。

0 个答案:

没有答案