Search in sources :

Example 6 with AlternativeOptions

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

the class SelfRoleCommand method remove.

@Command(value = "delete", aliases = { "remove" }, description = "Delete a self role from the current server")
@CommandId(333)
@Examples({ "self role delete @Role", "self role delete Role", "self role delete 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.SELF_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 delete every self role in the server?").setActionRow(buttons).queue();
    } else {
        Role role = option.getValue();
        event.getMongo().deleteSelfRole(Filters.eq("roleId", role.getIdLong())).whenComplete((result, exception) -> {
            if (ExceptionUtility.sendExceptionally(event, exception)) {
                return;
            }
            event.replySuccess(role.getAsMention() + " is no longer a self role").queue();
        });
    }
}
Also used : Role(net.dv8tion.jda.api.entities.Role) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) 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 7 with AlternativeOptions

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

the class StarboardCommand method emote.

@Command(value = "emote", aliases = { "emoji" }, description = "Sets the emote/emoji to be used for starboard")
@CommandId(199)
@Examples({ "starboard emote ☝️", "starboard emote <:upvote:761345612079693865>", "starboard emote reset" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void emote(Sx4CommandEvent event, @Argument(value = "emote | reset", endless = true) @AlternativeOptions("reset") Alternative<ReactionEmote> option) {
    ReactionEmote emote = option.getValue();
    boolean emoji = emote != null && emote.isEmoji();
    List<Bson> update = emote == null ? List.of(Operators.unset("starboard.emote")) : List.of(Operators.set("starboard.emote." + (emoji ? "name" : "id"), emoji ? emote.getEmoji() : emote.getIdLong()), Operators.unset("starboard.emote." + (emoji ? "id" : "name")));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.BEFORE).upsert(true).projection(Projections.include("starboard.emote"));
    event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        Document emoteData = data == null ? null : data.getEmbedded(List.of("starboard", "emote"), Document.class);
        if ((emote == null && emoteData == null) || (emote != null && emoteData != null && (emoji ? emote.getEmoji().equals(emoteData.getString("name")) : emoteData.getLong("id") == emote.getIdLong()))) {
            event.replyFailure("Your starboard emote was already " + (emote == null ? "unset" : "set to that")).queue();
            return;
        }
        event.replySuccess("Your starboard emote has been " + (emote == null ? "unset" : "updated")).queue();
    });
}
Also used : Document(org.bson.Document) ReactionEmote(net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 8 with AlternativeOptions

use of com.sx4.bot.annotations.argument.AlternativeOptions 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;
        }
        String acceptId = new CustomButtonId.Builder().setType(ButtonType.SUGGESTION_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(author.getName() + ", are you sure you want to delete **all** the suggestions in this server?").setActionRow(buttons).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();
        });
    }
}
Also used : MessageArgument(com.sx4.bot.entities.argument.MessageArgument) WebhookClient(club.minnced.discord.webhook.WebhookClient) Button(net.dv8tion.jda.api.interactions.components.buttons.Button) ObjectId(org.bson.types.ObjectId) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 9 with AlternativeOptions

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

the class WelcomerCommand method channel.

@Command(value = "channel", description = "Sets the channel where welcomer messages are sent to")
@CommandId(93)
@Examples({ "welcomer channel", "welcomer channel #joins", "welcomer 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("welcomer.channelId", channel == null ? Operators.REMOVE : channel.getIdLong()), Operators.unset("welcomer.webhook.id"), Operators.unset("welcomer.webhook.token"));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true).projection(Projections.include("welcomer.webhook.id", "welcomer.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("welcomer", "channelId"), 0L);
        event.getBot().getWelcomerManager().removeWebhook(channelId);
        if ((channel == null ? 0L : channel.getIdLong()) == channelId) {
            event.replyFailure("The welcomer 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("welcomer", "webhook", "id"), 0L);
        if (oldChannel != null && webhookId != 0L) {
            oldChannel.deleteWebhookById(Long.toString(webhookId)).queue(null, ErrorResponseException.ignore(ErrorResponse.UNKNOWN_WEBHOOK));
        }
        event.replySuccess("The welcomer 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 10 with AlternativeOptions

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

the class RoleCommand method add.

@Command(value = "add", description = "Add a role to a member")
@CommandId(250)
@Redirects({ "addrole", "add role", "ar" })
@Examples({ "role add @Shea#6653 Role", "role add Shea 345718366373150720", "role add @Role" })
@AuthorPermissions(permissions = { Permission.MANAGE_ROLES })
@BotPermissions(permissions = { Permission.MANAGE_ROLES })
public void add(Sx4CommandEvent event, @Argument(value = "user", nullDefault = true) @AlternativeOptions("all") Alternative<Member> option, @Argument(value = "role", endless = true) Role role) {
    if (role.isManaged()) {
        event.replyFailure("I cannot give managed roles").queue();
        return;
    }
    if (role.isPublicRole()) {
        event.replyFailure("I cannot give the @everyone role").queue();
        return;
    }
    if (!event.getMember().canInteract(role)) {
        event.replyFailure("You cannot give a role higher or equal than your top role").queue();
        return;
    }
    if (!event.getSelfMember().canInteract(role)) {
        event.replyFailure("I cannot give a role higher or equal than my top role").queue();
        return;
    }
    if (option != null && option.isAlternative()) {
        List<Member> members = event.getGuild().getMemberCache().applyStream(stream -> stream.filter(member -> !member.getRoles().contains(role)).collect(Collectors.toList()));
        if (members.size() == 0) {
            event.replyFailure("All users already have that role").queue();
            return;
        }
        if (!this.pending.add(event.getGuild().getIdLong())) {
            event.replyFailure("You can only have 1 concurrent role being added to all users").queue();
            return;
        }
        event.replyFormat("Adding %s to **%,d** user%s, another message will be sent once this is done %s", role.getAsMention(), members.size(), members.size() == 1 ? "" : "s", event.getConfig().getSuccessEmote()).queue();
        List<CompletableFuture<Integer>> futures = new ArrayList<>();
        for (Member member : members) {
            futures.add(event.getGuild().addRoleToMember(member, role).submit().handle((result, exception) -> exception == null ? 1 : 0));
        }
        FutureUtility.allOf(futures).whenComplete((completed, exception) -> {
            this.pending.remove(event.getGuild().getIdLong());
            int count = completed.stream().reduce(0, Integer::sum);
            event.replyFormat("Successfully added the role %s to **%,d/%,d** user%s %s", role.getAsMention(), count, members.size(), count == 1 ? "" : "s", event.getConfig().getSuccessEmote()).queue();
        });
    } else {
        Member effectiveMember = option == null ? event.getMember() : option.getValue();
        event.getGuild().addRoleToMember(effectiveMember, role).flatMap($ -> event.replySuccess(role.getAsMention() + " has been added to **" + effectiveMember.getUser().getAsTag() + "**")).queue();
    }
}
Also used : Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command) Permission(net.dv8tion.jda.api.Permission) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Member(net.dv8tion.jda.api.entities.Member) Colour(com.sx4.bot.annotations.argument.Colour) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ModuleCategory(com.sx4.bot.category.ModuleCategory) Alternative(com.sx4.bot.entities.argument.Alternative) PermissionUtility(com.sx4.bot.utility.PermissionUtility) List(java.util.List) Role(net.dv8tion.jda.api.entities.Role) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Option(com.jockie.bot.core.option.Option) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) FutureUtility(com.sx4.bot.utility.FutureUtility) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Argument(com.jockie.bot.core.argument.Argument) Limit(com.sx4.bot.annotations.argument.Limit) PermissionUtil(net.dv8tion.jda.internal.utils.PermissionUtil) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) Member(net.dv8tion.jda.api.entities.Member) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

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