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();
});
}
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();
});
});
}
Aggregations