Converters
Converters are a core part of the Kord Extensions command framework. They provide a way to convert arguments provided by Discord into rich data types, handling parsing and validation, and making sure you get the types you expect.
Converter Types
As converters support multiple parsing strategies, they're divided into specific types. Each type handles its arguments in a different way, and some are limited to specific types of commands:
Type | Commands | Description |
---|---|---|
Single | All | The default converter type, which represents a single, mandatory command argument. |
Defaulting | All | A converter type that represents an optional command argument, with a specified default value. The default value will be used when no argument is provided. |
Optional | All | A converter type that represents an optional command argument, with a default value of |
Coalescing | All | A converter type that attempts to consume as many arguments as possible, combining them into a single value. This converter type will stop at the first argument that it can't parse, passing control to the next converter. Note: Coalescing converters will only ever parse a single value when used in a slash command, as Discord does not support argument types that accept multiple values. |
List | All | A converter type that acts like a coalescing converter, but which returns a list of parsed arguments instead of a single combined value. Note: List converters will only ever parse a single value when used in a slash command, as Discord does not support argument types that accept multiple values. |
Choice | All | A converter type that represents a single argument that is limited to a predefined set of specific choices. When used with Slash Commands, user input will be limited to the given choices. |
Usage
Converters provide builder functions that you can use to define your command arguments. These functions are available for use within classes that extend Arguments
, as briefly explained on the commands overview page.
Once you've created your arguments class, you'll need to figure out which converter functions you'll need. You can figure out the name of the relevant function by following these rules:
Single converters simply use the name of the argument type — for example,
string
,int
,message
.List converters use the single converter name with the
List
suffix — for example,stringList
,intList
,messageList
.All other converter types add prefixes to the single converter name — for example,
coalescingString
,defaultingInt
,optionalMessage
.Defaulting and optional converters may be combined with other types, with an additional suffix added to the single converter name — for example,
coalescingOptionalString
.
Converter Settings
All converters must be specified via delegation, as explained in the example above. Each converter builder function exposes a set of options that can be used to customize the behavior of the converter in question.
Common Settings
Properties
Name | Type | Description | |
---|---|---|---|
Required Properties | |||
|
| Required: Key object representing a short description of the argument, which explains what it's for. | |
|
| Required: Key object representing the argument's name. For Chat Commands, this name is displayed in help commands and used to refer to the argument by name when specified as a keyword argument. For Slash Commands, this name is displayed within the Discord client, where it's used to identify the argument. |
Auto-Completion
When working with slash commands, Discord supports the ability to provide suggestions to the user when they're filling out specific arguments. You can use the autoComplete
builder when defining your argument if you wish to support this.
The autoComplete
builder is a receiver function against the AutoCompleteInteraction
type, with an event
argument of the AutoCompleteInteractionCreateEvent
type. A number of APIs are available to make things easier.
Functions
All the below functions are used to suggest potential argument values to the user. They automatically limit themselves to suggesting up to the maximum possible number of values, and they support the following arguments:
strategy
- the filtering strategy to use, based on the data the user has entered so far. While it's possible to define your own, the following are provided:FilterStrategy.Contains
- Filter options based on whether they contain the provided data.FilterStrategy.Prefix
- Filter options based on whether they start with the provided data.FilterStrategy.Suffix
- Filter options based on whether they end with the provided data.
suggestInputWithoutMatches
- When the given filtering strategy doesn't match anything, whether to suggest the user's provided data as a possible option. This may be useful for arguments that represent a form of data that will be created if an existing value isn't found.Defaults to
false
.
Name | Description |
---|---|
| Use a collection (such as a list) of doubles to suggest potential argument values to the user. |
| Use a map of strings to doubles to suggest potential argument values to the user. The string keys are displayed to the user, and mapped to their corresponding value when the argument is filled. They should be a human-readable representation of the given value. |
| Use a collection (such as a list) of integers to suggest potential argument values to the user. |
| Use a map of strings to integers to suggest potential argument values to the user. The string keys are displayed to the user, and mapped to their corresponding value when the argument is filled. They should be a human-readable representation of the given value. |
| Use a collection (such as a list) of longs to suggest potential argument values to the user. |
| Use a map of strings to longs to suggest potential argument values to the user. The string keys are displayed to the user, and mapped to their corresponding value when the argument is filled. They should be a human-readable representation of the given value. |
| Use a collection (such as a list) of doubles (Discord's default number type) to suggest potential argument values to the user. |
| Use a map of strings to doubles (Discord's default number type) to suggest potential argument values to the user. The string keys are displayed to the user, and mapped to their corresponding value when the argument is filled. They should be a human-readable representation of the given value. |
| Use a collection (such as a list) of strings to suggest potential argument values to the user. |
| Use a map of string keys to values to suggest potential argument values to the user. The string keys are displayed to the user, and mapped to their corresponding value when the argument is filled. They should be a human-readable representation of the given value. |
Properties
Name | Type | Description |
---|---|---|
|
| The argument that's currently being focused by the user. You can use this property to retrieve the data the user has entered for this argument so far. |
Mutators
Mutators allow you to modify the argument's value after it's been filled. This is only useful in a limited number of use-cases, but may be handy for providing light customizations for existing converter types.
To define a mutator, call the mutate
builder function. This builder takes the existing value as its first argument, expecting the modified value to be the return value.
No additional APIs are provided.
Validators
Validators allow you to validate the argument's value after it's been filled, potentially returning an error to the user if the value fails to validate.
To define a validator, call the validate
builder function. This builder is a receiver function against the ValidationContext
type, which provides an API similar to that of a check context.
Functions
Name | Description |
---|---|
| Mark this validator as having failed, optionally providing a message for the user. |
| If the given value (or result of the given callback) is |
| If the given value (or result of the given callback) is |
| Mark this validator as having passed successfully. As this is the default state of a validator, this function is only useful when the validator may have failed previously. |
| If the given value (or result of the given callback) is |
| If the given value (or result of the given callback) is |
| Call the given block only when the |
| Call the given block only when the |
Properties
Name | Type | Description |
---|---|---|
|
| Context object representing the current command execution. |
|
| Translation Key object, which is used to format the error message when validation fails. A plain string may not work here, as a single replacement value (the error message) must be inserted by the translation system. Defaults to |
|
| Human-readable error message. This message is usually provided by the relevant API functions and isn't usually specified manually. Defaults to |
|
| Whether this validator has passed. This property is modified by the relevant API functions, and isn't usually modified manually. |
|
| The current argument's value. |
Choice Converters
Choice converters allow you to specify a set of predefined command options via a simple API.
Functions
Name | Description |
---|---|
| Add a choice. This function takes a Key object, which will be shown to the user on Discord when using Slash Commands. When the command is submitted, the corresponding |
| If you have a pre-defined map of choices, this function will use that map as the predefined argument options. This will replace any choices that may have been defined previously. This map's Key objects must represent a human-readable representation of their corresponding values, as they'll be shown to the user on Discord. If preferred, the |
Coalescing Converters
Properties
Name | Type | Description |
---|---|---|
|
| Whether to ignore errors thrown while attempting to parse a given value, assuming no values have been parsed yet. This is |
Defaulting Converters
Properties
Name | Type | Description |
---|---|---|
|
| Value to use when the user doesn't provide this argument, or when it fails to parse and |
|
| Whether to ignore errors thrown while attempting to parse a given value. This is |
List Converters
Properties
Name | Type | Description |
---|---|---|
|
| Whether to ignore errors thrown while attempting to parse a given value, assuming no values have been parsed yet. This is |
Optional Converters
Properties
Name | Type | Description |
---|---|---|
|
| Whether to ignore errors thrown while attempting to parse a given value. This is |
Bundled Converters
Kord Extensions provides a number of converters for common data types, which you can make use of in your bots.
Type | Description | |
---|---|---|
General Converters | ||
Boolean | Boolean argument, with support for translated values.
| |
Color | Color argument, supporting hex codes prefixed with The values returned by this converter will be Kord Color objects. Converter functions matching both US English ("color") and UK English ("colour") are available. Translated color names are also supported. | |
Decimal | Decimal argument, parsing the provided value into a Double.
| |
Duration | Duration argument, using a complex parsing strategy to transform the provided value into a DateTimePeriod object. This object is timezone-agnostic, and may need to be normalized against a specific timezone for some use-cases. This is also available as a coalescing converter.
In addition to Discord-formatted timestamps, this converter supports specifying durations as a series of units and durations.
| |
Email argument, validated using the Apache Commons email validator. | ||
Enum | Arbitrary enum argument, allowing for the specification of enum value names. By default, enum value names will be matched case-insensitively. This is also available as a choice converter. The enum to use is specified by providing a generic type parameter to the converter's builder function. This will be referred to as
| |
Int | Integer argument, parsing the provided value into an Int.
| |
Long | Long argument, parsing the provided value into a Long. This is also available as a choice converter, as a converter named
| |
Regex | Regex argument, allowing users to specify a regular expression.
| |
Serialized | Serialized argument, converting values to the given type using
| |
String | String argument, providing users' input verbatim. This is also available as a choice converter or coalescing converter.
| |
Supported Locale | Supported locale argument, allowing users to provide one of Kord Extensions' supported locales. This converter will return a If a locale you need isn't supported, please feel free to contribute translations for it. | |
Discord Entities | ||
Channel | Discord channel argument, supporting mentions, IDs, and names. Also supports "this" to refer to the current channel. When working with slash commands, the Discord client resolves the channel object instead of the bot. This means that the converter will only ever return the channel object provided by the client, and cannot resolve the special name "this." It will also ignore the
| |
Emoji | Emoji argument, supporting Unicode emojis, guild emoji IDs, guild emoji names, and Unicode emoji names as used by Discord's emoji picker. Guild emoji IDs may be specified with or without surrounding colons, or specified using the emoji mentions that the Discord client inserts into messages. When a guild emoji is specified, it must come from a guild the bot is present on. If a guild emoji is specified only by name, the first emoji the bot finds from its guilds is used. | |
Guild | Discord guild argument, supporting IDs and names. Also supports "this" to refer to the current guild. The guild specified must be a guild the bot is present on. | |
Member | Discord guild member argument, supporting mentions, IDs, usernames, tags, and names. Also supports "me" to refer to the member running the command, and "you" to refer to the bot. When working with slash commands, the Discord client resolves the member object instead of the bot. This means that the converter will only ever return the member object provided by the client, and cannot resolve the special names "me" or "you."
| |
Message | Discord message argument, supporting message jump URLs and IDs. When a message ID is specified, it's assumed to be in the same channel the command was executed within. Messages will be retrieved if they're not already in Kord's cache, which requires the bot to be able to see the message in question. When using a URL, the bot cannot resolve messages in DMs.
| |
Role | Discord role argument, supporting role mentions, names, and IDs. Role resolution via commands run in DMs is not supported. When working with slash commands, the Discord client resolves the role object instead of the bot. This means that the converter will only ever return the role object provided by the client, and the
| |
Snowflake | Discord snowflake argument. Parses the given value into a | |
Tag | Discord forum channel tag argument. Provides built-in Auto-Completion support for tags based on the current channel or the
| |
Timestamp | Discord timestamp argument. Parses a Discord-formatted timestamp into a This converter only supports Discord-formatted timestamps, and is unable to parse any other form of timestamp. | |
User | Discord user argument supporting mentions, IDs, usernames, and tags. Also supports "me" to refer to the member running the command, and "you" to refer to the bot. When working with slash commands, the Discord client resolves the user object instead of the bot. This means that the converter will only ever return the user object provided by the client, and cannot resolve the special names "me" or "you."
| |
Slash Commands Only | ||
Attachment | A file attachment, as provided in a slash command invocation. Discord will display a drop target for this type of argument. |