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.