use of com.sx4.bot.annotations.command.CooldownMessage in project Sx4 by sx4-discord-bot.
the class MarriageCommand method add.
@Command(value = "add", description = "Propose to a user and marry them if they accept")
@CommandId(268)
@Redirects({ "marry" })
@Cooldown(60)
@CooldownMessage("You already have a pending marriage :no_entry:")
@Examples({ "marriage add @Shea#6653", "marriage add Shea", "marriage add 402557516728369153" })
public void add(Sx4CommandEvent event, @Argument(value = "user", endless = true) Member member) {
User author = event.getAuthor();
if (member.getUser().isBot()) {
event.replyFailure("You cannot marry bots").queue();
return;
}
Bson checkFilter = Filters.or(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", author.getIdLong()), Filters.eq("proposerId", member.getIdLong()), Filters.eq("partnerId", member.getIdLong()));
List<Document> marriages = event.getMongo().getMarriages(checkFilter, Projections.include("partnerId", "proposerId")).into(new ArrayList<>());
long userCount = marriages.stream().filter(d -> d.getLong("proposerId") == author.getIdLong() || d.getLong("partnerId") == author.getIdLong()).count();
if (userCount >= 5) {
event.removeCooldown();
event.replyFailure("You cannot marry more than 5 users").queue();
return;
}
long memberCount = marriages.stream().filter(d -> d.getLong("proposerId") == member.getIdLong() || d.getLong("partnerId") == member.getIdLong()).count();
if (memberCount >= 5) {
event.removeCooldown();
event.replyFailure("That user is already married to 5 users").queue();
return;
}
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply(member.getAsMention() + ", **" + author.getName() + "** would like to marry you! Do you accept?").allowedMentions(EnumSet.of(Message.MentionType.USER)).setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, member.getUser())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, member.getUser())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
event.removeCooldown();
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Better luck next time " + author.getName() + " :broken_heart:").queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
Bson filter = Filters.or(Filters.and(Filters.eq("proposerId", member.getIdLong()), Filters.eq("partnerId", author.getIdLong())), Filters.and(Filters.eq("proposerId", author.getIdLong()), Filters.eq("partnerId", member.getIdLong())));
event.getMongo().updateMarriage(filter, Updates.combine(Updates.setOnInsert("proposerId", author.getIdLong()), Updates.setOnInsert("partnerId", member.getIdLong()))).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException)) {
return;
}
if (result.getMatchedCount() != 0) {
e.reply("You're already married to that user " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("You're now married to " + member.getAsMention() + " :tada: :heart:").queue();
});
});
}
Aggregations