Search in sources :

Example 1 with SuggestionState

use of com.sx4.bot.entities.management.SuggestionState in project Sx4 by sx4-discord-bot.

the class SuggestionCommand method set.

@Command(value = "set", description = "Sets a suggestion to a specified state")
@CommandId(86)
@Examples({ "suggestion set 5e45ce6d3688b30ee75201ae pending Need some time to think about this", "suggestion set 5e45ce6d3688b30ee75201ae accepted I think this is a great idea", "suggestion 5e45ce6d3688b30ee75201ae set denied Not possible" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void set(Sx4CommandEvent event, @Argument(value = "id | message", acceptEmpty = true) Or<ObjectId, MessageArgument> argument, @Argument(value = "state") String stateName, @Argument(value = "reason", endless = true, nullDefault = true) String reason) {
    Document data = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("suggestion.states", "suggestion.webhook")).get("suggestion", MongoDatabase.EMPTY_DOCUMENT);
    List<Document> states = data.getList("states", Document.class, SuggestionState.DEFAULT_STATES);
    Document state = states.stream().filter(stateData -> stateData.getString("dataName").equalsIgnoreCase(stateName)).findFirst().orElse(null);
    if (state == null) {
        event.replyFailure("You do not have a suggestion state with that name").queue();
        return;
    }
    String stateData = state.getString("dataName");
    Bson update = Updates.combine(reason == null ? Updates.unset("reason") : Updates.set("reason", reason), Updates.set("state", stateData), Updates.set("moderatorId", event.getAuthor().getIdLong()));
    Bson filter = Filters.and(argument.hasFirst() ? Filters.eq("_id", argument.getFirst()) : Filters.eq("messageId", argument.getSecond().getMessageId()), Filters.eq("guildId", event.getGuild().getIdLong()));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.BEFORE).projection(Projections.include("channelId", "authorId", "reason", "state", "suggestion", "messageId", "image"));
    event.getMongo().findAndUpdateSuggestion(filter, update, options).whenComplete((suggestionData, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (suggestionData == null) {
            event.replyFailure("There is no suggestion with that id").queue();
            return;
        }
        String reasonData = suggestionData.getString("reason");
        boolean reasonMatch = reasonData == null && reason == null || (reasonData != null && reasonData.equals(reason));
        if (suggestionData.getString("state").equals(stateData) && reasonMatch) {
            event.replyFailure("That suggestion is already in that state and has the same reason").queue();
            return;
        }
        TextChannel channel = event.getGuild().getTextChannelById(suggestionData.getLong("channelId"));
        if (channel == null) {
            event.replyFailure("The channel for that suggestion no longer exists").queue();
            return;
        }
        User author = event.getShardManager().getUserById(suggestionData.getLong("authorId"));
        long messageId = suggestionData.getLong("messageId");
        if (author != null) {
            author.openPrivateChannel().flatMap(privateChannel -> privateChannel.sendMessage("Your suggestion has been updated by a moderator, click the message link to view it\nhttps://discord.com/channels/" + event.getGuild().getIdLong() + "/" + channel.getIdLong() + "/" + messageId)).queue(null, ErrorResponseException.ignore(ErrorResponse.CANNOT_SEND_TO_USER));
        }
        WebhookEmbed embed = Suggestion.getWebhookEmbed(suggestionData.getObjectId("_id"), event.getAuthor(), author, suggestionData.getString("suggestion"), suggestionData.getString("image"), reason, new SuggestionState(state));
        event.getBot().getSuggestionManager().editSuggestion(messageId, channel.getIdLong(), data.get("webhook", MongoDatabase.EMPTY_DOCUMENT), embed);
        event.replySuccess("That suggestion has been set to the `" + stateData + "` state").queue();
    });
}
Also used : Document(org.bson.Document) CancelException(com.sx4.bot.waiter.exception.CancelException) WebhookClient(club.minnced.discord.webhook.WebhookClient) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) MessageArgument(com.sx4.bot.entities.argument.MessageArgument) Suggestion(com.sx4.bot.entities.management.Suggestion) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) ColourUtility(com.sx4.bot.utility.ColourUtility) ButtonUtility(com.sx4.bot.utility.ButtonUtility) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) com.mongodb.client.model(com.mongodb.client.model) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Or(com.sx4.bot.entities.argument.Or) Argument(com.jockie.bot.core.argument.Argument) Message(net.dv8tion.jda.api.entities.Message) Operators(com.sx4.bot.database.mongo.model.Operators) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Sx4Command(com.sx4.bot.core.Sx4Command) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) Colour(com.sx4.bot.annotations.argument.Colour) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) ObjectId(org.bson.types.ObjectId) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) SuggestionState(com.sx4.bot.entities.management.SuggestionState) Collections(java.util.Collections) TextChannel(net.dv8tion.jda.api.entities.TextChannel) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) User(net.dv8tion.jda.api.entities.User) SuggestionState(com.sx4.bot.entities.management.SuggestionState) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 2 with SuggestionState

use of com.sx4.bot.entities.management.SuggestionState in project Sx4 by sx4-discord-bot.

the class SuggestionCommand method add.

@Command(value = "add", description = "Sends a suggestion to the suggestion channel if one is setup in the server")
@CommandId(84)
@Redirects({ "suggest" })
@Examples({ "suggestion add Add the dog emote", "suggestion Add a channel for people looking to play games" })
@BotPermissions(permissions = { Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS })
public void add(Sx4CommandEvent event, @Argument(value = "suggestion", endless = true) String description) {
    Document data = event.getMongo().getGuildById(event.getGuild().getIdLong(), Projections.include("suggestion.channelId", "suggestion.enabled", "suggestion.webhook", "premium.endAt"));
    Document suggestionData = data.get("suggestion", MongoDatabase.EMPTY_DOCUMENT);
    if (!suggestionData.getBoolean("enabled", false)) {
        event.replyFailure("Suggestions are not enabled in this server").queue();
        return;
    }
    long channelId = suggestionData.get("channelId", 0L);
    if (channelId == 0L) {
        event.replyFailure("There is no suggestion channel").queue();
        return;
    }
    TextChannel channel = event.getGuild().getTextChannelById(channelId);
    if (channel == null) {
        event.replyFailure("The suggestion channel no longer exists").queue();
        return;
    }
    SuggestionState state = SuggestionState.PENDING;
    String image = event.getMessage().getAttachments().stream().filter(Message.Attachment::isImage).map(Message.Attachment::getUrl).findFirst().orElse(null);
    Suggestion suggestion = new Suggestion(channelId, event.getGuild().getIdLong(), event.getAuthor().getIdLong(), description, image, state.getDataName());
    boolean premium = Clock.systemUTC().instant().getEpochSecond() < data.getEmbedded(List.of("premium", "endAt"), 0L);
    event.getBot().getSuggestionManager().sendSuggestion(channel, suggestionData.get("webhook", MongoDatabase.EMPTY_DOCUMENT), premium, suggestion.getWebhookEmbed(null, event.getAuthor(), state)).whenComplete((message, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        suggestion.setMessageId(message.getId());
        event.getMongo().insertSuggestion(suggestion.toData()).whenComplete((result, dataException) -> {
            if (ExceptionUtility.sendExceptionally(event, dataException)) {
                return;
            }
            channel.addReactionById(message.getId(), "✅").flatMap($ -> channel.addReactionById(message.getId(), "❌")).queue();
            event.replySuccess("Your suggestion has been sent to " + channel.getAsMention()).queue();
        });
    });
}
Also used : Document(org.bson.Document) CancelException(com.sx4.bot.waiter.exception.CancelException) WebhookClient(club.minnced.discord.webhook.WebhookClient) Command(com.jockie.bot.core.command.Command) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) MessageArgument(com.sx4.bot.entities.argument.MessageArgument) Suggestion(com.sx4.bot.entities.management.Suggestion) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) PagedResult(com.sx4.bot.paged.PagedResult) User(net.dv8tion.jda.api.entities.User) Bson(org.bson.conversions.Bson) Alternative(com.sx4.bot.entities.argument.Alternative) ColourUtility(com.sx4.bot.utility.ColourUtility) ButtonUtility(com.sx4.bot.utility.ButtonUtility) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) Button(net.dv8tion.jda.api.interactions.components.Button) AlternativeOptions(com.sx4.bot.annotations.argument.AlternativeOptions) Waiter(com.sx4.bot.waiter.Waiter) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) com.mongodb.client.model(com.mongodb.client.model) com.sx4.bot.annotations.command(com.sx4.bot.annotations.command) Or(com.sx4.bot.entities.argument.Or) Argument(com.jockie.bot.core.argument.Argument) Message(net.dv8tion.jda.api.entities.Message) Operators(com.sx4.bot.database.mongo.model.Operators) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) Sx4Command(com.sx4.bot.core.Sx4Command) CompletionException(java.util.concurrent.CompletionException) TimeoutException(com.sx4.bot.waiter.exception.TimeoutException) Colour(com.sx4.bot.annotations.argument.Colour) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) ImageUrl(com.sx4.bot.annotations.argument.ImageUrl) ObjectId(org.bson.types.ObjectId) Clock(java.time.Clock) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) SuggestionState(com.sx4.bot.entities.management.SuggestionState) Collections(java.util.Collections) Suggestion(com.sx4.bot.entities.management.Suggestion) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Message(net.dv8tion.jda.api.entities.Message) SuggestionState(com.sx4.bot.entities.management.SuggestionState) Document(org.bson.Document) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Aggregations

WebhookClient (club.minnced.discord.webhook.WebhookClient)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)2 Argument (com.jockie.bot.core.argument.Argument)2 Command (com.jockie.bot.core.command.Command)2 com.mongodb.client.model (com.mongodb.client.model)2 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)2 Colour (com.sx4.bot.annotations.argument.Colour)2 ImageUrl (com.sx4.bot.annotations.argument.ImageUrl)2 com.sx4.bot.annotations.command (com.sx4.bot.annotations.command)2 ModuleCategory (com.sx4.bot.category.ModuleCategory)2 Sx4Command (com.sx4.bot.core.Sx4Command)2 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)2 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)2 Operators (com.sx4.bot.database.mongo.model.Operators)2 Alternative (com.sx4.bot.entities.argument.Alternative)2 MessageArgument (com.sx4.bot.entities.argument.MessageArgument)2 Or (com.sx4.bot.entities.argument.Or)2 Suggestion (com.sx4.bot.entities.management.Suggestion)2 SuggestionState (com.sx4.bot.entities.management.SuggestionState)2 PagedResult (com.sx4.bot.paged.PagedResult)2