gaato/discord/interaction does not have a README file

CommandModel

pub(open) trait CommandModel {
fn from_options(CommandOptions) -> Self raise
}

A typed view of one command invocation, decoded from its submitted options.

Implement this by hand per command (MoonBit has no derive macro for it):

struct Echo {
text : String
times : Int64?
}

impl @interaction.CommandModel for Echo with fn from_options(options) {
{ text: options.string("text"), times: options.int_opt("times"), }
}

Commands with subcommands are naturally modeled as an enum whose from_options matches on options.path().

ArgsDecodeError

pub(all) suberror ArgsDecodeError {
Validation(name~ : String, message~ : String)
} derive(
Debug
)

Errors introduced by typed argument combinators.

OptionError

pub(all) suberror OptionError {
MissingOption(name~ : String)
WrongType(name~ : String, expected~ : String)
MissingResolved(name~ : String, kind~ : String)
} derive(
Debug
)

Errors raised while reading typed values out of submitted command options.

Arg

pub struct Arg[A] {
// private fields
}

One typed command option, combining its registration definition and reader.

Arg::map

fn[A, B] Arg::map(self : Arg[A], f : (A) -> B) -> Arg[B]

Transform one decoded argument without changing its registration definition.

Arg::optional

fn[A] Arg::optional(self : Arg[A]) -> Arg[A?]

Make an argument optional. A missing submitted option decodes to None.

test "make a command option optional" {
let args = @interaction.Args::of(
@interaction.arg_string(name="note", description="Optional note").optional(),
)
json_inspect(args.definitions(), content=[
{
"type": 3,
"name": "note",
"description": "Optional note",
"required": false,
},
])
}

Arg::validate

fn[A] Arg::validate(self : Arg[A], check : (A) -> String?) -> Arg[A]

Validate a decoded argument. Returning Some(message) rejects the value.

Arg::with_default

fn[A] Arg::with_default(self : Arg[A], value : A) -> Arg[A]

Make an argument optional in the registration payload and use a default value when it is absent from a submitted command.

Args

pub struct Args[A] {
// private fields
}

A typed collection of command options used for registration and decoding.

Args::custom

fn[A] Args::custom(definitions~ : Array[
CommandOption
], decode~ : (CommandOptions) -> A raise) -> Args[A]

Build an argument collection from custom definitions and a custom decoder.

Args::decode

fn[A] Args::decode(self : Args[A], input : CommandOptions) -> A raise

Decode submitted command options into the composed value.

Args::definitions

Return the option definitions in registration order.

Args::map

fn[A, B] Args::map(self : Args[A], f : (A) -> B) -> Args[B]

Transform the result of a composed argument collection.

Args::map1

fn[A, Z] Args::map1(a : Arg[A], f : (A) -> Z) -> Args[Z]

Build a command's argument decoder from one option. The option is registered from a's definition and f maps its decoded value into the handler's argument type.

Args::map2

fn[A, B, Z] Args::map2(a : Arg[A], b : Arg[B], f : (A, B) -> Z) -> Args[Z]

Build a command's argument decoder from two options, combining their decoded values with the final function argument.

Args::map3

fn[A, B, C, Z] Args::map3(a : Arg[A], b : Arg[B], c : Arg[C], f : (A, B, C) -> Z) -> Args[Z]

Build a command's argument decoder from three options, combining their decoded values with the final function argument.

Args::map4

fn[A, B, C, D, Z] Args::map4(a : Arg[A], b : Arg[B], c : Arg[C], d : Arg[D], f : (A, B, C, D) -> Z) -> Args[Z]

Build a command's argument decoder from four options, combining their decoded values with the final function argument.

Args::map5

fn[A, B, C, D, E, Z] Args::map5(a : Arg[A], b : Arg[B], c : Arg[C], d : Arg[D], e : Arg[E], f : (A, B, C, D, E) -> Z) -> Args[Z]

Build a command's argument decoder from five options, combining their decoded values with the final function argument.

Args::map6

fn[A, B, C, D, E, F, Z] Args::map6(a : Arg[A], b : Arg[B], c : Arg[C], d : Arg[D], e : Arg[E], f : Arg[F], combine : (A, B, C, D, E, F) -> Z) -> Args[Z]

Build a command's argument decoder from six options, combining their decoded values with the final function argument.

Args::map7

fn[A, B, C, D, E, F, G, Z] Args::map7(a : Arg[A], b : Arg[B], c : Arg[C], d : Arg[D], e : Arg[E], f : Arg[F], g : Arg[G], combine : (A, B, C, D, E, F, G) -> Z) -> Args[Z]

Build a command's argument decoder from seven options, combining their decoded values with the final function argument.

Args::map8

fn[A, B, C, D, E, F, G, H, Z] Args::map8(a : Arg[A], b : Arg[B], c : Arg[C], d : Arg[D], e : Arg[E], f : Arg[F], g : Arg[G], h : Arg[H], combine : (A, B, C, D, E, F, G, H) -> Z) -> Args[Z]

Build a command's argument decoder from eight options, combining their decoded values with the final function argument.

Args::of

fn[A] Args::of(arg : Arg[A]) -> Args[A]

Lift one argument into a composable argument collection.

test "compose defaulting and validation into an argument spec" {
let args = @interaction.Args::of(
@interaction.arg_int(
name="times",
description="Repeat count",
min=1L,
max=5L,
)
.with_default(1L)
.validate(value => if value <= 5L { None } else { Some("too large") }),
)
json_inspect(args.definitions(), content=[
{
"type": 4,
"name": "times",
"description": "Repeat count",
"required": false,
"min_value": 1,
"max_value": 5,
},
])
}

Args::suggestions

fn[A] Args::suggestions(self : Args[A]) -> Array[SuggestHandler]

Return the type-erased autocomplete handlers carried by these arguments.

Args::unit

fn Args::unit() -> Args[Unit]

Define a command with no options.

Args::zip

fn[A, B] Args::zip(self : Args[A], other : Args[B]) -> Args[(A, B)]

Combine two argument collections, preserving left-to-right definition and decoding order.

CommandOptions

pub struct CommandOptions {
// private fields
}

Typed reader over the options submitted with an application command.

Subcommand and subcommand-group levels are flattened away on construction: path() reports the traversed names and the value accessors see only the leaf options. Required accessors raise OptionError; _opt variants return None when the option is absent but still raise if a present option has an unexpected shape.

CommandOptions::attachment

Look up an attachment option through the interaction's resolved data.

CommandOptions::bool

fn CommandOptions::bool(self : CommandOptions, name : String) -> Bool raise OptionError

The required BOOLEAN option name; raises OptionError when absent or not a boolean.

CommandOptions::bool_opt

fn CommandOptions::bool_opt(self : CommandOptions, name : String) -> Bool? raise OptionError

The optional BOOLEAN option name, or None when absent.

CommandOptions::channel

The required CHANNEL option name as a ChannelId; pass it to resolved_channel for the partial channel object.

CommandOptions::channel_opt

The optional CHANNEL option name as a ChannelId, or None when absent.

CommandOptions::focused

fn CommandOptions::focused(self : CommandOptions) -> String?

The name of the option currently focused for autocomplete, if any.

CommandOptions::focused_input

fn CommandOptions::focused_input(self : CommandOptions) -> SuggestInput?

The focused option and its raw partial value, when present.

CommandOptions::from_data

Build a typed reader from submitted command data, flattening any subcommand / subcommand-group nesting into path().

CommandOptions::has

fn CommandOptions::has(self : CommandOptions, name : String) -> Bool

Whether an option named name was submitted.

CommandOptions::int

fn CommandOptions::int(self : CommandOptions, name : String) -> Int64 raise OptionError

The required INTEGER option name; raises OptionError when absent or not an integer.

CommandOptions::int_opt

fn CommandOptions::int_opt(self : CommandOptions, name : String) -> Int64? raise OptionError

The optional INTEGER option name, or None when absent.

CommandOptions::mentionable

The required MENTIONABLE option name as an untyped snowflake; check resolved_user / resolved_role to learn which kind it is.

CommandOptions::mentionable_opt

The optional MENTIONABLE option name, or None when absent.

CommandOptions::number

fn CommandOptions::number(self : CommandOptions, name : String) -> Double raise OptionError

The required NUMBER option name; raises OptionError when absent or not a number.

CommandOptions::number_opt

fn CommandOptions::number_opt(self : CommandOptions, name : String) -> Double? raise OptionError

The optional NUMBER option name, or None when absent.

CommandOptions::path

fn CommandOptions::path(self : CommandOptions) -> Array[String]

The subcommand-group / subcommand names traversed during flattening, outermost first (empty for a plain command).

CommandOptions::resolved_channel

The resolved partial Channel for a channel option, when available.

CommandOptions::resolved_member

The resolved partial GuildMember for a user option, when available.

CommandOptions::resolved_role

The resolved Role for a role option, when available.

CommandOptions::resolved_user

The resolved User for a user option, when Discord included it.

CommandOptions::role

The required ROLE option name as a RoleId; pass it to resolved_role for the full role object.

CommandOptions::role_opt

The optional ROLE option name as a RoleId, or None when absent.

CommandOptions::string

fn CommandOptions::string(self : CommandOptions, name : String) -> String raise OptionError

The required STRING option name; raises OptionError when absent or not a string.

CommandOptions::string_opt

fn CommandOptions::string_opt(self : CommandOptions, name : String) -> String? raise OptionError

The optional STRING option name, or None when absent.

CommandOptions::user

The required USER option name as a UserId; pass it to resolved_user / resolved_member for the full objects.

CommandOptions::user_opt

The optional USER option name as a UserId, or None when absent.

CommandSpec

pub(all) struct CommandSpec {
name : String
name_localizations : Map[String, String]?
description : String
description_localizations : Map[String, String]?
typ :
ApplicationCommandType

options : Array[
CommandOption
]
default_member_permissions :
Permissions
?
nsfw : Bool?
integration_types : Array[
ApplicationIntegrationType
]?
contexts : Array[
InteractionContextType
]?
} derive(
Debug
)

A declarative description of one application command, used both to register the command (its ToJson is the registration payload) and to route incoming interactions by command type and name.

CommandSpec::message

fn CommandSpec::message(name : String, default_member_permissions? :
Permissions
, nsfw? : Bool, integration_types? : Array[
ApplicationIntegrationType
], contexts? : Array[
InteractionContextType
], name_localizations? : Map[String, String]) -> CommandSpec

Describe a MESSAGE context-menu command (no description, no options).

CommandSpec::slash

fn CommandSpec::slash(name : String, description : String, options? : Array[
CommandOption
], default_member_permissions? :
Permissions
, nsfw? : Bool, integration_types? : Array[
ApplicationIntegrationType
], contexts? : Array[
InteractionContextType
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> CommandSpec

Describe a CHAT_INPUT (slash) command.

CommandSpec::user

fn CommandSpec::user(name : String, default_member_permissions? :
Permissions
, nsfw? : Bool, integration_types? : Array[
ApplicationIntegrationType
], contexts? : Array[
InteractionContextType
], name_localizations? : Map[String, String]) -> CommandSpec

Describe a USER context-menu command (no description, no options).

SuggestCtx

pub(all) struct SuggestCtx {
interaction :
Interaction

options : CommandOptions
guild_id :
Id
[
GuildMarker
]?
locale : String?
}

Data available while producing autocomplete choices. This context is transport-neutral and intentionally does not own an HTTP client.

SuggestHandler

pub struct SuggestHandler {
// private fields
}

A type-erased autocomplete handler retained by Args.

SuggestHandler::name

fn SuggestHandler::name(self : SuggestHandler) -> String

The option name served by this handler.

SuggestHandler::suggest

Produce choices from a focused raw input.

SuggestInput

pub struct SuggestInput {
// private fields
}

The raw focused option passed to a type-erased suggestion handler.

SuggestInput::name

fn SuggestInput::name(self : SuggestInput) -> String

The name of the focused option.

action_row

Build a message component row.

arg_attachment

fn arg_attachment(name~ : String, description~ : String, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[
Attachment
]

Define a required attachment option resolved to its attachment object.

arg_bool

fn arg_bool(name~ : String, description~ : String, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[Bool]

Define a required boolean option.

arg_channel

fn arg_channel(name~ : String, description~ : String, channel_types? : Array[
ChannelType
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[
Id
[
ChannelMarker
]]

Define a required channel option.

arg_int

fn arg_int(name~ : String, description~ : String, min? : Int64, max? : Int64, choices? : Array[
CommandOptionChoice
], suggest? : async (SuggestCtx, Int64) -> Array[
CommandOptionChoice
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[Int64]

Define a required integer option.

arg_mentionable

fn arg_mentionable(name~ : String, description~ : String, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[
Id
[
GenericMarker
]]

Define a required mentionable option.

arg_number

fn arg_number(name~ : String, description~ : String, min? : Double, max? : Double, choices? : Array[
CommandOptionChoice
], suggest? : async (SuggestCtx, Double) -> Array[
CommandOptionChoice
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[Double]

Define a required floating-point number option.

arg_role

fn arg_role(name~ : String, description~ : String, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[
Id
[
RoleMarker
]]

Define a required role option.

arg_string

fn arg_string(name~ : String, description~ : String, choices? : Array[
CommandOptionChoice
], min_length? : Int, max_length? : Int, suggest? : async (SuggestCtx, String) -> Array[
CommandOptionChoice
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[String]

Define a required string option.

arg_user

fn arg_user(name~ : String, description~ : String, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) -> Arg[
Id
[
UserMarker
]]

Define a required user option.

attachment_option

fn attachment_option(name : String, description : String, required? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe an ATTACHMENT option.

boolean_option

fn boolean_option(name : String, description : String, required? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a BOOLEAN option.

button

fn button(custom_id~ : String, label? : String, style? :
ButtonStyle
, emoji? :
Emoji
, disabled? : Bool) ->
Component

Build an interactive button.

channel_option

fn channel_option(name : String, description : String, required? : Bool, channel_types? : Array[
ChannelType
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a CHANNEL option.

channel_select

fn channel_select(custom_id~ : String, placeholder? : String, min_values? : Int, max_values? : Int, disabled? : Bool, channel_types? : Array[
ChannelType
]) ->
Component

Build a channel select menu, optionally restricted to channel_types; the chosen channels arrive via the component context's selected_channels.

container

fn container(components~ : Array[
Component
], accent_color? : Int, spoiler? : Bool) ->
Component

Build a container (components v2): groups child components in a rounded box with an optional accent_color (0xRRGGBB) stripe, similar to an embed. Reply and send layers set the required IS_COMPONENTS_V2 message flag automatically.

int_choice

fn int_choice(name : String, value : Int64, name_localizations? : Map[String, String]) ->
CommandOptionChoice

Describe an integer-valued option choice.

integer_option

fn integer_option(name : String, description : String, required? : Bool, choices? : Array[
CommandOptionChoice
], min_value? : Int64, max_value? : Int64, autocomplete? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe an INTEGER option.

label

fn label(label~ : String, component~ :
Component
, description? : String) ->
Component

Wrap a modal component with a label and optional description. Modals require every input to be wrapped in a label; the typed ModalField builders do this automatically.
fn link_button(url~ : String, label? : String, emoji? :
Emoji
, disabled? : Bool) ->
Component

Build a link button.

Build a media gallery (components v2): a grid of 1–10 media items. Reply and send layers set the required IS_COMPONENTS_V2 message flag automatically.

media_item

fn media_item(url~ : String, description? : String, spoiler? : Bool) ->
MediaGalleryItem

Build one media gallery entry from an image or video url (https://... or attachment://<filename>), for use with media_gallery.

mentionable_option

fn mentionable_option(name : String, description : String, required? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a MENTIONABLE option.

mentionable_select

fn mentionable_select(custom_id~ : String, placeholder? : String, min_values? : Int, max_values? : Int, disabled? : Bool) ->
Component

Build a mentionable select menu accepting both users and roles.

number_choice

fn number_choice(name : String, value : Double, name_localizations? : Map[String, String]) ->
CommandOptionChoice

Describe a number-valued option choice.

number_option

fn number_option(name : String, description : String, required? : Bool, choices? : Array[
CommandOptionChoice
], min_value? : Double, max_value? : Double, autocomplete? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a NUMBER option.

role_option

fn role_option(name : String, description : String, required? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a ROLE option.

role_select

fn role_select(custom_id~ : String, placeholder? : String, min_values? : Int, max_values? : Int, disabled? : Bool) ->
Component

Build a role select menu; the chosen roles arrive via the component context's selected_roles.

section

Build a section (components v2): up to three text displays laid out next to an accessory (a thumbnail or button). Reply and send layers set the required IS_COMPONENTS_V2 message flag automatically.

select_option

fn select_option(label~ : String, value~ : String, description? : String, emoji? :
Emoji
, default? : Bool) ->
SelectOption

Build an option for a string select menu.

separator

fn separator(divider? : Bool, spacing? : Int) ->
Component

Build a separator (components v2): vertical padding between components, with an optional visible divider line and spacing size (1 = small, 2 = large). Reply and send layers set the required IS_COMPONENTS_V2 message flag automatically.

string_choice

fn string_choice(name : String, value : String, name_localizations? : Map[String, String]) ->
CommandOptionChoice

Describe a string-valued option choice.

string_option

fn string_option(name : String, description : String, required? : Bool, choices? : Array[
CommandOptionChoice
], min_length? : Int, max_length? : Int, autocomplete? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a STRING option.

string_select

fn string_select(custom_id~ : String, options~ : Array[
SelectOption
], placeholder? : String, min_values? : Int, max_values? : Int, disabled? : Bool) ->
Component

Build a string select menu.

sub_command

fn sub_command(name : String, description : String, options? : Array[
CommandOption
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a SUB_COMMAND option.

sub_command_group

fn sub_command_group(name : String, description : String, sub_commands : Array[
CommandOption
], name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a SUB_COMMAND_GROUP option.

text_display

fn text_display(content : String) ->
Component

Build a text display block (components v2): markdown content rendered in the message body. Reply and send layers set the required IS_COMPONENTS_V2 message flag automatically.

text_input

fn text_input(custom_id~ : String, style? :
TextInputStyle
, placeholder? : String, min_length? : Int, max_length? : Int, required? : Bool, value? : String) ->
Component

Build a modal text input. style picks single-line Short (default) or multi-line Paragraph; value prefills the field. Wrap it with label before placing it in a modal (the typed ModalField builders handle this).

thumbnail

fn thumbnail(url~ : String, description? : String, spoiler? : Bool) ->
Component

Build a thumbnail accessory (components v2) from an image url (https://... or attachment://<filename>).

user_option

fn user_option(name : String, description : String, required? : Bool, name_localizations? : Map[String, String], description_localizations? : Map[String, String]) ->
CommandOption

Describe a USER option.

user_select

fn user_select(custom_id~ : String, placeholder? : String, min_values? : Int, max_values? : Int, disabled? : Bool) ->
Component

Build a user select menu; the chosen users arrive in resolved and via the component context's selected_users.