use of org.joda.time.format.PeriodFormatter in project FlareBot by FlareBot.
the class UpdateCommand method onCommand.
@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
if (PerGuildPermissions.isCreator(sender)) {
if (args.length == 0) {
update(false, channel);
} else if (args.length == 1) {
if (args[0].equalsIgnoreCase("force")) {
update(true, channel);
} else if (args[0].equalsIgnoreCase("no-active-channels")) {
channel.sendMessage("I will now update to the latest version when no channels are playing music!").queue();
if (Getters.getConnectedVoiceChannels() == 0) {
update(true, channel);
} else {
if (!queued.getAndSet(true)) {
FlareBot.NOVOICE_UPDATING.set(true);
} else
channel.sendMessage("There is already an update queued!").queue();
}
} else if (args[0].equalsIgnoreCase("schedule")) {
if (!queued.getAndSet(true)) {
FlareBot.instance().scheduleUpdate();
MessageUtils.sendSuccessMessage("Update scheduled for 12PM GMT!", channel);
} else {
MessageUtils.sendErrorMessage("There is already an update queued!", channel);
}
} else if (args[0].equalsIgnoreCase("cancel")) {
if (!queued.getAndSet(true)) {
MessageUtils.sendErrorMessage("There is no update queued!", channel);
} else {
if (Scheduler.cancelTask("Scheduled-Update")) {
MessageUtils.sendSuccessMessage("Cancelled Update!", channel);
} else {
MessageUtils.sendErrorMessage("Could not cancel update!", channel);
}
}
} else {
if (!queued.getAndSet(true)) {
Period p;
try {
PeriodFormatter formatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d").appendHours().appendSuffix("h").appendMinutes().appendSuffix("m").appendSeconds().appendSuffix("s").toFormatter();
p = formatter.parsePeriod(args[0]);
new FlareBotTask("Scheduled-Update") {
@Override
public void run() {
update(true, channel);
}
}.delay(TimeUnit.SECONDS.toMillis(p.toStandardSeconds().getSeconds()));
} catch (IllegalArgumentException e) {
channel.sendMessage("That is an invalid time option!").queue();
return;
}
channel.sendMessage("I will now update to the latest version in " + p.toStandardSeconds().getSeconds() + " seconds.").queue();
} else {
channel.sendMessage("There is already an update queued!").queue();
}
}
}
}
}
use of org.joda.time.format.PeriodFormatter in project FlareBot by FlareBot.
the class GeneralUtils method parseTime.
/**
* Parses time from string.
* The input can be in these formats
* s
* m:s
* h:m:s
* XhXmXs
* Examples:
* 5 - Would be 5 seconds
* 5s - Would also be 5 seconds
* 1:10 - Would be 1 minute and 10 seconds
* 1m10s - Would also be 1 minute and 10 seconds
* 1:00:00 - Would be an hour
* 1h - Would also be an hour
*
* @param time The time string to parse.
* @return The long representing the time entered in millis from when this method was ran.
*/
public static Long parseTime(String time) {
Matcher digitMatcher = timeRegex.matcher(time);
if (digitMatcher.matches()) {
try {
return new PeriodFormatterBuilder().appendHours().appendSuffix(":").appendMinutes().appendSuffix(":").appendSeconds().toFormatter().parsePeriod(time).toStandardDuration().getMillis();
} catch (IllegalArgumentException e) {
return null;
}
}
PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h").appendMinutes().appendSuffix("m").appendSeconds().appendSuffix("s").toFormatter();
Period period;
try {
period = formatter.parsePeriod(time);
} catch (IllegalArgumentException e) {
return null;
}
return period.toStandardDuration().getMillis();
}
Aggregations