use of net.kodehawa.mantarobot.db.entities.Marriage in project MantaroBot by Mantaro.
the class MarryCmd method marry.
@Subscribe
public void marry(CommandRegistry cr) {
final IncreasingRateLimiter rateLimiter = new IncreasingRateLimiter.Builder().limit(1).cooldown(10, TimeUnit.MINUTES).maxCooldown(40, TimeUnit.MINUTES).randomIncrement(false).pool(MantaroData.getDefaultJedisPool()).prefix("marry").build();
ITreeCommand marryCommand = cr.register("marry", new TreeCommand(CommandCategory.CURRENCY) {
@Override
public Command defaultTrigger(Context ctx, String mainCommand, String commandName) {
return new SubCommand() {
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
if (ctx.getMentionedUsers().isEmpty()) {
ctx.sendLocalized("commands.marry.no_mention", EmoteReference.ERROR);
return;
}
// We don't need to change those. I sure fucking hope we don't.
final DBGuild dbGuild = ctx.getDBGuild();
User proposingUser = ctx.getAuthor();
User proposedToUser = ctx.getMentionedUsers().get(0);
// This is just for checking purposes, so we don't need the DBUser itself.
UserData proposingUserData = ctx.getDBUser(proposingUser).getData();
UserData proposedToUserData = ctx.getDBUser(proposedToUser).getData();
// Again just for checking, and no need to change.
final Inventory proposingPlayerInventory = ctx.getPlayer(proposingUser).getInventory();
// Why would you do this...
if (proposedToUser.getId().equals(ctx.getAuthor().getId())) {
ctx.sendLocalized("commands.marry.marry_yourself_notice", EmoteReference.ERROR);
return;
}
final Marriage proposingMarriage = proposingUserData.getMarriage();
final Marriage proposedToMarriage = proposedToUserData.getMarriage();
// Proposed to is a bot user, cannot marry bots, this is still not 2100.
if (proposedToUser.isBot()) {
ctx.sendLocalized("commands.marry.marry_bot_notice", EmoteReference.ERROR);
return;
}
// Already married to the same person you're proposing to.
if ((proposingMarriage != null && proposedToMarriage != null) && proposedToUserData.getMarriage().getId().equals(proposingMarriage.getId())) {
ctx.sendLocalized("commands.marry.already_married_receipt", EmoteReference.ERROR);
return;
}
// You're already married. Huh huh.
if (proposingMarriage != null) {
ctx.sendLocalized("commands.marry.already_married", EmoteReference.ERROR);
return;
}
// Receipt is married, cannot continue.
if (proposedToMarriage != null) {
ctx.sendLocalized("commands.marry.receipt_married", EmoteReference.ERROR);
return;
}
// Not enough rings to continue. Buy more rings w.
if (!proposingPlayerInventory.containsItem(ItemReference.RING) || proposingPlayerInventory.getAmount(ItemReference.RING) < 2) {
ctx.sendLocalized("commands.marry.no_ring", EmoteReference.ERROR);
return;
}
// Check for rate limit
if (!RatelimitUtils.ratelimit(rateLimiter, ctx, ctx.getLanguageContext().get("commands.marry.ratelimit_message"), false))
return;
// Send confirmation message.
ctx.sendLocalized("commands.marry.confirmation", EmoteReference.MEGA, proposedToUser.getName(), ctx.getAuthor().getName(), EmoteReference.STOPWATCH);
InteractiveOperations.create(ctx.getChannel(), ctx.getAuthor().getIdLong(), 120, (ie) -> {
// Ignore all messages from anyone that isn't the user we already proposed to. Waiting for confirmation...
if (!ie.getAuthor().getId().equals(proposedToUser.getId()))
return Operation.IGNORED;
// Replace prefix because people seem to think you have to add the prefix before saying yes.
String message = ie.getMessage().getContentRaw();
for (String s : ctx.getConfig().prefix) {
if (message.toLowerCase().startsWith(s)) {
message = message.substring(s.length());
}
}
String guildCustomPrefix = dbGuild.getData().getGuildCustomPrefix();
if (guildCustomPrefix != null && !guildCustomPrefix.isEmpty() && message.toLowerCase().startsWith(guildCustomPrefix)) {
message = message.substring(guildCustomPrefix.length());
}
// Lovely~ <3
if (message.equalsIgnoreCase("yes")) {
// Here we NEED to get the Player,
// User and Marriage objects once again
// to avoid race conditions or changes on those that might have happened on the 120 seconds that this lasted for.
// We need to check if the marriage is empty once again before continuing, also if we have enough rings!
// Else we end up with really annoying to debug bugs, lol.
Player proposingPlayer = ctx.getPlayer(proposingUser);
Player proposedToPlayer = ctx.getPlayer(proposedToUser);
DBUser proposingUserDB = ctx.getDBUser(proposingUser);
DBUser proposedToUserDB = ctx.getDBUser(proposedToUser);
final Marriage proposingMarriageFinal = proposingUserDB.getData().getMarriage();
final Marriage proposedToMarriageFinal = proposedToUserDB.getData().getMarriage();
if (proposingMarriageFinal != null) {
ctx.sendLocalized("commands.marry.already_married", EmoteReference.ERROR);
return Operation.COMPLETED;
}
if (proposedToMarriageFinal != null) {
ctx.sendLocalized("commands.marry.receipt_married", EmoteReference.ERROR);
return Operation.COMPLETED;
}
// LAST inventory check and ring assignment is gonna happen using those.
final Inventory proposingPlayerFinalInventory = proposingPlayer.getInventory();
final Inventory proposedToPlayerInventory = proposedToPlayer.getInventory();
if (proposingPlayerFinalInventory.getAmount(ItemReference.RING) < 2) {
ctx.sendLocalized("commands.marry.ring_check_fail", EmoteReference.ERROR);
return Operation.COMPLETED;
}
// Remove the ring from the proposing player inventory.
proposingPlayerFinalInventory.process(new ItemStack(ItemReference.RING, -1));
// Silently scrape the rings if the receipt has more than 5000 rings.
if (proposedToPlayerInventory.getAmount(ItemReference.RING) < 5000) {
proposedToPlayerInventory.process(new ItemStack(ItemReference.RING, 1));
}
final long marriageCreationMillis = Instant.now().toEpochMilli();
// Onto the UUID we need to encode userId + timestamp of
// the proposing player and the proposed to player after the acceptance is done.
String marriageId = new UUID(proposingUser.getIdLong(), proposedToUser.getIdLong()).toString();
// Make and save the new marriage object.
Marriage actualMarriage = Marriage.of(marriageId, proposingUser, proposedToUser);
actualMarriage.getData().setMarriageCreationMillis(marriageCreationMillis);
actualMarriage.save();
// Assign the marriage ID to the respective users and save it.
proposingUserDB.getData().setMarriageId(marriageId);
proposedToUserDB.getData().setMarriageId(marriageId);
proposingUserDB.save();
proposedToUserDB.save();
// Send marriage confirmation message.
ctx.sendLocalized("commands.marry.accepted", EmoteReference.POPPER, ie.getAuthor().getName(), ie.getAuthor().getDiscriminator(), proposingUser.getName(), proposingUser.getDiscriminator());
// Add the badge to the married couple.
proposingPlayer.getData().addBadgeIfAbsent(Badge.MARRIED);
proposedToPlayer.getData().addBadgeIfAbsent(Badge.MARRIED);
// Give a love letter both to the proposing player and the one who was proposed to.
if (proposingPlayerFinalInventory.getAmount(ItemReference.LOVE_LETTER) < 5000) {
proposingPlayerFinalInventory.process(new ItemStack(ItemReference.LOVE_LETTER, 1));
}
if (proposedToPlayerInventory.getAmount(ItemReference.LOVE_LETTER) < 5000) {
proposedToPlayerInventory.process(new ItemStack(ItemReference.LOVE_LETTER, 1));
}
// Badge assignment saving.
proposingPlayer.save();
proposedToPlayer.save();
return Operation.COMPLETED;
}
if (message.equalsIgnoreCase("no")) {
ctx.sendLocalized("commands.marry.denied", EmoteReference.CORRECT, proposingUser.getName());
// Well, we have a badge for this too. Consolation prize I guess.
final Player proposingPlayer = ctx.getPlayer(proposingUser);
if (proposingPlayer.getData().addBadgeIfAbsent(Badge.DENIED)) {
proposingPlayer.saveUpdating();
}
return Operation.COMPLETED;
}
return Operation.IGNORED;
});
}
};
}
@Override
public HelpContent help() {
return new HelpContent.Builder().setDescription("Basically marries you with a user.").setUsage("`~>marry <@mention>` - Propose to someone\n" + "`~>marry <command>`").addParameter("@mention", "The person to propose to").addParameter("command", "The subcommand you can use. Check the subcommands section for a list and usage of each.").build();
}
});
marryCommand.addSubCommand("createletter", new SubCommand() {
@Override
public String description() {
return "Create a love letter for your marriage. Usage: `~>marry createletter <content>`";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
final User author = ctx.getAuthor();
Player player = ctx.getPlayer();
Inventory playerInventory = player.getInventory();
DBUser dbUser = ctx.getDBUser();
// Without one love letter we cannot do much, ya know.
if (playerInventory.containsItem(ItemReference.LOVE_LETTER)) {
final Marriage currentMarriage = dbUser.getData().getMarriage();
// that the love letter is less than 1500 characters long.
if (currentMarriage == null) {
ctx.sendLocalized("commands.marry.loveletter.no_marriage", EmoteReference.SAD);
return;
}
if (currentMarriage.getData().getLoveLetter() != null) {
ctx.sendLocalized("commands.marry.loveletter.already_done", EmoteReference.ERROR);
return;
}
if (content.isEmpty()) {
ctx.sendLocalized("commands.marry.loveletter.empty", EmoteReference.ERROR);
return;
}
if (content.length() > 500) {
ctx.sendLocalized("commands.marry.loveletter.too_long", EmoteReference.ERROR);
return;
}
// Can we find the user this is married to?
final User marriedTo = ctx.retrieveUserById(currentMarriage.getOtherPlayer(author.getId()));
if (marriedTo == null) {
ctx.sendLocalized("commands.marry.loveletter.cannot_see", EmoteReference.ERROR);
return;
}
// Send a confirmation message.
String finalContent = Utils.DISCORD_INVITE.matcher(content).replaceAll("-invite link-");
finalContent = Utils.DISCORD_INVITE_2.matcher(finalContent).replaceAll("-invite link-");
ctx.sendStrippedLocalized("commands.marry.loveletter.confirmation", EmoteReference.TALKING, marriedTo.getName(), marriedTo.getDiscriminator(), finalContent);
// Start the operation.
InteractiveOperations.create(ctx.getChannel(), author.getIdLong(), 60, e -> {
if (!e.getAuthor().getId().equals(author.getId())) {
return Operation.IGNORED;
}
// Replace prefix because people seem to think you have to add the prefix before saying yes.
String c = e.getMessage().getContentRaw();
for (String s : ctx.getConfig().prefix) {
if (c.toLowerCase().startsWith(s)) {
c = c.substring(s.length());
}
}
String guildCustomPrefix = ctx.getDBGuild().getData().getGuildCustomPrefix();
if (guildCustomPrefix != null && !guildCustomPrefix.isEmpty() && c.toLowerCase().startsWith(guildCustomPrefix)) {
c = c.substring(guildCustomPrefix.length());
}
// Confirmed they want to save this as the permanent love letter.
if (c.equalsIgnoreCase("yes")) {
final Player playerFinal = ctx.getPlayer();
final Inventory inventoryFinal = playerFinal.getInventory();
final Marriage currentMarriageFinal = dbUser.getData().getMarriage();
// We need to do most of the checks all over again just to make sure nothing important slipped through.
if (currentMarriageFinal == null) {
ctx.sendLocalized("commands.marry.loveletter.no_marriage", EmoteReference.SAD);
return Operation.COMPLETED;
}
if (!inventoryFinal.containsItem(ItemReference.LOVE_LETTER)) {
ctx.sendLocalized("commands.marry.loveletter.no_letter", EmoteReference.SAD);
return Operation.COMPLETED;
}
// Remove the love letter from the inventory.
inventoryFinal.process(new ItemStack(ItemReference.LOVE_LETTER, -1));
playerFinal.save();
// Save the love letter. The content variable is the actual letter, while c is the content of the operation itself.
// Yes it's confusing.
currentMarriageFinal.getData().setLoveLetter(content);
currentMarriageFinal.save();
ctx.sendLocalized("commands.marry.loveletter.confirmed", EmoteReference.CORRECT);
return Operation.COMPLETED;
} else if (c.equalsIgnoreCase("no")) {
ctx.sendLocalized("commands.marry.loveletter.scrapped", EmoteReference.CORRECT);
return Operation.COMPLETED;
}
return Operation.IGNORED;
});
} else {
ctx.sendLocalized("commands.marry.loveletter.no_letter", EmoteReference.SAD);
}
}
});
marryCommand.addSubCommand("house", new SubCommand() {
@Override
public String description() {
return "Buys a house to live in. You need to buy a house in market first. Usage: `~>marry buyhouse <name>`";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var player = ctx.getPlayer();
var playerInventory = player.getInventory();
var dbUser = ctx.getDBUser();
var marriage = dbUser.getData().getMarriage();
if (marriage == null) {
ctx.sendLocalized("commands.marry.buyhouse.not_married", EmoteReference.ERROR);
return;
}
if (!playerInventory.containsItem(ItemReference.HOUSE)) {
ctx.sendLocalized("commands.marry.buyhouse.no_house", EmoteReference.ERROR);
return;
}
if (player.getCurrentMoney() < housePrice) {
ctx.sendLocalized("commands.marry.buyhouse.not_enough_money", EmoteReference.ERROR, housePrice);
return;
}
content = content.replace("\n", "").trim();
if (content.isEmpty()) {
ctx.sendLocalized("commands.marry.buyhouse.no_name", EmoteReference.ERROR);
return;
}
if (content.length() > 150) {
ctx.sendLocalized("commands.pet.buy.too_long", EmoteReference.ERROR);
return;
}
var finalContent = Utils.HTTP_URL.matcher(content).replaceAll("-url-");
ctx.sendLocalized("commands.marry.buyhouse.confirm", EmoteReference.WARNING, housePrice, finalContent);
InteractiveOperations.create(ctx.getChannel(), ctx.getAuthor().getIdLong(), 30, (e) -> {
if (!e.getAuthor().equals(ctx.getAuthor()))
return Operation.IGNORED;
if (e.getMessage().getContentRaw().equalsIgnoreCase("yes")) {
var playerConfirmed = ctx.getPlayer();
var playerInventoryConfirmed = playerConfirmed.getInventory();
var dbUserConfirmed = ctx.getDBUser();
var marriageConfirmed = dbUserConfirmed.getData().getMarriage();
// People like to mess around lol.
if (!playerInventoryConfirmed.containsItem(ItemReference.HOUSE)) {
ctx.sendLocalized("commands.marry.buyhouse.no_house");
return Operation.COMPLETED;
}
if (playerConfirmed.getCurrentMoney() < housePrice) {
ctx.sendLocalized("commands.marry.buyhouse.not_enough_money");
return Operation.COMPLETED;
}
playerInventoryConfirmed.process(new ItemStack(ItemReference.HOUSE, -1));
playerConfirmed.removeMoney(housePrice);
playerConfirmed.save();
marriageConfirmed.getData().setHasHouse(true);
marriageConfirmed.getData().setHouseName(finalContent);
marriageConfirmed.save();
ctx.sendLocalized("commands.marry.buyhouse.success", EmoteReference.POPPER, housePrice, finalContent);
return Operation.COMPLETED;
}
if (e.getMessage().getContentRaw().equalsIgnoreCase("no")) {
ctx.sendLocalized("commands.marry.buyhouse.cancel_success", EmoteReference.CORRECT);
return Operation.COMPLETED;
}
return Operation.IGNORED;
});
}
}).createSubCommandAlias("house", "buyhouse");
marryCommand.addSubCommand("car", new SubCommand() {
@Override
public String description() {
return "Buys a car to travel in. You need to buy a ~~cat~~ car in market first. Usage: `~>marry buycar <name>`";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var player = ctx.getPlayer();
var playerInventory = player.getInventory();
var dbUser = ctx.getDBUser();
var marriage = dbUser.getData().getMarriage();
if (marriage == null) {
ctx.sendLocalized("commands.marry.general.not_married", EmoteReference.ERROR);
return;
}
if (!playerInventory.containsItem(ItemReference.CAR)) {
ctx.sendLocalized("commands.marry.buycar.no_car", EmoteReference.ERROR);
return;
}
if (player.getCurrentMoney() < carPrice) {
ctx.sendLocalized("commands.marry.buycar.not_enough_money", EmoteReference.ERROR, carPrice);
return;
}
if (content.isEmpty()) {
ctx.sendLocalized("commands.marry.buycar.no_name", EmoteReference.ERROR);
return;
}
content = content.replace("\n", "").trim();
if (content.length() > 150) {
ctx.sendLocalized("commands.pet.buy.too_long", EmoteReference.ERROR);
return;
}
var finalContent = Utils.HTTP_URL.matcher(content).replaceAll("-url-");
ctx.sendLocalized("commands.marry.buycar.confirm", EmoteReference.WARNING, carPrice, content);
InteractiveOperations.create(ctx.getChannel(), ctx.getAuthor().getIdLong(), 30, (e) -> {
if (!e.getAuthor().equals(ctx.getAuthor()))
return Operation.IGNORED;
if (e.getMessage().getContentRaw().equalsIgnoreCase("yes")) {
var playerConfirmed = ctx.getPlayer();
var playerInventoryConfirmed = playerConfirmed.getInventory();
var dbUserConfirmed = ctx.getDBUser();
var marriageConfirmed = dbUserConfirmed.getData().getMarriage();
// People like to mess around lol.
if (!playerInventoryConfirmed.containsItem(ItemReference.CAR)) {
ctx.sendLocalized("commands.marry.buycar.no_car");
return Operation.COMPLETED;
}
if (playerConfirmed.getCurrentMoney() < carPrice) {
ctx.sendLocalized("commands.marry.buycar.not_enough_money");
return Operation.COMPLETED;
}
playerInventoryConfirmed.process(new ItemStack(ItemReference.CAR, -1));
playerConfirmed.removeMoney(carPrice);
playerConfirmed.save();
marriageConfirmed.getData().setHasCar(true);
marriageConfirmed.getData().setCarName(finalContent);
marriageConfirmed.save();
ctx.sendLocalized("commands.marry.buycar.success", EmoteReference.POPPER, carPrice, finalContent);
return Operation.COMPLETED;
}
if (e.getMessage().getContentRaw().equalsIgnoreCase("no")) {
ctx.sendLocalized("commands.marry.buycar.cancel_success", EmoteReference.CORRECT);
return Operation.COMPLETED;
}
return Operation.IGNORED;
});
}
}).createSubCommandAlias("car", "buycar");
IncreasingRateLimiter tzRatelimit = new IncreasingRateLimiter.Builder().limit(1).spamTolerance(2).cooldown(2, TimeUnit.DAYS).maxCooldown(2, TimeUnit.DAYS).randomIncrement(false).premiumAware(false).pool(MantaroData.getDefaultJedisPool()).prefix("marriage-tz").build();
marryCommand.addSubCommand("timezone", new SubCommand() {
@Override
public String description() {
return "Sets the timezone for your marriage. Useful for pet sleep times.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
var dbUser = ctx.getDBUser();
var marriage = dbUser.getData().getMarriage();
if (content.isEmpty()) {
ctx.sendLocalized("commands.marry.timezone.no_content", EmoteReference.ERROR);
return;
}
if (marriage == null) {
ctx.sendLocalized("commands.marry.general.not_married", EmoteReference.ERROR);
return;
}
String timezone = content;
if (// Avoid replacing valid zone IDs / uppercasing them.
offsetRegex.matcher(timezone).matches())
timezone = content.toUpperCase().replace("UTC", "GMT");
if (!Utils.isValidTimeZone(timezone)) {
ctx.sendLocalized("commands.marry.timezone.invalid", EmoteReference.ERROR);
return;
}
if (!RatelimitUtils.ratelimit(tzRatelimit, ctx)) {
return;
}
marriage.getData().setTimezone(timezone);
marriage.save();
dbUser.save();
ctx.sendLocalized("commands.marry.timezone.success", EmoteReference.CORRECT, timezone);
}
});
marryCommand.addSubCommand("status", new SubCommand() {
@Override
public String description() {
return "Check your marriage status.";
}
@Override
protected void call(Context ctx, I18nContext languageContext, String content) {
if (!ctx.getSelfMember().hasPermission(ctx.getChannel(), Permission.MESSAGE_EMBED_LINKS)) {
ctx.sendLocalized("general.missing_embed_permissions");
return;
}
final var author = ctx.getAuthor();
final var dbUser = ctx.getDBUser();
final var dbUserData = dbUser.getData();
final var currentMarriage = dbUserData.getMarriage();
// What status would we have without marriage? Well, we can be unmarried omegalul.
if (currentMarriage == null) {
ctx.sendLocalized("commands.marry.status.no_marriage", EmoteReference.SAD);
return;
}
final var data = currentMarriage.getData();
// Can we find the user this is married to?
final var marriedTo = ctx.retrieveUserById(currentMarriage.getOtherPlayer(author.getId()));
if (marriedTo == null) {
ctx.sendLocalized("commands.marry.loveletter.cannot_see", EmoteReference.ERROR);
return;
}
// Get the current love letter.
var loveLetter = data.getLoveLetter();
if (loveLetter == null || loveLetter.isEmpty()) {
loveLetter = languageContext.get("general.none");
}
final var marriedDBUser = ctx.getDBUser(marriedTo);
final var dateFormat = Utils.formatDate(data.getMarriageCreationMillis(), dbUserData.getLang());
final var eitherHasWaifus = !(dbUserData.getWaifus().isEmpty() && marriedDBUser.getData().getWaifus().isEmpty());
final var marriedToName = dbUserData.isPrivateTag() ? marriedTo.getName() : marriedTo.getAsTag();
final var authorName = dbUserData.isPrivateTag() ? author.getName() : author.getAsTag();
final var daysMarried = TimeUnit.of(ChronoUnit.MILLIS).toDays(System.currentTimeMillis() - data.getMarriageCreationMillis());
EmbedBuilder embedBuilder = new EmbedBuilder().setThumbnail(author.getEffectiveAvatarUrl()).setAuthor(languageContext.get("commands.marry.status.header"), null, author.getEffectiveAvatarUrl()).setColor(ctx.getMemberColor()).setDescription(languageContext.get("commands.marry.status.description_format").formatted(EmoteReference.HEART, authorName, marriedToName)).addField(EmoteReference.CALENDAR2.toHeaderString() + languageContext.get("commands.marry.status.date"), dateFormat, false).addField(EmoteReference.CLOCK.toHeaderString() + languageContext.get("commands.marry.status.age"), daysMarried + " " + languageContext.get("general.days"), false).addField(EmoteReference.LOVE_LETTER.toHeaderString() + languageContext.get("commands.marry.status.love_letter"), loveLetter, false).addField(EmoteReference.ZAP.toHeaderString() + languageContext.get("commands.marry.status.waifus"), String.valueOf(eitherHasWaifus), false).setFooter("Marriage ID: " + currentMarriage.getId(), author.getEffectiveAvatarUrl());
if (data.hasHouse()) {
var houseName = data.getHouseName().replace("\n", "").trim();
embedBuilder.addField(EmoteReference.HOUSE.toHeaderString() + languageContext.get("commands.marry.status.house"), houseName, true);
}
if (data.hasCar()) {
var carName = data.getCarName().replace("\n", "").trim();
embedBuilder.addField(EmoteReference.CAR.toHeaderString() + languageContext.get("commands.marry.status.car"), carName, true);
}
if (data.getPet() != null) {
var pet = data.getPet();
var petType = data.getPet().getType();
embedBuilder.addField(EmoteReference.PET_HOUSE.toHeaderString() + languageContext.get("commands.marry.status.pet"), pet.getName() + " (" + petType.getName() + ")", false);
}
ctx.send(embedBuilder.build());
}
}).createSubCommandAlias("status", "stats");
cr.registerAlias("marry", "marriage");
}
Aggregations