use of com.sx4.bot.entities.argument.Or 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.Or 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.Or in project Sx4 by sx4-discord-bot.
the class WelcomerCommand method screening.
@Command(value = "screening", aliases = { "member screening" }, description = "Toggles whether the bot should send a welcomer message after or before a member has gone through member screening")
@CommandId(461)
@Examples({ "welcomer screening" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void screening(Sx4CommandEvent event) {
List<Bson> update = List.of(Operators.set("welcomer.screening", Operators.cond(Operators.exists("$welcomer.screening"), Operators.REMOVE, false)));
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("welcomer.screening")).upsert(true);
event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
if (ExceptionUtility.sendExceptionally(event, exception)) {
return;
}
event.replySuccess("Welcomer will now send a message " + (data.getEmbedded(List.of("welcomer", "screening"), true) ? "after" : "before") + " member screening").queue();
});
}
use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class HelpCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "command | module", endless = true, nullDefault = true) String commandName) {
boolean embed = !event.isFromGuild() || event.getGuild().getSelfMember().hasPermission(event.getTextChannel(), Permission.MESSAGE_EMBED_LINKS);
if (commandName == null) {
List<Sx4Category> categories = Arrays.stream(ModuleCategory.ALL_ARRAY).filter(category -> !category.getCommands(event.isAuthorDeveloper()).isEmpty()).collect(Collectors.toList());
PagedResult<Sx4Category> paged = new PagedResult<>(event.getBot(), categories).setPerPage(categories.size()).setSelect(SelectType.OBJECT).setSelectablePredicate((content, category) -> category.getName().equalsIgnoreCase(content) || Arrays.stream(category.getAliases()).anyMatch(content::equalsIgnoreCase)).setCustomFunction(page -> {
MessageBuilder builder = new MessageBuilder();
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor("Help", null, event.getSelfUser().getEffectiveAvatarUrl());
embedBuilder.setFooter(event.getPrefix() + "help <module> or respond below with a name of a module", event.getAuthor().getEffectiveAvatarUrl());
embedBuilder.setDescription("All commands are put in a set category also known as a module, use `" + event.getPrefix() + "help <module>` on the module of your choice, The bot will then " + "list all the commands in that module. If you need further help feel free to join the [support server](https://discord.gg/PqJNcfB).");
embedBuilder.addField("Modules", "`" + categories.stream().map(Sx4Category::getName).collect(Collectors.joining("`, `")) + "`", false);
return builder.setEmbeds(embedBuilder.build());
});
paged.onSelect(select -> {
Sx4Category category = select.getSelected();
List<Sx4Command> categoryCommands = category.getCommands(event.isAuthorDeveloper()).stream().map(Sx4Command.class::cast).sorted(Comparator.comparing(Sx4Command::getCommandTrigger)).collect(Collectors.toList());
PagedResult<Sx4Command> categoryPaged = HelpUtility.getCommandsPaged(event.getBot(), categoryCommands).setAuthor(category.getName(), null, event.getAuthor().getEffectiveAvatarUrl());
categoryPaged.onSelect(categorySelect -> event.reply(HelpUtility.getHelpMessage(categorySelect.getSelected(), embed)).queue());
categoryPaged.execute(event);
});
paged.execute(event);
} else {
Sx4Category category = SearchUtility.getModule(commandName);
List<Sx4Command> commands = SearchUtility.getCommands(event.getCommandListener(), commandName, event.isAuthorDeveloper());
if (category != null) {
List<Sx4Command> categoryCommands = category.getCommands(event.isAuthorDeveloper()).stream().map(Sx4Command.class::cast).sorted(Comparator.comparing(Sx4Command::getCommandTrigger)).collect(Collectors.toList());
PagedResult<Sx4Command> paged = HelpUtility.getCommandsPaged(event.getBot(), categoryCommands).setAuthor(category.getName(), null, event.getAuthor().getEffectiveAvatarUrl());
paged.onSelect(select -> event.reply(HelpUtility.getHelpMessage(select.getSelected(), embed)).queue());
paged.execute(event);
} else if (!commands.isEmpty()) {
PagedResult<Sx4Command> paged = new PagedResult<>(event.getBot(), commands).setAuthor(commandName, null, event.getAuthor().getEffectiveAvatarUrl()).setAutoSelect(true).setPerPage(15).setSelectablePredicate((content, command) -> command.getCommandTrigger().equals(content)).setDisplayFunction(Sx4Command::getUsage);
paged.onSelect(select -> event.reply(HelpUtility.getHelpMessage(select.getSelected(), embed)).queue());
paged.execute(event);
} else {
event.replyFailure("I could not find that command/module").queue();
}
}
}
use of com.sx4.bot.entities.argument.Or in project Sx4 by sx4-discord-bot.
the class MoveCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "user", nullDefault = true) Member member, @Argument(value = "voice channel", nullDefault = true) VoiceChannel channel) {
if (member == null && channel == null) {
event.replyFailure("Either a user or voice channel needs to be provided").queue();
return;
}
Member effectiveMember = member == null ? event.getMember() : member;
VoiceChannel memberChannel = effectiveMember.getVoiceState().getChannel();
if (memberChannel == null) {
event.replyFailure("That user is not in a voice channel").queue();
return;
}
VoiceChannel effectiveChannel = channel == null ? event.getMember().getVoiceState().getChannel() : channel;
if (effectiveChannel == null) {
event.replyFailure("You are not in a voice channel").queue();
return;
}
if (memberChannel.getIdLong() == effectiveChannel.getIdLong()) {
event.replyFailure("You cannot move a user to the channel they are already connected to").queue();
return;
}
if (!PermissionUtility.canConnect(event.getSelfMember(), effectiveChannel)) {
event.replyFailure("I cannot move people to that voice channel").queue();
return;
}
if (!PermissionUtility.canConnect(event.getMember(), effectiveChannel)) {
event.replyFailure("You cannot move people to that voice channel").queue();
return;
}
event.getGuild().moveVoiceMember(effectiveMember, effectiveChannel).flatMap($ -> event.replySuccess("Moved **" + effectiveMember.getUser().getAsTag() + "** to `" + effectiveChannel.getName() + "`")).queue();
}
Aggregations