use of com.sx4.bot.annotations.command.Premium in project Sx4 by sx4-discord-bot.
the class TwitchNotificationCommand method toggle.
@Command(value = "toggle", description = "Enables/disables a specific Twitch notification")
@CommandId(496)
@Command.Cooldown(5)
@Examples({ "twitch notification toggle 5e45ce6d3688b30ee75201ae" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void toggle(Sx4CommandEvent event, @Argument(value = "id") ObjectId id) {
List<Bson> guildPipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))), Projections.computed("guildId", "$_id"))), Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())));
List<Bson> countPipeline = List.of(Aggregates.match(Operators.expr(Operators.and(Operators.and(Operators.eq("$streamerId", "$$notification.streamerId"), Operators.extinct("$enabled"))))), Aggregates.limit(2), Aggregates.group(null, Accumulators.sum("streamerCount", 1)));
List<Bson> pipeline = List.of(Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())), Aggregates.project(Projections.include("streamerId", "enabled")), Aggregates.group(null, Accumulators.push("notifications", Operators.ROOT)), Aggregates.unionWith("guilds", guildPipeline), AggregateOperators.mergeFields("premium", "notifications"), Aggregates.project(Projections.fields(Projections.include("premium", "notifications"), Projections.computed("notification", Operators.first(Operators.filter(Operators.ifNull("$notifications", Collections.EMPTY_LIST), Operators.eq("$$this._id", id)))))), Aggregates.lookup("twitchNotifications", List.of(new Variable<>("notification", "$notification")), countPipeline, "streamerCount"), Aggregates.project(Projections.fields(Projections.computed("streamerId", "$notification.streamerId"), Projections.computed("streamerCount", Operators.cond(Operators.isEmpty("$streamerCount"), 0, Operators.get(Operators.arrayElemAt("$streamerCount", 0), "streamerCount"))), Projections.computed("premium", Operators.ifNull("$premium", false)), Projections.computed("count", Operators.size(Operators.ifNull(Operators.filter("$notifications", Operators.extinct("$$this.enabled")), Collections.EMPTY_LIST))), Projections.computed("disabled", Operators.not(Operators.ifNull("$notification.enabled", true))))));
AtomicInteger subscribe = new AtomicInteger();
AtomicReference<String> streamerId = new AtomicReference<>();
event.getMongo().aggregateTwitchNotifications(pipeline).thenCompose(documents -> {
Document data = documents.isEmpty() ? null : documents.get(0);
if (data == null || !data.containsKey("streamerId")) {
throw new IllegalArgumentException("There is not a twitch notification with that id");
}
boolean disabled = data.getBoolean("disabled");
int count = data.getInteger("count");
if (disabled && count >= 3 && !data.getBoolean("premium")) {
throw new IllegalArgumentException("You need to have Sx4 premium to have more than 3 enabled twitch notifications, you can get premium at <https://www.patreon.com/Sx4>");
}
if (count >= 10) {
throw new IllegalArgumentException("You can not have any more than 10 enabled twitch notifications");
}
int streamerCount = data.getInteger("streamerCount", -1);
if (disabled && streamerCount == 0) {
subscribe.set(1);
} else if (!disabled && streamerCount == 1) {
subscribe.set(2);
}
streamerId.set(data.getString("streamerId"));
List<Bson> update = List.of(Operators.set("enabled", Operators.cond(Operators.exists("$enabled"), Operators.REMOVE, false)));
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("enabled"));
return event.getMongo().findAndUpdateTwitchNotification(Filters.eq("_id", id), update, options);
}).whenComplete((data, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof IllegalArgumentException) {
event.replyFailure(cause.getMessage()).queue();
return;
}
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (data == null) {
event.replyFailure("There is not a twitch notification with that id").queue();
return;
}
int value = subscribe.get();
if (value == 1) {
event.getBot().getTwitchManager().subscribe(streamerId.get());
} else if (value == 2) {
event.getBot().getTwitchManager().unsubscribe(streamerId.get());
}
event.replySuccess("That twitch notification is now **" + (data.get("enabled", true) ? "enabled" : "disabled") + "**").queue();
});
}
use of com.sx4.bot.annotations.command.Premium in project Sx4 by sx4-discord-bot.
the class SuggestionCommand method add.
@Command(value = "add", description = "Sends a suggestion to the suggestion channel if one is setup in the server")
@CommandId(84)
@Redirects({ "suggest" })
@Examples({ "suggestion add Add the dog emote", "suggestion Add a channel for people looking to play games" })
@BotPermissions(permissions = { Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS })
public void add(Sx4CommandEvent event, @Argument(value = "suggestion", endless = true) String description) {
Document data = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("suggestion.channelId", "suggestion.enabled", "suggestion.webhook", "premium.endAt"));
Document suggestionData = data.get("suggestion", MongoDatabase.EMPTY_DOCUMENT);
if (!suggestionData.getBoolean("enabled", false)) {
event.replyFailure("Suggestions are not enabled in this server").queue();
return;
}
long channelId = suggestionData.get("channelId", 0L);
if (channelId == 0L) {
event.replyFailure("There is no suggestion channel").queue();
return;
}
BaseGuildMessageChannel channel = event.getGuild().getChannelById(BaseGuildMessageChannel.class, channelId);
if (channel == null) {
event.replyFailure("The suggestion channel no longer exists").queue();
return;
}
SuggestionState state = SuggestionState.PENDING;
String image = event.getMessage().getAttachments().stream().filter(Message.Attachment::isImage).map(Message.Attachment::getUrl).findFirst().orElse(null);
Suggestion suggestion = new Suggestion(channelId, event.getGuild().getIdLong(), event.getAuthor().getIdLong(), description, image, state.getDataName());
boolean premium = Clock.systemUTC().instant().getEpochSecond() < data.getEmbedded(List.of("premium", "endAt"), 0L);
event.getBot().getSuggestionManager().sendSuggestion(channel, suggestionData.get("webhook", MongoDatabase.EMPTY_DOCUMENT), premium, suggestion.getWebhookEmbed(null, event.getAuthor(), state)).whenComplete((message, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
suggestion.setMessageId(message.getId());
event.getMongo().insertSuggestion(suggestion.toData()).whenComplete((result, dataException) -> {
if (ExceptionUtility.sendExceptionally(event, dataException)) {
return;
}
channel.addReactionById(message.getId(), "✅").flatMap($ -> channel.addReactionById(message.getId(), "❌")).queue();
event.replySuccess("Your suggestion has been sent to " + channel.getAsMention()).queue();
});
});
}
use of com.sx4.bot.annotations.command.Premium in project Sx4 by sx4-discord-bot.
the class WelcomerCommand method preview.
@Command(value = "preview", description = "Preview your welcomer message")
@CommandId(99)
@Examples({ "welcomer preview" })
public void preview(Sx4CommandEvent event) {
Document data = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("welcomer.message", "welcomer.image", "welcomer.enabled", "premium.endAt"));
Document welcomer = data.get("welcomer", MongoDatabase.EMPTY_DOCUMENT);
Document image = welcomer.get("image", MongoDatabase.EMPTY_DOCUMENT);
boolean messageEnabled = welcomer.get("enabled", false), imageEnabled = image.get("enabled", false);
if (!messageEnabled && !imageEnabled) {
event.replyFailure("Neither welcomer or image welcomer is enabled").queue();
return;
}
boolean gif = data.getEmbedded(List.of("premium", "endAt"), 0L) >= Clock.systemUTC().instant().getEpochSecond();
WelcomerUtility.getWelcomerMessage(event.getHttpClient(), messageEnabled ? welcomer.get("message", WelcomerManager.DEFAULT_MESSAGE) : null, image.getString("bannerId"), event.getMember(), event.getConfig().isCanary(), imageEnabled, gif, (builder, exception) -> {
if (exception instanceof IllegalArgumentException) {
event.replyFailure(exception.getMessage()).queue();
return;
}
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
MessageUtility.fromWebhookMessage(event.getChannel(), builder.build()).queue();
});
}
use of com.sx4.bot.annotations.command.Premium in project Sx4 by sx4-discord-bot.
the class LoggerCommand method toggle.
@Command(value = "toggle", aliases = { "enable", "disable" }, description = "Toggles the state of a logger")
@CommandId(56)
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
@Examples({ "logger toggle #logs", "logger toggle" })
public void toggle(Sx4CommandEvent event, @Argument(value = "channel", endless = true, nullDefault = true) BaseGuildMessageChannel channel) {
MessageChannel messageChannel = event.getChannel();
if (channel == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
event.replyFailure("You cannot use this channel type").queue();
return;
}
BaseGuildMessageChannel effectiveChannel = channel == null ? (BaseGuildMessageChannel) messageChannel : channel;
List<Bson> guildPipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))), Projections.computed("guildId", "$_id"))), Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())));
List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("guildId", event.getGuild().getIdLong()), Filters.exists("enabled", false))), Aggregates.project(Projections.include("channelId")), Aggregates.group(null, Accumulators.push("loggers", Operators.ROOT)), Aggregates.unionWith("guilds", guildPipeline), Aggregates.group(null, Accumulators.max("loggers", "$loggers"), Accumulators.max("premium", "$premium")), Aggregates.project(Projections.fields(Projections.computed("premium", Operators.ifNull("$premium", false)), Projections.computed("count", Operators.size(Operators.ifNull("$loggers", Collections.EMPTY_LIST))), Projections.computed("disabled", Operators.isEmpty(Operators.filter(Operators.ifNull("$loggers", Collections.EMPTY_LIST), Operators.eq("$$this.channelId", effectiveChannel.getIdLong())))))));
event.getMongo().aggregateLoggers(pipeline).thenCompose(documents -> {
Document data = documents.isEmpty() ? null : documents.get(0);
boolean disabled = data == null || data.getBoolean("disabled");
int count = data == null ? 0 : data.getInteger("count");
if (data != null && disabled && count >= 3 && !data.getBoolean("premium")) {
throw new IllegalArgumentException("You need to have Sx4 premium to have more than 3 enabled loggers, you can get premium at <https://www.patreon.com/Sx4>");
}
if (count >= 25) {
throw new IllegalArgumentException("You can not have any more than 25 enabled loggers");
}
List<Bson> update = List.of(Operators.set("enabled", Operators.cond(Operators.exists("$enabled"), Operators.REMOVE, false)));
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("enabled"));
return event.getMongo().findAndUpdateLogger(Filters.eq("channelId", effectiveChannel.getIdLong()), update, options);
}).whenComplete((data, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof IllegalArgumentException) {
event.replyFailure(cause.getMessage()).queue();
return;
}
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (data == null) {
event.replyFailure("There is not a logger in that channel").queue();
return;
}
event.replySuccess("The logger in " + effectiveChannel.getAsMention() + " is now **" + (data.get("enabled", true) ? "enabled" : "disabled") + "**").queue();
});
}
use of com.sx4.bot.annotations.command.Premium in project Sx4 by sx4-discord-bot.
the class LoggerCommand method add.
@Command(value = "add", description = "Adds a logger to a certain channel")
@CommandId(54)
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
@Examples({ "logger add #logs", "logger add" })
public void add(Sx4CommandEvent event, @Argument(value = "channel", endless = true, nullDefault = true) BaseGuildMessageChannel channel) {
MessageChannel messageChannel = event.getChannel();
if (channel == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
event.replyFailure("You cannot use this channel type").queue();
return;
}
BaseGuildMessageChannel effectiveChannel = channel == null ? (BaseGuildMessageChannel) messageChannel : channel;
List<Bson> guildPipeline = List.of(Aggregates.project(Projections.fields(Projections.computed("premium", Operators.lt(Operators.nowEpochSecond(), Operators.ifNull("$premium.endAt", 0L))), Projections.computed("guildId", "$_id"))), Aggregates.match(Filters.eq("guildId", event.getGuild().getIdLong())));
List<Bson> pipeline = List.of(Aggregates.match(Filters.and(Filters.eq("guildId", event.getGuild().getIdLong()), Filters.exists("enabled", false))), Aggregates.limit(25), Aggregates.group(null, Accumulators.sum("count", 1)), Aggregates.unionWith("guilds", guildPipeline), Aggregates.group(null, Accumulators.max("count", "$count"), Accumulators.max("premium", "$premium")), Aggregates.project(Projections.fields(Projections.computed("premium", Operators.ifNull("$premium", false)), Projections.computed("count", Operators.ifNull("$count", 0)))));
event.getMongo().aggregateLoggers(pipeline).thenCompose(documents -> {
Document counter = documents.isEmpty() ? null : documents.get(0);
int count = counter == null ? 0 : counter.getInteger("count");
if (counter != null && count >= 3 && !counter.getBoolean("premium")) {
event.replyFailure("You need to have Sx4 premium to have more than 3 enabled loggers, you can get premium at <https://www.patreon.com/Sx4>").queue();
return CompletableFuture.completedFuture(null);
}
if (count == 25) {
event.replyFailure("You can not have any more than 25 loggers").queue();
return CompletableFuture.completedFuture(null);
}
Document data = new Document("channelId", effectiveChannel.getIdLong()).append("guildId", event.getGuild().getIdLong());
return event.getMongo().insertLogger(data);
}).whenComplete((result, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof MongoWriteException && ((MongoWriteException) cause).getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
event.replyFailure("You already have a logger setup in " + effectiveChannel.getAsMention()).queue();
return;
}
if (ExceptionUtility.sendExceptionally(event, exception) || result == null) {
return;
}
event.replySuccess("You now have a logger setup in " + effectiveChannel.getAsMention()).queue();
});
}
Aggregations