Search in sources :

Example 1 with SlashCommandData

use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project triumph-cmds by TriumphTeam.

the class SlashCommand method asCommandData.

@NotNull
public SlashCommandData asCommandData() {
    final SlashCommandData commandData = Commands.slash(name, description);
    commandData.setDefaultEnabled(enabledRoles.isEmpty());
    if (isDefault) {
        final SlashSubCommand<S> subCommand = getDefaultSubCommand();
        // Should never be null.
        if (subCommand == null)
            throw new CommandRegistrationException("Could not find default subcommand");
        commandData.addOptions(subCommand.getJdaOptions());
        return commandData;
    }
    final List<SubcommandData> subData = subCommands.entrySet().stream().map(entry -> new SubcommandData(entry.getKey(), entry.getValue().getDescription()).addOptions(entry.getValue().getJdaOptions())).collect(Collectors.toList());
    commandData.addSubcommands(subData);
    return commandData;
}
Also used : Command(dev.triumphteam.cmd.core.Command) NamedArgumentRegistry(dev.triumphteam.cmd.core.argument.named.NamedArgumentRegistry) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) ExecutionProvider(dev.triumphteam.cmd.core.execution.ExecutionProvider) ArgumentRegistry(dev.triumphteam.cmd.core.argument.ArgumentRegistry) MessageRegistry(dev.triumphteam.cmd.core.message.MessageRegistry) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) BaseCommand(dev.triumphteam.cmd.core.BaseCommand) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Default(dev.triumphteam.cmd.core.annotation.Default) ChoiceRegistry(dev.triumphteam.cmd.slash.choices.ChoiceRegistry) Registry(dev.triumphteam.cmd.core.registry.Registry) RequirementRegistry(dev.triumphteam.cmd.core.requirement.RequirementRegistry) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull) SenderValidator(dev.triumphteam.cmd.core.sender.SenderValidator) Method(java.lang.reflect.Method) CommandRegistrationException(dev.triumphteam.cmd.core.exceptions.CommandRegistrationException) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SlashCommandData

use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method getBaseCommandData.

/**
 * Gets the complete {@link SlashCommandData} (including Subcommands & Subcommand Groups) of a single {@link BaseSlashCommand}.
 *
 * @param command      The base command's instance.
 * @param commandClass The base command's class.
 * @param guild        The current guild (if available)
 * @return The new {@link CommandListUpdateAction}.
 * @throws Exception If an error occurs.
 */
private SlashCommandData getBaseCommandData(@NotNull BaseSlashCommand command, Class<? extends BaseSlashCommand> commandClass, @Nullable Guild guild) throws Exception {
    if (command.getCommandData() == null) {
        DIH4JDALogger.warn(String.format("Class %s is missing CommandData. It will be ignored.", commandClass.getName()));
        return null;
    }
    SlashCommandData commandData = command.getCommandData();
    if (command.getSubcommandGroups() != null) {
        commandData.addSubcommandGroups(this.getSubcommandGroupData(command, guild));
    }
    if (command.getSubcommands() != null) {
        commandData.addSubcommands(this.getSubcommandData(command, command.getSubcommands(), null, guild));
    }
    if (command.getSubcommandGroups() == null && command.getSubcommands() == null) {
        slashCommandIndex.put(buildCommandPath(commandData.getName()), new SlashCommandInteraction((ISlashCommand) command, commandClass, command.getCommandPrivileges()));
        DIH4JDALogger.info(String.format("\t[*] Registered command: /%s", command.getCommandData().getName()), DIH4JDALogger.Type.SLASH_COMMAND_REGISTERED);
    }
    return commandData;
}
Also used : ISlashCommand(com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) SlashCommandInteraction(com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)

Example 3 with SlashCommandData

use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project Bean by Xirado.

the class CommandsRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    List<SlashCommand> commands = Bean.getInstance().isDebug() ? Bean.getInstance().getInteractionCommandHandler().getRegisteredGuildCommands().get(DEV_GUILD_ID).stream().filter(cmd -> cmd instanceof SlashCommand).map(cmd -> (SlashCommand) cmd).toList() : Bean.getInstance().getInteractionCommandHandler().getRegisteredSlashCommands();
    DataArray commandArray = DataArray.empty();
    for (SlashCommand command : commands) {
        DataObject commandObject = DataObject.empty();
        SlashCommandData slashCommandData = command.getData();
        DataArray options = DataArray.empty();
        DataArray subCommands = DataArray.empty();
        for (OptionData option : slashCommandData.getOptions()) {
            DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
            options.add(optionObject);
        }
        for (SubcommandData subcommandData : slashCommandData.getSubcommands()) {
            DataObject subCommandObject = DataObject.empty();
            DataArray subCommandOptions = DataArray.empty();
            for (OptionData option : subcommandData.getOptions()) {
                DataObject optionObject = DataObject.empty().put("name", option.getName()).put("type", option.getType().toString()).put("description", option.getDescription()).put("required", option.isRequired());
                subCommandOptions.add(optionObject);
            }
            subCommandObject.put("name", subcommandData.getName()).put("description", subcommandData.getDescription());
            subCommandObject.put("options", subCommandOptions);
            subCommands.add(subCommandObject);
        }
        commandObject.put("name", slashCommandData.getName()).put("description", slashCommandData.getDescription()).put("options", options).put("sub_commands", subCommands);
        commandArray.add(commandObject);
    }
    return commandArray.toString();
}
Also used : SlashCommand(at.xirado.bean.command.SlashCommand) DataArray(net.dv8tion.jda.api.utils.data.DataArray) List(java.util.List) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) Request(spark.Request) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) Response(spark.Response) SlashCommand(at.xirado.bean.command.SlashCommand) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(spark.Route) Bean(at.xirado.bean.Bean) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) DataObject(net.dv8tion.jda.api.utils.data.DataObject) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData)

Example 4 with SlashCommandData

use of net.dv8tion.jda.api.interactions.commands.build.SlashCommandData in project Discord-Core-Bot-Apple by demodude4u.

the class DiscordBot method buildUpdateCommands.

// Hold my beer
@SuppressWarnings("unchecked")
private void buildUpdateCommands(CommandListUpdateAction updateCommands) {
    Map<String, Object> root = new LinkedHashMap<>();
    for (SlashCommandDefinition command : commandSlash.values()) {
        String[] pathSplit = command.getPath().split("/");
        Map<String, Object> group = root;
        for (int i = 0; i < pathSplit.length; i++) {
            String name = pathSplit[i];
            if (i == pathSplit.length - 1) {
                group.put(name, command);
            } else {
                Object subGroup = group.get(name);
                if (subGroup == null) {
                    group.put(name, subGroup = new LinkedHashMap<String, Object>());
                }
                group = (Map<String, Object>) subGroup;
            }
        }
    }
    for (Entry<String, Object> rootEntry : root.entrySet()) {
        SlashCommandData commandData;
        if (rootEntry.getValue() instanceof SlashCommandDefinition) {
            SlashCommandDefinition commandDefinition = (SlashCommandDefinition) rootEntry.getValue();
            commandData = Commands.slash(rootEntry.getKey(), commandDefinition.getDescription());
            for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
                commandData = commandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
            }
        } else {
            Map<String, Object> sub = (Map<String, Object>) rootEntry.getValue();
            commandData = Commands.slash(rootEntry.getKey(), sub.keySet().stream().collect(Collectors.joining(", ")));
            for (Entry<String, Object> subEntry : sub.entrySet()) {
                if (subEntry.getValue() instanceof SlashCommandDefinition) {
                    SlashCommandDefinition commandDefinition = (SlashCommandDefinition) subEntry.getValue();
                    SubcommandData subcommandData = new SubcommandData(subEntry.getKey(), commandDefinition.getDescription());
                    for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
                        subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
                    }
                    commandData = commandData.addSubcommands(subcommandData);
                } else {
                    Map<String, SlashCommandDefinition> subSub = (Map<String, SlashCommandDefinition>) subEntry.getValue();
                    SubcommandGroupData subcommandGroupData = new SubcommandGroupData(subEntry.getKey(), subSub.keySet().stream().collect(Collectors.joining(", ")));
                    for (Entry<String, SlashCommandDefinition> subSubEntry : subSub.entrySet()) {
                        SubcommandData subcommandData = new SubcommandData(subSubEntry.getKey(), subSubEntry.getValue().getDescription());
                        for (SlashCommandOptionDefinition option : subSubEntry.getValue().getOptions()) {
                            subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
                        }
                        subcommandGroupData = subcommandGroupData.addSubcommands(subcommandData);
                    }
                    commandData = commandData.addSubcommandGroups(subcommandGroupData);
                }
            }
        }
        updateCommands = updateCommands.addCommands(commandData);
    }
    for (MessageCommandDefinition commandDefinition : commandMessage.values()) {
        CommandData commandData = Commands.message(commandDefinition.getName());
        updateCommands = updateCommands.addCommands(commandData);
    }
}
Also used : SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)

Aggregations

SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)4 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)3 List (java.util.List)2 Map (java.util.Map)2 Bean (at.xirado.bean.Bean)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 ISlashCommand (com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand)1 SlashCommandInteraction (com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)1 BaseCommand (dev.triumphteam.cmd.core.BaseCommand)1 Command (dev.triumphteam.cmd.core.Command)1 Default (dev.triumphteam.cmd.core.annotation.Default)1 ArgumentRegistry (dev.triumphteam.cmd.core.argument.ArgumentRegistry)1 NamedArgumentRegistry (dev.triumphteam.cmd.core.argument.named.NamedArgumentRegistry)1 CommandRegistrationException (dev.triumphteam.cmd.core.exceptions.CommandRegistrationException)1 ExecutionProvider (dev.triumphteam.cmd.core.execution.ExecutionProvider)1 MessageRegistry (dev.triumphteam.cmd.core.message.MessageRegistry)1 Registry (dev.triumphteam.cmd.core.registry.Registry)1 RequirementRegistry (dev.triumphteam.cmd.core.requirement.RequirementRegistry)1 SenderValidator (dev.triumphteam.cmd.core.sender.SenderValidator)1 ChoiceRegistry (dev.triumphteam.cmd.slash.choices.ChoiceRegistry)1