Search in sources :

Example 11 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project JDA by DV8FromTheWorld.

the class CommandDataTest method testNormal.

@Test
public void testNormal() {
    CommandData command = new CommandDataImpl("ban", "Ban a user from this server").setDefaultEnabled(false).addOption(OptionType.USER, "user", "The user to ban", // required before non-required
    true).addOption(OptionType.STRING, "reason", // test that default is false
    "The ban reason").addOption(OptionType.INTEGER, "days", "The duration of the ban", // test with explicit false
    false);
    DataObject data = command.toData();
    Assertions.assertEquals("ban", data.getString("name"));
    Assertions.assertEquals("Ban a user from this server", data.getString("description"));
    Assertions.assertFalse(data.getBoolean("default_permission"));
    DataArray options = data.getArray("options");
    DataObject option = options.getObject(0);
    Assertions.assertTrue(option.getBoolean("required"));
    Assertions.assertEquals("user", option.getString("name"));
    Assertions.assertEquals("The user to ban", option.getString("description"));
    option = options.getObject(1);
    Assertions.assertFalse(option.getBoolean("required"));
    Assertions.assertEquals("reason", option.getString("name"));
    Assertions.assertEquals("The ban reason", option.getString("description"));
    option = options.getObject(2);
    Assertions.assertFalse(option.getBoolean("required"));
    Assertions.assertEquals("days", option.getString("name"));
    Assertions.assertEquals("The duration of the ban", option.getString("description"));
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) DataArray(net.dv8tion.jda.api.utils.data.DataArray) Test(org.junit.jupiter.api.Test)

Example 12 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project TJ-Bot by Together-Java.

the class SlashCommandAdapterTest method getData.

@Test
void getData() {
    SlashCommandAdapter adapter = createAdapter();
    CommandData data = adapter.getData();
    assertEquals(NAME, data.getName(), "adapters name is inconsistent with the base data object");
    assertEquals(DESCRIPTION, data.getDescription(), "adapters description is inconsistent with the base data object");
    // Check that the method does not return a new data instance and does not mess with it
    String otherName = NAME + "-bar";
    String otherDescription = DESCRIPTION + "-bar";
    data.setName(otherName).setDescription(otherDescription);
    CommandData otherData = adapter.getData();
    assertSame(data, otherData, "adapter changed the data object");
    assertEquals(otherName, otherData.getName(), "name changes did not carry over");
    assertEquals(otherDescription, otherData.getDescription(), "description changes did not carry over");
}
Also used : CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Test(org.junit.jupiter.api.Test)

Example 13 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Utility by anweisen.

the class SlashCommandHelper method acquireCommand.

@Nonnull
private IBaseCommandData<?, ?> acquireCommand(@Nonnull RegisteredCommand command) {
    String name = command.getOptions().getFirstName();
    List<String> subcommandNames = new ArrayList<>(Arrays.asList(name.split(" ")));
    String parentName = subcommandNames.get(0);
    ICommandData parentRootCommand = slashCommands.computeIfAbsent(parentName, key -> new ICommandData(parentName));
    switch(subcommandNames.size()) {
        default:
            throw new IllegalArgumentException("Discord only supports 3 chained command names for subcommands in slashcommands (got " + subcommandNames.size() + ": '" + name + "')");
        case 1:
            return parentRootCommand;
        case 2:
            {
                String commandName = subcommandNames.get(1);
                ISubcommandData commandData = new ISubcommandData(commandName);
                parentRootCommand.addSubcommand(commandData);
                return commandData;
            }
        case 3:
            {
                String subCommandName = subcommandNames.get(1);
                ISubcommandGroupData subcommandGroup = parentRootCommand.getSubcommandGroups().stream().filter(data -> data.getName().equals(subCommandName)).findFirst().orElseGet(() -> {
                    ISubcommandGroupData result = new ISubcommandGroupData(subCommandName);
                    parentRootCommand.addSubcommandGroup(result);
                    return result;
                });
                String commandName = subcommandNames.get(2);
                ISubcommandData commandData = new ISubcommandData(commandName);
                subcommandGroup.addSubcommand(commandData);
                return commandData;
            }
    }
}
Also used : PrintWriter(java.io.PrintWriter) IBaseCommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.IBaseCommandData) java.util(java.util) JDA(net.dv8tion.jda.api.JDA) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) ISubcommandGroupData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandGroupData) WrappedException(net.anweisen.utilities.common.collection.WrappedException) RequiredArgument(net.anweisen.utilities.jda.manager.hooks.registered.RequiredArgument) RegisteredCommand(net.anweisen.utilities.jda.manager.hooks.registered.RegisteredCommand) ICommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ICommandData) ISubcommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandData) CommandManager(net.anweisen.utilities.jda.manager.CommandManager) Nonnull(javax.annotation.Nonnull) StringBuilderPrintWriter(net.anweisen.utilities.common.collection.StringBuilderPrintWriter) ISubcommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandData) ISubcommandGroupData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ISubcommandGroupData) ICommandData(net.anweisen.utilities.jda.manager.impl.slashcommands.build.ICommandData) Nonnull(javax.annotation.Nonnull)

Example 14 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Utility by anweisen.

the class ICommandData method convert.

@Nonnull
public CommandData convert() {
    CommandData data = new CommandData(name, " ");
    try {
        options.forEach(option -> data.addOptions(option));
        subcommands.forEach(subcommand -> data.addSubcommands(subcommand.convert()));
        subcommandGroups.forEach(group -> data.addSubcommandGroups(group.convert()));
    } catch (IllegalArgumentException ex) {
        throw new WrappedException("Cannot convert '" + name + "'", ex);
    }
    return data;
}
Also used : WrappedException(net.anweisen.utilities.common.collection.WrappedException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Nonnull(javax.annotation.Nonnull)

Example 15 with CommandData

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

the class InteractionHandler method getContextCommandData.

/**
 * Gets the complete {@link CommandData} from a single {@link BaseContextCommand}.
 *
 * @param command      The base context command's instance.
 * @param commandClass The base context command's class.
 * @return The new {@link CommandListUpdateAction}.
 */
private CommandData getContextCommandData(@NotNull BaseContextCommand command, Class<? extends BaseContextCommand> commandClass) {
    if (command.getCommandData() == null) {
        DIH4JDALogger.warn(String.format("Class %s is missing CommandData. It will be ignored.", commandClass.getName()));
        return null;
    }
    CommandData commandData = command.getCommandData();
    if (commandData.getType() == Command.Type.MESSAGE) {
        messageContextIndex.put(commandData.getName(), new MessageContextInteraction((IMessageContextCommand) command));
    } else if (commandData.getType() == Command.Type.USER) {
        userContextIndex.put(commandData.getName(), new UserContextInteraction((IUserContextCommand) command));
    } else {
        DIH4JDALogger.error(String.format("Invalid Command Type \"%s\" for Context Command! This command will be ignored.", commandData.getType()));
        return null;
    }
    DIH4JDALogger.info(String.format("\t[*] Registered context command: %s", command.getCommandData().getName()), DIH4JDALogger.Type.CONTEXT_COMMAND_REGISTERED);
    return commandData;
}
Also used : UserContextInteraction(com.dynxsty.dih4jda.commands.interactions.context_command.UserContextInteraction) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) MessageContextInteraction(com.dynxsty.dih4jda.commands.interactions.context_command.MessageContextInteraction) IMessageContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand)

Aggregations

CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)20 ArrayList (java.util.ArrayList)6 JDA (net.dv8tion.jda.api.JDA)5 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)5 Guild (net.dv8tion.jda.api.entities.Guild)4 List (java.util.List)3 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)3 InteractionCommandArgument (net.essentialsx.api.v2.services.discord.InteractionCommandArgument)3 BaseContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand)2 Nonnull (javax.annotation.Nonnull)2 WrappedException (net.anweisen.utilities.common.collection.WrappedException)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)2 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)2 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)2 InteractionCommand (net.essentialsx.api.v2.services.discord.InteractionCommand)2 Test (org.junit.jupiter.api.Test)2 IMessageContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand)1 MessageContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.MessageContextInteraction)1 UserContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.UserContextInteraction)1