Search in sources :

Example 66 with Sx4CommandEvent

use of com.sx4.bot.core.Sx4CommandEvent 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 67 with Sx4CommandEvent

use of com.sx4.bot.core.Sx4CommandEvent 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 68 with Sx4CommandEvent

use of com.sx4.bot.core.Sx4CommandEvent in project Sx4 by sx4-discord-bot.

the class WelcomerCommand method formatters.

@Command(value = "formatters", aliases = { "format", "formatting" }, description = "Get all the formatters for welcomer you can use")
@CommandId(441)
@Examples({ "welcomer formatters" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void formatters(Sx4CommandEvent event) {
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Welcomer Formatters", null, event.getSelfUser().getEffectiveAvatarUrl());
    FormatterManager manager = FormatterManager.getDefaultManager();
    StringJoiner content = new StringJoiner("\n");
    for (FormatterVariable<?> variable : manager.getVariables(User.class)) {
        content.add("`{user." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(Member.class)) {
        content.add("`{member." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(Guild.class)) {
        content.add("`{server." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(OffsetDateTime.class)) {
        content.add("`{now." + variable.getName() + "}` - " + variable.getDescription());
    }
    content.add("`{user.age}` - Gets the age of a user as a string");
    embed.setDescription(content.toString());
    event.reply(embed.build()).queue();
}
Also used : FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StringJoiner(java.util.StringJoiner) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 69 with Sx4CommandEvent

use of com.sx4.bot.core.Sx4CommandEvent 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();
    });
}
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 70 with Sx4CommandEvent

use of com.sx4.bot.core.Sx4CommandEvent in project Sx4 by sx4-discord-bot.

the class WelcomerCommand method dmToggle.

@Command(value = "dm toggle", aliases = { "dm" }, description = "Toggle the state of welcomer private messaging the user")
@CommandId(98)
@Examples({ "welcomer dm toggle" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void dmToggle(Sx4CommandEvent event) {
    List<Bson> update = List.of(Operators.set("welcomer.dm", Operators.cond("$welcomer.dm", Operators.REMOVE, true)));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER).projection(Projections.include("welcomer.dm")).upsert(true);
    event.getMongo().findAndUpdateGuildById(event.getGuild().getIdLong(), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        event.replySuccess("Welcomer will " + (data.getEmbedded(List.of("welcomer", "dm"), false) ? "now" : "no longer") + " send in private messages").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)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)255 Command (com.jockie.bot.core.command.Command)181 Document (org.bson.Document)153 ModuleCategory (com.sx4.bot.category.ModuleCategory)130 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)130 CommandId (com.sx4.bot.annotations.command.CommandId)121 Argument (com.jockie.bot.core.argument.Argument)119 Examples (com.sx4.bot.annotations.command.Examples)119 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)119 Permission (net.dv8tion.jda.api.Permission)119 Bson (org.bson.conversions.Bson)111 PagedResult (com.sx4.bot.paged.PagedResult)90 HttpCallback (com.sx4.bot.http.HttpCallback)69 Request (okhttp3.Request)68 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)60 User (net.dv8tion.jda.api.entities.User)57 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)55 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)54 Operators (com.sx4.bot.database.mongo.model.Operators)50 List (java.util.List)50