Skip to content

Type Adapters

TypeAdapters are a part of the execution chain. They are used to adapt the input of a slash command to the correct type needed to invoke the method.

Default Type Adapters

JDA-Commands provides the following type adapters by default:

  • all primitive types and their respective wrapper types
  • User
  • Member
  • Role
  • Channel and subtypes (e.g. StageChannel, NewsChannel, etc.)

You can add any of these types as a parameter to your slash command methods. See Command Options for details. These default types will be mapped to the most fitting option type. You can find the mapping here.

Writing Own Type Adapters

Example

@SlashCommand("example")
public void onCommand(CommandEvent event, CustomType object) {
    ...
}
@Implementation(clazz = CustomType.class)
public class UserProfileTypeAdapter implements TypeAdapter<CustomType> {

    public Optional<CustomType> apply(String raw, GenericInteractionCreateEvent event) {
        return Optional.of(new CustomType(raw, event));
    }

}

public class UserProfileTypeAdapter implements TypeAdapter<CustomType> {

    public Optional<CustomType> apply(String raw, GenericInteractionCreateEvent event) {
        return Optional.of(new CustomType(raw, event));
    }

}
JDACommands.builder(jda, Main.class)
    .adapter(CustomType.class, new UserProfileTypeAdapter());
    .start();

Tip

If your type adapter is simple enough, you could also just use lambda expressions:

JDACommands.builder(jda, Main.class)
    .adapter(CustomType.class, (raw, event) -> Optional.of(new CustomType(raw, event));
    .start();

Your own types will always be mapped to OptionType.STRING. If the type adapting fails (an empty Optional is returned) an error message will be sent to the user:

Type Adapter Error Message

You can customize this error message, find more about it here.