Search in sources :

Example 16 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class StarboardCommand method delete.

@Command(value = "delete", aliases = { "remove" }, description = "Deletes a starboard")
@CommandId(204)
@Examples({ "starboard delete 5ff636647f93247aeb2ac429", "starboard delete all" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void delete(Sx4CommandEvent event, @Argument(value = "id | all") @AlternativeOptions("all") Alternative<ObjectId> option) {
    if (option.isAlternative()) {
        String acceptId = new CustomButtonId.Builder().setType(ButtonType.STARBOARD_DELETE_CONFIRM).setOwners(event.getAuthor().getIdLong()).setTimeout(60).getId();
        String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setOwners(event.getAuthor().getIdLong()).setTimeout(60).getId();
        List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
        event.reply(event.getAuthor().getName() + ", are you sure you want to delete **all** starboards in this server?").setActionRow(buttons).queue();
    } else {
        ObjectId id = option.getValue();
        AtomicReference<Document> atomicData = new AtomicReference<>();
        event.getMongo().findAndDeleteStarboard(Filters.and(Filters.eq("_id", id), Filters.eq("guildId", event.getGuild().getIdLong()))).thenCompose(data -> {
            if (data == null) {
                return CompletableFuture.completedFuture(null);
            }
            atomicData.set(data);
            return event.getMongo().deleteManyStars(Filters.eq("messageId", data.getLong("originalMessageId")));
        }).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result == null) {
                event.replyFailure("I could not find that starboard").queue();
                return;
            }
            Document data = atomicData.get();
            WebhookClient webhook = event.getBot().getStarboardManager().getWebhook(data.getLong("channelId"));
            if (webhook != null) {
                webhook.delete(data.getLong("messageId"));
            }
            event.replySuccess("That starboard has been deleted").queue();
        });
    }
}
Also used : Document(org.bson.Document) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) ReactionEmote(net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote) FormatterVariable(com.sx4.bot.formatter.function.FormatterVariable) WebhookClient(club.minnced.discord.webhook.WebhookClient) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) CompletableFuture(java.util.concurrent.CompletableFuture) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) PagedResult(com.sx4.bot.paged.PagedResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) com.mongodb.client.model(com.mongodb.client.model) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) Operators(com.sx4.bot.database.mongo.model.Operators) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Sx4Command(com.sx4.bot.core.Sx4Command) StarboardManager(com.sx4.bot.managers.StarboardManager) FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) StringJoiner(java.util.StringJoiner) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) CustomButtonId(com.sx4.bot.entities.interaction.CustomButtonId) ObjectId(org.bson.types.ObjectId) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Comparator(java.util.Comparator) ButtonType(com.sx4.bot.entities.interaction.ButtonType) WebhookClient(club.minnced.discord.webhook.WebhookClient) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) ObjectId(org.bson.types.ObjectId) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) Document(org.bson.Document) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 17 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class AutoRoleCommand method remove.

@Command(value = "remove", aliases = { "delete" }, description = "Remove a role from being given when a user joins")
@CommandId(40)
@Examples({ "auto role remove @Role", "auto role remove Role", "auto role remove all" })
@AuthorPermissions(permissions = { Permission.MANAGE_ROLES })
public void remove(Sx4CommandEvent event, @Argument(value = "role | all", endless = true) @AlternativeOptions("all") Alternative<Role> option) {
    if (option.isAlternative()) {
        String acceptId = new CustomButtonId.Builder().setType(ButtonType.AUTO_ROLE_DELETE_CONFIRM).setOwners(event.getAuthor().getIdLong()).setTimeout(60).getId();
        String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setOwners(event.getAuthor().getIdLong()).setTimeout(60).getId();
        List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
        event.reply(event.getAuthor().getName() + ", are you sure you want to remove every auto role in the server?").setActionRow(buttons).queue();
    } else {
        Role role = option.getValue();
        event.getMongo().deleteAutoRole(Filters.eq("roleId", role.getIdLong())).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result.getDeletedCount() == 0) {
                event.replyFailure("That role is not an auto role").queue();
                return;
            }
            event.replySuccess(role.getAsMention() + " is no longer an auto role").queue();
        });
    }
}
Also used : Role(net.dv8tion.jda.api.entities.Role) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 18 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class LeaverCommand method channel.

@Command(value = "channel", description = "Sets the channel where leaver messages are sent to")
@CommandId(190)
@Examples({ "leaver channel", "leaver channel #leaves", "leaver channel reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void channel(Sx4CommandEvent event, @Argument(value = "channel | reset", endless = true, nullDefault = true) @AlternativeOptions("reset") Alternative<BaseGuildMessageChannel> option) {
    MessageChannel messageChannel = event.getChannel();
    if (option == null && !(messageChannel instanceof BaseGuildMessageChannel)) {
        event.replyFailure("You cannot use this channel type").queue();
        return;
    }
    BaseGuildMessageChannel channel = option == null ? (BaseGuildMessageChannel) messageChannel : option.isAlternative() ? null : option.getValue();
    List<Bson> update = List.of(Operators.set("leaver.channelId", channel == null ? Operators.REMOVE : channel.getIdLong()), Operators.unset("leaver.webhook.id"), Operators.unset("leaver.webhook.token"));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true).projection(Projections.include("leaver.webhook.token", "leaver.webhook.id", "leaver.channelId")).returnDocument(ReturnDocument.BEFORE);
    event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        long channelId = data == null ? 0L : data.getEmbedded(List.of("leaver", "channelId"), 0L);
        event.getBot().getLeaverManager().removeWebhook(channelId);
        if ((channel == null ? 0L : channel.getIdLong()) == channelId) {
            event.replyFailure("The leaver channel is already " + (channel == null ? "unset" : "set to " + channel.getAsMention())).queue();
            return;
        }
        BaseGuildMessageChannel oldChannel = channelId == 0L ? null : event.getGuild().getChannelById(BaseGuildMessageChannel.class, channelId);
        long webhookId = data == null ? 0L : data.getEmbedded(List.of("leaver", "webhook", "id"), 0L);
        if (oldChannel != null && webhookId != 0L) {
            oldChannel.deleteWebhookById(Long.toString(webhookId)).queue(null, ErrorResponseException.ignore(ErrorResponse.UNKNOWN_WEBHOOK));
        }
        event.replySuccess("The leaver channel has been " + (channel == null ? "unset" : "set to " + channel.getAsMention())).queue();
    });
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 19 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class MuteCommand method leaveAction.

@Command(value = "leave action", aliases = { "leaveaction" }, description = "Set an action to occur when a user leaves and rejoins while muted")
@CommandId(451)
@Examples({ "mute leave action BAN", "mute leave action MUTE_EXTEND 24h", "mute leave action reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void leaveAction(Sx4CommandEvent event, @Argument(value = "action | reset", endless = true) @AlternativeOptions({ "reset" }) @EnumOptions(value = { "KICK", "UNBAN", "UNMUTE" }, exclude = true) Alternative<TimedArgument<ModAction>> option) {
    Bson update;
    if (option.isAlternative()) {
        update = Updates.unset("mute.leaveAction");
    } else {
        TimedArgument<ModAction> timedAction = option.getValue();
        ModAction action = timedAction.getArgument();
        Document modAction = new Document("type", action.getType());
        if (action.isTimed()) {
            Duration duration = timedAction.getDuration();
            if (duration == null) {
                event.replyFailure("You need to provide a duration for this mod action").queue();
                return;
            }
            modAction.append("duration", duration.toSeconds());
        }
        update = Updates.set("mute.leaveAction", modAction);
    }
    event.getMongo().updateGuildById(event.getGuild().getIdLong(), update).whenComplete((result, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (result.getModifiedCount() == 0 && result.getUpsertedId() == null) {
            event.replyFailure("Your leave action was already " + (option.isAlternative() ? "unset" : "set to that")).queue();
            return;
        }
        event.replySuccess("Your leave action has been " + (option.isAlternative() ? "unset" : "updated")).queue();
    });
}
Also used : ModAction(com.sx4.bot.entities.mod.action.ModAction) Duration(java.time.Duration) Document(org.bson.Document) Bson(org.bson.conversions.Bson) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 20 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions in project Sx4 by sx4-discord-bot.

the class FakePermissionsCommand method delete.

@Command(value = "delete", aliases = { "remove" }, description = "Deletes fake permissions for a user or role")
@CommandId(172)
@Examples({ "fake permissions delete @Shea#6653", "fake permissions delete @Mods", "fake permissions delete all" })
@AuthorPermissions(permissions = { Permission.ADMINISTRATOR })
public void delete(Sx4CommandEvent event, @Argument(value = "user | role | all", endless = true, nullDefault = true) @AlternativeOptions("all") Alternative<IPermissionHolder> option) {
    if (option == null) {
        List<Document> fakePermissions = event.getProperty("fakePermissions");
        if (fakePermissions.isEmpty()) {
            event.replyFailure("Nothing has fake permissions in this server").queue();
            return;
        }
        PagedResult<Document> paged = new PagedResult<>(event.getBot(), fakePermissions).setAuthor("User & Roles", null, event.getGuild().getIconUrl()).setTimeout(60).setDisplayFunction(data -> {
            int type = data.getInteger("type");
            long id = data.getLong("id");
            if (type == HolderType.USER.getType()) {
                User user = event.getShardManager().getUserById(id);
                return user == null ? "Anonymous#0000 (" + id + ")" : user.getAsTag();
            } else {
                Role role = event.getGuild().getRoleById(id);
                return role == null ? "Deleted Role (" + id + ")" : role.getAsMention();
            }
        });
        paged.onTimeout(() -> event.reply("Timed out :stopwatch:").queue());
        paged.onSelect(select -> {
            Document data = select.getSelected();
            boolean isRole = data.getInteger("type") == HolderType.ROLE.getType();
            long id = data.getLong("id");
            User user = isRole ? null : event.getShardManager().getUserById(id);
            Role role = isRole ? event.getGuild().getRoleById(id) : null;
            event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.pull("fakePermissions.holders", Filters.eq("id", id))).whenComplete((result, exception) -> {
                if (ExceptionUtility.sendExceptionally(event, exception)) {
                    return;
                }
                if (result.getModifiedCount() == 0) {
                    event.replyFailure("That " + (isRole ? "role" : "user") + " doesn't have any fake permissions").queue();
                    return;
                }
                event.replySuccess((isRole ? (role == null ? "Deleted Role (" + id + ")" : role.getAsMention()) : "**" + (user == null ? "Anonymous#0000** (" + id + ")" : user.getAsTag() + "**")) + " no longer has any fake permissions").queue();
            });
        });
        paged.execute(event);
    } else if (option.isAlternative()) {
        String acceptId = new CustomButtonId.Builder().setType(ButtonType.FAKE_PERMISSIONS_DELETE_CONFIRM).setOwners(event.getAuthor().getIdLong()).setTimeout(60).getId();
        String rejectId = new CustomButtonId.Builder().setType(ButtonType.GENERIC_REJECT).setOwners(event.getAuthor().getIdLong()).setTimeout(60).getId();
        List<Button> buttons = List.of(Button.success(acceptId, "Yes"), Button.danger(rejectId, "No"));
        event.reply(event.getAuthor().getName() + ", are you sure you want to delete **all** fake permissions data?").setActionRow(buttons).queue();
    } else {
        IPermissionHolder holder = option.getValue();
        boolean role = holder instanceof Role;
        event.getMongo().updateGuildById(event.getGuild().getIdLong(), Updates.pull("fakePermissions.holders", Filters.eq("id", holder.getIdLong()))).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            if (result.getModifiedCount() == 0) {
                event.replyFailure("That " + (role ? "role" : "user") + " doesn't have any fake permissions").queue();
                return;
            }
            event.replySuccess((role ? ((Role) holder).getAsMention() : "**" + ((Member) holder).getUser().getAsTag() + "**") + " no longer has any fake permissions").queue();
        });
    }
}
Also used : HolderType(com.sx4.bot.entities.settings.HolderType) Document(org.bson.Document) Arrays(java.util.Arrays) Command(com.jockie.bot.core.command.Command) Permission(net.dv8tion.jda.api.Permission) Projections(com.mongodb.client.model.Projections) CommandId(com.sx4.bot.annotations.command.CommandId) Member(net.dv8tion.jda.api.entities.Member) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Argument(com.jockie.bot.core.argument.Argument) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Operators(com.sx4.bot.database.mongo.model.Operators) Sx4Command(com.sx4.bot.core.Sx4Command) Predicate(java.util.function.Predicate) Updates(com.mongodb.client.model.Updates) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Collectors(java.util.stream.Collectors) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Examples(com.sx4.bot.annotations.command.Examples) IPermissionHolder(net.dv8tion.jda.api.entities.IPermissionHolder) CustomButtonId(com.sx4.bot.entities.interaction.CustomButtonId) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Comparator(java.util.Comparator) Collections(java.util.Collections) ButtonType(com.sx4.bot.entities.interaction.ButtonType) User(net.dv8tion.jda.api.entities.User) Document(org.bson.Document) IPermissionHolder(net.dv8tion.jda.api.entities.IPermissionHolder) Role(net.dv8tion.jda.api.entities.Role) CustomButtonId(com.sx4.bot.entities.interaction.CustomButtonId) List(java.util.List) AuthorPermissions(com.sx4.bot.annotations.command.AuthorPermissions) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Aggregations

Command (com.jockie.bot.core.command.Command)26 Sx4Command (com.sx4.bot.core.Sx4Command)26 Bson (org.bson.conversions.Bson)21 CommandId (com.sx4.bot.annotations.command.CommandId)16 Examples (com.sx4.bot.annotations.command.Examples)16 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)11 Document (org.bson.Document)11 Button (net.dv8tion.jda.api.interactions.components.buttons.Button)10 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)9 TextChannel (net.dv8tion.jda.api.entities.TextChannel)8 Argument (com.jockie.bot.core.argument.Argument)7 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)7 ModuleCategory (com.sx4.bot.category.ModuleCategory)7 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)7 Alternative (com.sx4.bot.entities.argument.Alternative)7 CustomButtonId (com.sx4.bot.entities.interaction.CustomButtonId)6 PagedResult (com.sx4.bot.paged.PagedResult)6 List (java.util.List)6 Filters (com.mongodb.client.model.Filters)5 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)5