use of com.sx4.bot.core.Sx4CommandEvent 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) BaseGuildMessageChannel 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<BaseGuildMessageChannel> 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 -> {
BaseGuildMessageChannel messageChannel = SearchUtility.getBaseMessageChannel(event.getGuild(), e.getMessage().getContentRaw());
if (messageChannel != null) {
atomicChannel.set(messageChannel);
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;
}
BaseGuildMessageChannel 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();
});
});
});
}
use of com.sx4.bot.core.Sx4CommandEvent in project Sx4 by sx4-discord-bot.
the class GiveawayCommand method end.
@Command(value = "end", description = "Ends an active giveaway early")
@CommandId(50)
@Examples({ "giveaway end 727224132202397726" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void end(Sx4CommandEvent event, @Argument(value = "message id") MessageArgument messageArgument) {
Document data = event.getMongo().getGiveawayById(messageArgument.getMessageId());
if (data == null) {
event.replyFailure("There is no giveaway with that id").queue();
return;
}
if (data.containsKey("winners")) {
event.replyFailure("That giveaway has already ended").queue();
return;
}
event.getBot().getGiveawayManager().endGiveaway(data, true);
}
use of com.sx4.bot.core.Sx4CommandEvent in project Sx4 by sx4-discord-bot.
the class LeaverCommand method toggle.
@Command(value = "toggle", description = "Toggle the state of leaver")
@CommandId(189)
@Examples({ "leaver toggle" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void toggle(Sx4CommandEvent event) {
List<Bson> update = List.of(Operators.set("leaver.enabled", Operators.cond("$leaver.enabled", Operators.REMOVE, true)));
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("leaver.enabled")).upsert(true);
event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.replySuccess("Leaver is now " + (data.getEmbedded(List.of("leaver", "enabled"), false) ? "enabled" : "disabled")).queue();
});
}
use of com.sx4.bot.core.Sx4CommandEvent in project Sx4 by sx4-discord-bot.
the class LeaverCommand method preview.
@Command(value = "preview", description = "Preview your leaver message")
@CommandId(195)
@Examples({ "leaver preview" })
public void preview(Sx4CommandEvent event) {
Document data = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("leaver.message", "leaver.enabled"));
Document leaver = data.get("leaver", MongoDatabase.EMPTY_DOCUMENT);
if (!leaver.get("enabled", false)) {
event.replyFailure("Leaver is not enabled").queue();
return;
}
WebhookMessageBuilder builder;
try {
builder = LeaverUtility.getLeaverMessage(leaver.get("message", LeaverManager.DEFAULT_MESSAGE), event.getMember());
} catch (IllegalArgumentException e) {
event.replyFailure(e.getMessage()).queue();
return;
}
MessageUtility.fromWebhookMessage(event.getChannel(), builder.build()).queue();
}
use of com.sx4.bot.core.Sx4CommandEvent in project Sx4 by sx4-discord-bot.
the class LoggerCommand method name.
@Command(value = "name", description = "Set the name of the webhook that sends logs")
@CommandId(423)
@Examples({ "logger name #logs Logs", "logger name Logger" })
@Premium
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void name(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) BaseGuildMessageChannel channel, @Argument(value = "name", endless = true) String name) {
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;
event.getMongo().updateLogger(Filters.eq("channelId", effectiveChannel.getIdLong()), Updates.set("webhook.name", name)).whenComplete((result, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (result.getModifiedCount() == 0) {
event.replyFailure("Your webhook name for that logger was already set to that").queue();
return;
}
event.replySuccess("Your webhook name has been updated for that logger, this only works with premium <https://patreon.com/Sx4>").queue();
});
}
Aggregations