use of com.sx4.bot.entities.argument.MessageArgument in project Sx4 by sx4-discord-bot.
the class SuggestionCommand method view.
@Command(value = "view", aliases = { "list" }, description = "View a suggestion in the current channel")
@CommandId(87)
@Examples({ "suggestion view 5e45ce6d3688b30ee75201ae", "suggestion view" })
public void view(Sx4CommandEvent event, @Argument(value = "id | message", nullDefault = true, acceptEmpty = true) Or<MessageArgument, ObjectId> argument) {
Bson filter = Filters.eq("guildId", event.getGuild().getIdLong());
if (argument != null) {
filter = Filters.and(filter, argument.hasFirst() ? Filters.eq("messageId", argument.getFirst().getMessageId()) : Filters.eq("_id", argument.getSecond()));
}
List<Bson> guildPipeline = List.of(Aggregates.project(Projections.computed("states", Operators.ifNull("$suggestion.states", SuggestionState.getDefaultStates()))), Aggregates.match(Filters.eq("_id", event.getGuild().getIdLong())));
List<Bson> pipeline = List.of(Aggregates.match(filter), Aggregates.group(null, Accumulators.push("suggestions", Operators.ROOT)), Aggregates.unionWith("guilds", guildPipeline), Aggregates.group(null, Accumulators.max("states", "$states"), Accumulators.max("suggestions", Operators.ifNull("$suggestions", Collections.EMPTY_LIST))));
event.getMongo().aggregateSuggestions(pipeline).whenComplete((documents, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
Document data = documents.isEmpty() ? MongoDatabase.EMPTY_DOCUMENT : documents.get(0);
List<Document> suggestions = data.getList("suggestions", Document.class);
if (suggestions.isEmpty()) {
event.replyFailure("There are no suggestions in this server").queue();
return;
}
List<Document> states = data.getList("states", Document.class);
PagedResult<Document> paged = new PagedResult<>(event.getBot(), suggestions).setIncreasedIndex(true).setDisplayFunction(d -> {
long authorId = d.getLong("authorId");
User author = event.getShardManager().getUserById(authorId);
return String.format("`%s` by %s - **%s**", d.getObjectId("_id").toHexString(), author == null ? authorId : author.getAsTag(), d.getString("state"));
});
paged.onSelect(select -> {
Suggestion suggestion = Suggestion.fromData(select.getSelected());
event.reply(suggestion.getEmbed(event.getShardManager(), suggestion.getFullState(states))).queue();
});
paged.execute(event);
});
}
use of com.sx4.bot.entities.argument.MessageArgument in project Sx4 by sx4-discord-bot.
the class SuggestionCommand method remove.
@Command(value = "remove", aliases = { "delete" }, description = "Removes a suggestion, can be your own or anyones if you have the manage server permission")
@CommandId(85)
@Examples({ "suggestion remove 5e45ce6d3688b30ee75201ae", "suggestion remove all" })
public void remove(Sx4CommandEvent event, @Argument(value = "id | message | all", acceptEmpty = true) @AlternativeOptions("all") Alternative<Or<MessageArgument, ObjectId>> option) {
User author = event.getAuthor();
if (option.isAlternative()) {
if (!event.hasPermission(event.getMember(), Permission.MANAGE_SERVER)) {
event.replyFailure("You are missing the permission " + Permission.MANAGE_SERVER.getName() + " to execute this, you can remove your own suggestions only").queue();
return;
}
List<Button> buttons = List.of(Button.success("yes", "Yes"), Button.danger("no", "No"));
event.reply(author.getName() + ", are you sure you want to delete **all** the suggestions in this server?").setActionRow(buttons).submit().thenCompose(message -> {
return new Waiter<>(event.getBot(), ButtonClickEvent.class).setPredicate(e -> ButtonUtility.handleButtonConfirmation(e, message, event.getAuthor())).setCancelPredicate(e -> ButtonUtility.handleButtonCancellation(e, message, event.getAuthor())).onFailure(e -> ButtonUtility.handleButtonFailure(e, message)).setTimeout(60).start();
}).whenComplete((e, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof CancelException) {
GenericEvent cancelEvent = ((CancelException) cause).getEvent();
if (cancelEvent != null) {
((ButtonClickEvent) cancelEvent).reply("Cancelled " + event.getConfig().getSuccessEmote()).queue();
}
return;
} else if (cause instanceof TimeoutException) {
event.reply("Timed out :stopwatch:").queue();
return;
} else if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.getMongo().deleteManySuggestions(Filters.eq("guildId", event.getGuild().getIdLong())).whenComplete((result, databaseException) -> {
if (ExceptionUtility.sendExceptionally(event, databaseException)) {
return;
}
if (result.getDeletedCount() == 0) {
e.reply("This server has no suggestions " + event.getConfig().getFailureEmote()).queue();
return;
}
e.reply("All suggestions have been deleted in this server " + event.getConfig().getSuccessEmote()).queue();
});
});
} else {
Or<MessageArgument, ObjectId> argument = option.getValue();
boolean hasPermission = event.hasPermission(event.getMember(), Permission.MANAGE_SERVER);
Bson filter = Filters.and(argument.hasFirst() ? Filters.eq("messageId", argument.getFirst().getMessageId()) : Filters.eq("_id", argument.getSecond()), Filters.eq("guildId", event.getGuild().getIdLong()));
if (!hasPermission) {
filter = Filters.and(Filters.eq("authorId", author.getIdLong()), filter);
}
event.getMongo().findAndDeleteSuggestion(filter).whenComplete((data, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
if (data == null) {
event.replyFailure("I could not find that suggestion").queue();
return;
}
if (data.getLong("authorId") != author.getIdLong() && !hasPermission) {
event.replyFailure("You do not own that suggestion").queue();
return;
}
WebhookClient webhook = event.getBot().getSuggestionManager().getWebhook(data.getLong("channelId"));
if (webhook != null) {
webhook.delete(data.getLong("messageId"));
}
event.replySuccess("That suggestion has been removed").queue();
});
}
}
use of com.sx4.bot.entities.argument.MessageArgument in project Sx4 by sx4-discord-bot.
the class GiveawayCommand method reroll.
@Command(value = "reroll", aliases = { "re roll" }, description = "Rerolls the winners for an ended giveaway")
@CommandId(49)
@Examples({ "giveaway reroll 727224132202397726" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void reroll(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 not ended yet").queue();
return;
}
event.getBot().getGiveawayManager().endGiveaway(data, true);
}
use of com.sx4.bot.entities.argument.MessageArgument 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.entities.argument.MessageArgument in project Sx4 by sx4-discord-bot.
the class DiscordCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user", nullDefault = true) Member member, @Argument(value = "text | message id", endless = true) @Limit(max = 250) Or<MessageArgument, String> option, @Option(value = "light", description = "Sets the discord theme to light") boolean light) {
if (member == null && option.hasSecond()) {
event.replyFailure("You need to provide a user when not giving a message").queue();
return;
}
this.getContext(option, member).thenAccept(pair -> {
Member effectiveMember = pair.getLeft();
User user = effectiveMember.getUser();
String text = pair.getRight();
Request request = new ImageRequest(event.getConfig().getImageWebserverUrl("discord")).addField("name", effectiveMember.getEffectiveName()).addField("avatar", user.getEffectiveAvatarUrl()).addField("bot", user.isBot()).addField("dark_theme", !light).addField("colour", effectiveMember.getColorRaw()).addField("text", text).addAllFields(this.getMentions(event.getShardManager(), event.getGuild(), text)).build(event.getConfig().getImageWebserver());
event.getHttpClient().newCall(request).enqueue((HttpCallback) response -> ImageUtility.getImageMessage(event, response).queue());
});
}
Aggregations