use of com.sx4.bot.waiter.Waiter in project Sx4 by sx4-discord-bot.
the class FishingRodCommand method repair.
@Command(value = "repair", description = "Repair your current fishing rod with the material it is made from")
@CommandId(383)
@Examples({ "fishing rod repair 10", "fishing rod repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.ROD.getId()));
Document data = event.getMongo().getItem(filter, Projections.include("item"));
if (data == null) {
event.replyFailure("You do not have a fishing rod").queue();
return;
}
Rod rod = Rod.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
CraftItem item = rod.getRepairItem();
if (item == null) {
event.replyFailure("That fishing rod is not repairable").queue();
return;
}
int maxDurability = rod.getMaxDurability() - rod.getDurability();
if (maxDurability <= 0) {
event.replyFailure("Your fishing rod is already at full durability").queue();
return;
}
int durability;
if (option.isAlternative()) {
durability = maxDurability;
} else {
int amount = option.getValue();
if (amount > maxDurability) {
event.reply("You can only repair your fishing rod by **" + maxDurability + "** durability :no_entry:").queue();
return;
}
durability = amount;
}
int itemCount = (int) Math.ceil((((double) rod.getPrice() / item.getPrice()) / rod.getMaxDurability()) * durability);
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your fishing rod by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
List<Bson> update = List.of(Operators.set("amount", Operators.let(new Document("amount", Operators.ifNull("$amount", 0L)), Operators.cond(Operators.lte(itemCount, "$$amount"), Operators.subtract("$$amount", itemCount), "$$amount"))));
event.getMongo().updateItem(Filters.and(Filters.eq("item.id", item.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), update, new UpdateOptions()).thenCompose(result -> {
if (result.getMatchedCount() == 0 || result.getModifiedCount() == 0) {
e.reply("You do not have `" + itemCount + " " + item.getName() + "` " + event.getConfig().getFailureEmote()).queue();
return CompletableFuture.completedFuture(null);
}
List<Bson> itemUpdate = List.of(Operators.set("item.durability", Operators.cond(Operators.eq("$item.durability", rod.getDurability()), Operators.add("$item.durability", durability), "$item.durability")));
return event.getMongo().updateItem(Filters.and(Filters.eq("item.id", rod.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), itemUpdate, new UpdateOptions());
}).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException) || result == null) {
return;
}
if (result.getMatchedCount() == 0) {
e.reply("You no longer have that fishing rod " + event.getConfig().getFailureEmote()).queue();
return;
}
if (result.getMatchedCount() == 0) {
e.reply("The durability of your fishing rod has changed " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("You just repaired your fishing rod by **" + durability + "** durability " + event.getConfig().getSuccessEmote()).queue();
});
});
}
use of com.sx4.bot.waiter.Waiter in project Sx4 by sx4-discord-bot.
the class AxeCommand method repair.
@Command(value = "repair", description = "Repair your current axe with the material it is made from")
@CommandId(392)
@Examples({ "axe repair 10", "axe repair all" })
public void repair(Sx4CommandEvent event, @Argument(value = "durability") @AlternativeOptions("all") Alternative<Integer> option) {
Bson filter = Filters.and(Filters.eq("userId", event.getAuthor().getIdLong()), Filters.eq("item.type", ItemType.AXE.getId()));
Document data = event.getMongo().getItem(filter, Projections.include("item"));
if (data == null) {
event.replyFailure("You do not have a axe").queue();
return;
}
Axe axe = Axe.fromData(event.getBot().getEconomyManager(), data.get("item", Document.class));
CraftItem item = axe.getRepairItem();
if (item == null) {
event.replyFailure("That axe is not repairable").queue();
return;
}
int maxDurability = axe.getMaxDurability() - axe.getDurability();
if (maxDurability <= 0) {
event.replyFailure("Your axe is already at full durability").queue();
return;
}
int durability;
if (option.isAlternative()) {
durability = maxDurability;
} else {
int amount = option.getValue();
if (amount > maxDurability) {
event.reply("You can only repair your axe by **" + maxDurability + "** durability :no_entry:").queue();
return;
}
durability = amount;
}
int itemCount = (int) Math.ceil((((double) axe.getPrice() / item.getPrice()) / axe.getMaxDurability()) * durability);
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply("It will cost you `" + itemCount + " " + item.getName() + "` to repair your axe by **" + durability + "** durability, are you sure you want to repair it?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
List<Bson> update = List.of(Operators.set("amount", Operators.let(new Document("amount", Operators.ifNull("$amount", 0L)), Operators.cond(Operators.lte(itemCount, "$$amount"), Operators.subtract("$$amount", itemCount), "$$amount"))));
event.getMongo().updateItem(Filters.and(Filters.eq("item.id", item.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), update, new UpdateOptions()).thenCompose(result -> {
if (result.getMatchedCount() == 0 || result.getModifiedCount() == 0) {
e.reply("You do not have `" + itemCount + " " + item.getName() + "` " + event.getConfig().getFailureEmote()).queue();
return CompletableFuture.completedFuture(null);
}
List<Bson> itemUpdate = List.of(Operators.set("item.durability", Operators.cond(Operators.eq("$item.durability", axe.getDurability()), Operators.add("$item.durability", durability), "$item.durability")));
return event.getMongo().updateItem(Filters.and(Filters.eq("item.id", axe.getId()), Filters.eq("userId", event.getAuthor().getIdLong())), itemUpdate, new UpdateOptions());
}).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException) || result == null) {
return;
}
if (result.getMatchedCount() == 0) {
e.reply("You no longer have that axe " + event.getConfig().getFailureEmote()).queue();
return;
}
if (result.getMatchedCount() == 0) {
e.reply("The durability of your axe has changed " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("You just repaired your axe by **" + durability + "** durability " + event.getConfig().getSuccessEmote()).queue();
});
});
}
use of com.sx4.bot.waiter.Waiter in project Sx4 by sx4-discord-bot.
the class AutoRoleCommand method remove.
@Command(value = "remove", aliases = { "delete" }, description = "Remove a role from being given when a user joins")
@CommandId(40)
@Examples({ "auto role remove @Role", "auto role remove Role", "auto role remove all" })
@AuthorPermissions(permissions = { Permission.MANAGE_ROLES })
public void remove(Sx4CommandEvent event, @Argument(value = "role | all", endless = true) @AlternativeOptions("all") Alternative<Role> option) {
if (option.isAlternative()) {
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply(event.getAuthor().getName() + ", are you sure you want to remove every auto role in the server?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getMongo().deleteManyAutoRoles(Filters.eq("guildId", event.getGuild().getIdLong())).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException)) {
return;
}
if (result.getDeletedCount() == 0) {
e.reply("There are no auto roles in this server " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("All auto roles have been removed " + event.getConfig().getSuccessEmote()).queue();
});
});
} else {
Role role = option.getValue();
event.getMongo().deleteAutoRole(Filters.eq("roleId", role.getIdLong())).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getDeletedCount() == 0) {
event.replyFailure("That role is not an auto role").queue();
return;
}
event.replySuccess(role.getAsMention() + " is no longer an auto role").queue();
});
}
}
use of com.sx4.bot.waiter.Waiter in project Sx4 by sx4-discord-bot.
the class GiveawayCommand method delete.
@Command(value = "delete", aliases = { "remove" }, description = "Deletes a giveaway")
@CommandId(51)
@Examples({ "giveaway delete 727224132202397726", "giveaway delete all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void delete(Sx4CommandEvent event, @Argument(value = "message id | all") @AlternativeOptions("all") Alternative<MessageArgument> option) {
if (option.isAlternative()) {
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply(event.getAuthor().getName() + ", are you sure you want to delete **all** giveaways in this server?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getMongo().deleteManyGiveaways(Filters.eq("guildId", event.getGuild().getIdLong())).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException)) {
return;
}
if (result.getDeletedCount() == 0) {
e.reply("There are no giveaways in this server " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("All giveaways in this server have been deleted " + event.getConfig().getSuccessEmote()).queue();
});
});
} else {
long messageId = option.getValue().getMessageId();
event.getMongo().deleteGiveawayById(messageId).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getDeletedCount() == 0) {
event.replyFailure("There was no giveaway with that id").queue();
return;
}
event.replySuccess("That giveaway has been deleted").queue();
});
}
}
use of com.sx4.bot.waiter.Waiter in project Sx4 by sx4-discord-bot.
the class GiveawayCommand method setup.
@Command(value = "setup", description = "Setup giveaways for users to react to")
@CommandId(47)
@Examples({ "giveaway setup", "giveaway setup #giveaways 1 7d $10 Nitro" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void setup(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) TextChannel channel, @Argument(value = "winners") @DefaultNumber(0) @Limit(min = 1) int winners, @Argument(value = "duration", nullDefault = true) Duration duration, @Argument(value = "item", nullDefault = true, endless = true) String item) {
if (channel != null && winners != 0 && duration != null && item != null) {
long seconds = duration.toSeconds();
if (seconds < 1) {
event.replyFailure("The duration of a giveaway cannot be less than 1 second").queue();
return;
}
channel.sendMessageEmbeds(this.getEmbed(winners, seconds, item)).queue(message -> {
message.addReaction("🎉").queue();
Document data = new Document("messageId", message.getIdLong()).append("channelId", channel.getIdLong()).append("guildId", event.getGuild().getIdLong()).append("winnersAmount", winners).append("endAt", Clock.systemUTC().instant().getEpochSecond() + seconds).append("duration", seconds).append("item", item);
event.getMongo().insertGiveaway(data).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getBot().getGiveawayManager().putGiveaway(data, seconds);
event.reply("Your giveaway has been created in " + channel.getAsMention() + " :tada:").queue();
});
});
return;
}
AtomicReference<TextChannel> atomicChannel = new AtomicReference<>();
AtomicInteger atomicWinners = new AtomicInteger();
AtomicReference<Duration> atomicDuration = new AtomicReference<>();
AtomicReference<String> atomicItem = new AtomicReference<>();
CompletableFuture.completedFuture(true).thenCompose($ -> {
if (channel != null) {
atomicChannel.set(channel);
return CompletableFuture.completedFuture(true);
}
CompletableFuture<Boolean> future = new CompletableFuture<>();
event.reply("What channel would you like to start the giveaway in? Type `cancel` at anytime to cancel the creation.").queue(message -> {
Waiter<MessageReceivedEvent> waiter = new Waiter<>(event.getBot(), MessageReceivedEvent.class).setUnique(event.getAuthor().getIdLong(), event.getChannel().getIdLong()).setCancelPredicate(e -> e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setTimeout(30).setPredicate(e -> {
TextChannel textChannel = SearchUtility.getTextChannel(event.getGuild(), e.getMessage().getContentRaw());
if (textChannel != null) {
atomicChannel.set(textChannel);
return true;
}
event.replyFailure("I could not find that channel").queue();
return false;
});
waiter.onCancelled((type) -> {
event.replySuccess("Cancelled").queue();
future.complete(false);
});
waiter.onTimeout(() -> {
event.reply("Response timed out :stopwatch:").queue();
future.complete(false);
});
waiter.onSuccess(e -> future.complete(true));
waiter.start();
});
return future;
}).thenCompose(success -> {
if (!success) {
return CompletableFuture.completedFuture(false);
}
if (winners != 0) {
atomicWinners.set(winners);
return CompletableFuture.completedFuture(true);
}
CompletableFuture<Boolean> future = new CompletableFuture<>();
event.reply("How many winners would you like the giveaway to have?").queue(message -> {
Waiter<MessageReceivedEvent> waiter = new Waiter<>(event.getBot(), MessageReceivedEvent.class).setCancelPredicate(e -> e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setTimeout(30).setUnique(event.getAuthor().getIdLong(), event.getChannel().getIdLong()).setPredicate(e -> {
String content = e.getMessage().getContentRaw();
if (NumberUtility.isNumberUnsigned(content)) {
int number = Integer.parseInt(content);
if (number < 1) {
event.replyFailure("You have to have at least 1 winner").queue();
return false;
}
atomicWinners.set(number);
return true;
}
event.replyFailure("That is not a number").queue();
return false;
});
waiter.onCancelled((type) -> {
event.replySuccess("Cancelled").queue();
future.complete(false);
});
waiter.onTimeout(() -> {
event.reply("Response timed out :stopwatch:").queue();
future.complete(false);
});
waiter.onSuccess(e -> future.complete(true));
waiter.start();
});
return future;
}).thenCompose(success -> {
if (!success) {
return CompletableFuture.completedFuture(false);
}
if (duration != null) {
atomicDuration.set(duration);
return CompletableFuture.completedFuture(true);
}
CompletableFuture<Boolean> future = new CompletableFuture<>();
event.reply("How long would you like the giveaway to last?").queue(message -> {
Waiter<MessageReceivedEvent> waiter = new Waiter<>(event.getBot(), MessageReceivedEvent.class).setCancelPredicate(e -> e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setTimeout(30).setUnique(event.getAuthor().getIdLong(), event.getChannel().getIdLong()).setPredicate(e -> {
Duration durationReply = TimeUtility.getDurationFromString(e.getMessage().getContentRaw());
if (durationReply.toSeconds() < 1) {
event.replyFailure("The duration of a giveaway cannot be less than 1 second").queue();
return false;
}
atomicDuration.set(durationReply);
return true;
});
waiter.onCancelled((type) -> {
event.replySuccess("Cancelled").queue();
future.complete(false);
});
waiter.onTimeout(() -> {
event.reply("Response timed out :stopwatch:").queue();
future.complete(false);
});
waiter.onSuccess(e -> future.complete(true));
waiter.start();
});
return future;
}).thenCompose(success -> {
if (!success) {
return CompletableFuture.completedFuture(false);
}
if (item != null) {
atomicItem.set(item);
return CompletableFuture.completedFuture(true);
}
CompletableFuture<Boolean> future = new CompletableFuture<>();
event.reply("What would you like to giveaway?").queue(message -> {
Waiter<MessageReceivedEvent> waiter = new Waiter<>(event.getBot(), MessageReceivedEvent.class).setCancelPredicate(e -> e.getMessage().getContentRaw().equalsIgnoreCase("cancel")).setTimeout(30).setUnique(event.getAuthor().getIdLong(), event.getChannel().getIdLong()).setPredicate(e -> {
String content = e.getMessage().getContentRaw();
if (content.equalsIgnoreCase("cancel")) {
return false;
}
atomicItem.set(content);
return true;
});
waiter.onCancelled((type) -> {
event.replySuccess("Cancelled").queue();
future.complete(false);
});
waiter.onTimeout(() -> {
event.reply("Response timed out :stopwatch:").queue();
future.complete(false);
});
waiter.onSuccess(e -> future.complete(true));
waiter.start();
});
return future;
}).thenAccept(success -> {
if (!success) {
return;
}
TextChannel channelFuture = atomicChannel.get();
int winnersFuture = atomicWinners.get();
long durationFuture = atomicDuration.get().toSeconds();
String itemFuture = atomicItem.get();
channelFuture.sendMessageEmbeds(this.getEmbed(winnersFuture, durationFuture, itemFuture)).queue(message -> {
message.addReaction("🎉").queue();
Document data = new Document("messageId", message.getIdLong()).append("channelId", channelFuture.getIdLong()).append("guildId", event.getGuild().getIdLong()).append("winnersAmount", winnersFuture).append("endAt", Clock.systemUTC().instant().getEpochSecond() + durationFuture).append("duration", durationFuture).append("item", itemFuture);
event.getMongo().insertGiveaway(data).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getBot().getGiveawayManager().putGiveaway(data, durationFuture);
event.reply("Your giveaway has been created in " + channelFuture.getAsMention() + " :tada:").queue();
});
});
});
}
Aggregations