use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.
the class BlacklistCommand method reset.
@Command(value = "reset", description = "Reset the blacklist for a specific role/user in a channel")
@CommandId(181)
@Examples({ "blacklist reset #channel", "blacklist reset all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void reset(Sx4CommandEvent event, @Argument(value = "channel", endless = true) @AlternativeOptions("all") Alternative<TextChannel> option) {
List<Bson> update = List.of(Operators.set("holders", Operators.reduce(Operators.ifNull("$holders", Collections.EMPTY_LIST), Collections.EMPTY_LIST, Operators.concatArrays("$$value", Operators.cond(Operators.isEmpty(Operators.ifNull(Operators.first(Operators.map(List.of("$$this"), "$$holder.whitelisted", "holder")), Collections.EMPTY_LIST)), Collections.EMPTY_LIST, List.of(Operators.removeObject("$$this", "blacklisted")))))));
if (option.isAlternative()) {
event.getMongo().updateManyBlacklists(Filters.eq("guildId", event.getGuild().getIdLong()), update, new UpdateOptions()).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Nothing was blacklisted in this server").queue();
return;
}
event.replySuccess("Reset **" + result.getModifiedCount() + "** channels of their blacklist configurations").queue();
});
} else {
TextChannel channel = option.getValue();
event.getMongo().updateBlacklist(Filters.eq("channelId", channel.getIdLong()), update, new UpdateOptions()).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Nothing was blacklisted in that channel").queue();
return;
}
event.replySuccess("That channel no longer has any blacklists").queue();
});
}
}
use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.
the class WhitelistCommand method reset.
@Command(value = "reset", description = "Reset the whitelist for a specific role/user in a channel")
@CommandId(186)
@Examples({ "whitelist reset #channel", "whitelist reset all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void reset(Sx4CommandEvent event, @Argument(value = "channel | all", endless = true) @AlternativeOptions("all") Alternative<TextChannel> option) {
List<Bson> update = List.of(Operators.set("holders", Operators.reduce(Operators.ifNull("$holders", Collections.EMPTY_LIST), Collections.EMPTY_LIST, Operators.concatArrays("$$value", Operators.cond(Operators.isEmpty(Operators.ifNull(Operators.first(Operators.map(List.of("$$this"), "$$holder.blacklisted", "holder")), Collections.EMPTY_LIST)), Collections.EMPTY_LIST, List.of(Operators.removeObject("$$this", "whitelisted")))))));
if (option.isAlternative()) {
event.getMongo().updateManyBlacklists(Filters.eq("guildId", event.getGuild().getIdLong()), update, new UpdateOptions()).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Nothing was whitelisted in this server").queue();
return;
}
event.replySuccess("Reset **" + result.getModifiedCount() + "** channels of their whitelist configurations").queue();
});
} else {
TextChannel channel = option.getValue();
event.getMongo().updateBlacklist(Filters.eq("channelId", channel.getIdLong()), update, new UpdateOptions()).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Nothing was whitelisted in that channel").queue();
return;
}
event.replySuccess("That channel no longer has any whitelists").queue();
});
}
}
use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.
the class FactoryCommand method buy.
@Command(value = "buy", description = "Buy a factory with some materials")
@CommandId(396)
@Examples({ "factory buy 5 Shoe Factory", "factory buy Shoe Factory", "factory buy all" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void buy(Sx4CommandEvent event, @Argument(value = "factories", endless = true) @AlternativeOptions("all") Alternative<ItemStack<Factory>> option) {
ItemStack<Factory> stack = option.getValue();
event.getMongo().withTransaction(session -> {
Bson userFilter = Filters.eq("userId", event.getAuthor().getIdLong()), filter;
List<Factory> factories;
if (stack == null) {
filter = Filters.and(userFilter, Filters.eq("item.type", ItemType.MATERIAL.getId()));
factories = event.getBot().getEconomyManager().getItems(Factory.class);
} else {
Factory factory = stack.getItem();
filter = Filters.and(userFilter, Filters.eq("item.id", factory.getCost().getItem().getId()));
factories = List.of(factory);
}
List<Document> materials = event.getMongo().getItems().find(session, filter).projection(Projections.include("amount", "item.id")).into(new ArrayList<>());
List<ItemStack<Factory>> boughtFactories = new ArrayList<>();
Factories: for (Factory factory : factories) {
ItemStack<Material> cost = factory.getCost();
Material costMaterial = cost.getItem();
for (Document material : materials) {
int id = material.getEmbedded(List.of("item", "id"), Integer.class);
if (costMaterial.getId() == id) {
long buyableAmount = (long) Math.floor((double) material.getLong("amount") / cost.getAmount());
long amount = stack == null ? buyableAmount : stack.getAmount();
if (amount == 0 || amount > buyableAmount) {
continue Factories;
}
event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", id)), Updates.inc("amount", -amount * cost.getAmount()));
List<Bson> update = List.of(Operators.set("item", factory.toData()), Operators.set("amount", Operators.add(Operators.ifNull("$amount", 0L), amount)));
event.getMongo().getItems().updateOne(session, Filters.and(userFilter, Filters.eq("item.id", factory.getId())), update, new UpdateOptions().upsert(true));
boughtFactories.add(new ItemStack<>(factory, amount));
}
}
}
return boughtFactories;
}).whenComplete((factories, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (factories.isEmpty()) {
event.replyFailure("You cannot afford " + (stack == null ? "any factories" : "`" + stack.getAmount() + " " + stack.getItem().getName() + "`")).queue();
return;
}
String factoriesBought = factories.stream().sorted(Collections.reverseOrder(Comparator.comparingLong(ItemStack::getAmount))).map(ItemStack::toString).collect(Collectors.joining("\n• "));
EmbedBuilder embed = new EmbedBuilder().setColor(event.getMember().getColor()).setAuthor(event.getAuthor().getName(), null, event.getAuthor().getEffectiveAvatarUrl()).setDescription("With all your materials you have bought the following factories\n\n• " + factoriesBought);
event.reply(embed.build()).queue();
});
}
use of com.sx4.bot.annotations.argument.AlternativeOptions 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;
}
String acceptId = new CustomButtonId.Builder().setType(ButtonType.FISHING_ROD_REPAIR_CONFIRM).setTimeout(60).setOwners(event.getAuthor().getIdLong()).setArguments(item.getId(), rod.getId(), rod.getDurability(), durability).getId();
String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setTimeout(60).setOwners(event.getAuthor().getIdLong()).getId();
List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
int itemCount = (int) Math.ceil((((double) rod.getPrice() / item.getPrice()) / rod.getMaxDurability()) * durability);
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).queue();
}
use of com.sx4.bot.annotations.argument.AlternativeOptions 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;
}
String acceptId = new CustomButtonId.Builder().setType(ButtonType.AXE_REPAIR_CONFIRM).setTimeout(60).setOwners(event.getAuthor().getIdLong()).setArguments(item.getId(), axe.getId(), axe.getDurability(), durability).getId();
String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setTimeout(60).setOwners(event.getAuthor().getIdLong()).getId();
List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
int itemCount = (int) Math.ceil((((double) axe.getPrice() / item.getPrice()) / axe.getMaxDurability()) * durability);
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).queue();
}
Aggregations