Search in sources :

Example 6 with Sx4CommandEvent

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

the class MediaModeCommand method remove.

@Command(value = "remove", aliases = { "delete" }, description = "Removes a channel as media only channel")
@CommandId(351)
@Examples({ "media mode remove", "media mode remove #media" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void remove(Sx4CommandEvent event, @Argument(value = "channel", endless = true, nullDefault = true) TextChannel channel) {
    TextChannel effectiveChannel = channel == null ? event.getTextChannel() : channel;
    event.getMongo().deleteMediaChannel(Filters.eq("channelId", effectiveChannel.getIdLong())).whenComplete((result, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (result.getDeletedCount() == 0) {
            event.replyFailure("That channel is not a media only channel").queue();
            return;
        }
        event.replySuccess(effectiveChannel.getAsMention() + " is no longer a media only channel").queue();
    });
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) 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 Sx4CommandEvent

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

the class MediaModeCommand method add.

@Command(value = "add", description = "Add a channel as a media only channel")
@CommandId(350)
@Examples({ "media mode add", "media mode add #media" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void add(Sx4CommandEvent event, @Argument(value = "channel", endless = true, nullDefault = true) TextChannel channel) {
    TextChannel effectiveChannel = channel == null ? event.getTextChannel() : channel;
    Document data = new Document("channelId", effectiveChannel.getIdLong()).append("guildId", event.getGuild().getIdLong());
    event.getMongo().insertMediaChannel(data).whenComplete((result, exception) -> {
        Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
        if (cause instanceof MongoWriteException && ((MongoWriteException) cause).getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
            event.replyFailure("That channel is already a media only channel").queue();
            return;
        }
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        event.replySuccess(effectiveChannel.getAsMention() + " is now a media only channel").queue();
    });
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) MongoWriteException(com.mongodb.MongoWriteException) CompletionException(java.util.concurrent.CompletionException) Document(org.bson.Document) 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 8 with Sx4CommandEvent

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

the class FormatterCommand method onCommand.

public void onCommand(Sx4CommandEvent event, @Argument(value = "arguments", nullDefault = true) String content) {
    FormatterManager manager = FormatterManager.getDefaultManager();
    String[] arguments = content == null ? new String[0] : content.split("\\.");
    Set<Class<?>> functionClasses = manager.getFunctions().keySet();
    Set<Class<?>> variableClasses = manager.getVariables().keySet();
    Set<Class<?>> classes = new HashSet<>();
    classes.addAll(functionClasses);
    classes.addAll(variableClasses);
    List<Class<?>> filteredClasses = classes.stream().filter(clazz -> arguments.length == 0 || arguments[0].equalsIgnoreCase(clazz.getSimpleName())).collect(Collectors.toList());
    if (filteredClasses.isEmpty()) {
        event.replyFailure("I could not find that formatter type").queue();
        return;
    }
    PagedResult<Class<?>> paged = new PagedResult<>(event.getBot(), filteredClasses).setPerPage(15).setAutoSelect(true).setDisplayFunction(Class::getSimpleName);
    paged.onSelect(select -> {
        Class<?> clazz = select.getSelected();
        for (int i = 1; i < arguments.length; i++) {
            String name = arguments[i];
            FormatterFunction<?> function = manager.getFunctions(clazz).stream().filter(f -> f.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
            FormatterVariable<?> variable = manager.getVariables(clazz).stream().filter(v -> v.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
            boolean last = i == arguments.length - 1;
            MessageEmbed embed;
            if (function != null) {
                Method method = function.getMethod();
                if (!last) {
                    clazz = method.getReturnType();
                    continue;
                }
                embed = new EmbedBuilder().setDescription(function.getDescription()).setTitle(this.getFunctionString(function)).addField("Return Type", method.getReturnType().getSimpleName(), true).build();
            } else if (variable != null) {
                Class<?> returnType = variable.getReturnType();
                if (!last) {
                    clazz = returnType;
                    continue;
                }
                embed = new EmbedBuilder().setDescription(variable.getDescription()).setTitle(variable.getName()).addField("Return Type", returnType.getSimpleName(), true).build();
            } else {
                event.replyFailure("I could not find a variable or function named `" + name + "` on the type `" + clazz.getSimpleName() + "`").queue();
                return;
            }
            event.reply(embed).queue();
            return;
        }
        String variables = manager.getVariables(clazz).stream().map(FormatterVariable::getName).collect(Collectors.joining("\n"));
        String functions = manager.getFunctions(clazz).stream().map(this::getFunctionString).collect(Collectors.joining("\n"));
        EmbedBuilder embed = new EmbedBuilder().setTitle(clazz.getSimpleName()).addField("Functions", functions.isEmpty() ? "None" : functions, true).addField("Variables", variables.isEmpty() ? "None" : variables, true);
        event.reply(embed.build()).queue();
    });
    paged.execute(event);
}
Also used : Arrays(java.util.Arrays) FormatterVariable(com.sx4.bot.formatter.function.FormatterVariable) Sx4Command(com.sx4.bot.core.Sx4Command) Set(java.util.Set) FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) PagedResult(com.sx4.bot.paged.PagedResult) Collectors(java.util.stream.Collectors) FormatterFunction(com.sx4.bot.formatter.function.FormatterFunction) HashSet(java.util.HashSet) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Parameter(java.lang.reflect.Parameter) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Method(java.lang.reflect.Method) Argument(com.jockie.bot.core.argument.Argument) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Method(java.lang.reflect.Method) FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) HashSet(java.util.HashSet)

Example 9 with Sx4CommandEvent

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

the class FreeGamesCommand method platforms.

@Command(value = "platforms", description = "Select what platforms you want notifications from")
@CommandId(491)
@Examples({ "free games platforms #channel STEAM", "free games platforms STEAM EPIC_GAMES" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void platforms(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true) TextChannel channel, @Argument(value = "platforms") FreeGameType... types) {
    TextChannel effectiveChannel = channel == null ? event.getTextChannel() : channel;
    long raw = FreeGameType.getRaw(types);
    event.getMongo().updateFreeGameChannel(Filters.eq("channelId", effectiveChannel.getIdLong()), Updates.set("platforms", raw), new UpdateOptions()).whenComplete((result, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        if (result.getMatchedCount() == 0) {
            event.replyFailure("You do not have a free game channel in " + effectiveChannel.getAsMention()).queue();
            return;
        }
        if (result.getModifiedCount() == 0) {
            event.replyFailure("That free game channel already uses those platforms").queue();
            return;
        }
        event.replySuccess("That free game channel will now send notifications from those platforms").queue();
    });
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 10 with Sx4CommandEvent

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

the class FreeGamesCommand method toggle.

@Command(value = "toggle", description = "Enables/disables a free game channel")
@CommandId(484)
@Examples({ "free games toggle", "free games toggle #channel" })
@AuthorPermissions(permissions = { Permission.MANAGE_SERVER })
public void toggle(Sx4CommandEvent event, @Argument(value = "channel", nullDefault = true, endless = true) TextChannel channel) {
    TextChannel effectiveChannel = channel == null ? event.getTextChannel() : channel;
    List<Bson> update = List.of(Operators.set("enabled", Operators.cond(Operators.exists("$enabled"), Operators.REMOVE, false)));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().projection(Projections.include("enabled")).returnDocument(ReturnDocument.AFTER);
    event.getMongo().findAndUpdateFreeGameChannel(Filters.eq("channelId", effectiveChannel.getIdLong()), update, options).whenComplete((data, exception) -> {
        if (ExceptionUtility.sendExceptionally(event, exception)) {
            return;
        }
        event.replySuccess("The free game channel in " + effectiveChannel.getAsMention() + " is now **" + (data.get("enabled", true) ? "enabled" : "disabled") + "**").queue();
    });
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) 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