Lingo.Context (Lingo v0.4.2)

Copy Markdown View Source

An interaction your bot is handling: a slash or context menu command, a component click, a modal submission or an autocomplete request.

Every interaction callback in Lingo.Bot receives one, and this module's functions are imported into modules that use Lingo.Bot.

Responding

Discord expects an answer within 3 seconds: reply/2, update/2, show_modal/2 or autocomplete_result/2, or defer/2 first to answer within 15 minutes. Each returns {:ok, ctx}, remembering that the interaction was answered so the next reply/2 sends a follow-up. The ! versions return the context or raise Lingo.Error.

def handle_command("slow", ctx) do
  ctx = defer!(ctx)
  reply!(ctx, "Done: #{do_slow_work()}")
end

A context remembers which bot it belongs to, so it can be answered from any process.

Reading input

Options you declared with Lingo.Bot.Commands.command/3 have atom keys and subcommands nest; modal fields are keyed by their custom id, as strings. option/2 and modal_value/2 accept either form.

Summary

Types

Message content: a string, or a keyword list or map of message fields.

t()

Functions

Answers autocomplete with up to 25 choices, maps or keyword lists with :name and :value.

Like autocomplete_result/2, but returns the context or raises Lingo.Error.

Acknowledges the interaction now and answers later. Does nothing if already deferred. Options: :ephemeral (only the user sees the answer) and :update (for components: the answer edits their message).

Like defer/2, but returns the context or raises Lingo.Error.

Deletes the response message.

Like delete_reply/1, but returns the context or raises Lingo.Error.

Sends a response only the user who triggered the interaction can see.

Like ephemeral/2, but returns the context or raises Lingo.Error.

The option the user is typing in, during autocomplete, as {name, value}.

The attachment an option points at.

The channel an option points at.

The guild member an option points at.

Fetches the response message: the first reply/2 or its edits, not follow-ups. Use its id with Lingo.Collector.await_component/2.

The role an option points at.

The user an option points at.

Launches the app's Activity, for entry point commands with the :app_handler handler.

Like launch_activity/1, but returns the context or raises Lingo.Error.

The value of a modal field, by its custom id. Same as option/2.

The value of a command option or modal field, or nil. For user, role, channel, mentionable and attachment options this is the id; get_user/2 and friends give you the struct.

Sends a message: the response, or a follow-up once answered. content is a string, or message fields such as :content, :embeds, :components, :files and :flags.

Like reply/2, but returns the context or raises Lingo.Error.

An attachment Discord sent along with the interaction.

A channel Discord sent along with the interaction.

A guild member Discord sent along with the interaction, by user id.

A message Discord sent along with the interaction; for message commands, ctx.target_id is the target.

A role Discord sent along with the interaction.

A user Discord sent along with the interaction.

Opens a modal built with Lingo.Component.Modal.modal/3. Must be the first response.

Like show_modal/2, but returns the context or raises Lingo.Error.

Edits the message a component is on, as the response to clicking it or after defer(ctx, update: true).

Like update/2, but returns the context or raises Lingo.Error.

Edits the response message. Ephemerality can't change after the first response.

Like update_reply/2, but returns the context or raises Lingo.Error.

Types

content()

@type content() :: String.t() | keyword() | map()

Message content: a string, or a keyword list or map of message fields.

t()

@type t() :: %Lingo.Context{
  app_permissions: String.t() | nil,
  application_id: String.t(),
  bot: atom() | nil,
  channel_id: String.t() | nil,
  command_name: String.t() | nil,
  component_type: atom() | integer() | nil,
  custom_id: String.t() | nil,
  deferred: boolean(),
  guild_id: String.t() | nil,
  guild_locale: String.t() | nil,
  interaction: Lingo.Type.Interaction.t() | nil,
  interaction_id: String.t(),
  interaction_token: String.t(),
  locale: String.t() | nil,
  member: Lingo.Type.Member.t() | nil,
  message: Lingo.Type.Message.t() | nil,
  options: %{optional(atom() | String.t()) => term()},
  raw_options: [map()] | nil,
  replied: boolean(),
  resolved: map() | nil,
  target_id: String.t() | nil,
  type: Lingo.Type.Interaction.interaction_type(),
  user: Lingo.Type.User.t() | nil,
  user_id: String.t() | nil,
  values: [String.t()]
}

Functions

autocomplete_result(ctx, choices)

Answers autocomplete with up to 25 choices, maps or keyword lists with :name and :value.

autocomplete_result!(ctx, choices)

Like autocomplete_result/2, but returns the context or raises Lingo.Error.

defer(ctx, opts \\ [])

Acknowledges the interaction now and answers later. Does nothing if already deferred. Options: :ephemeral (only the user sees the answer) and :update (for components: the answer edits their message).

defer!(ctx, opts \\ [])

Like defer/2, but returns the context or raises Lingo.Error.

delete_reply(ctx)

Deletes the response message.

delete_reply!(ctx)

Like delete_reply/1, but returns the context or raises Lingo.Error.

ephemeral(ctx, content)

Sends a response only the user who triggered the interaction can see.

ephemeral!(ctx, content)

Like ephemeral/2, but returns the context or raises Lingo.Error.

focused_option(ctx)

The option the user is typing in, during autocomplete, as {name, value}.

get_attachment(ctx, name)

The attachment an option points at.

get_channel(ctx, name)

The channel an option points at.

get_member(ctx, name)

The guild member an option points at.

get_reply(ctx)

Fetches the response message: the first reply/2 or its edits, not follow-ups. Use its id with Lingo.Collector.await_component/2.

get_role(ctx, name)

The role an option points at.

get_user(ctx, name)

The user an option points at.

launch_activity(ctx)

Launches the app's Activity, for entry point commands with the :app_handler handler.

launch_activity!(ctx)

Like launch_activity/1, but returns the context or raises Lingo.Error.

option(ctx, name)

The value of a command option or modal field, or nil. For user, role, channel, mentionable and attachment options this is the id; get_user/2 and friends give you the struct.

reply(ctx, content)

Sends a message: the response, or a follow-up once answered. content is a string, or message fields such as :content, :embeds, :components, :files and :flags.

reply!(ctx, content)

Like reply/2, but returns the context or raises Lingo.Error.

resolved_attachment(ctx, attachment_id)

An attachment Discord sent along with the interaction.

resolved_channel(ctx, channel_id)

A channel Discord sent along with the interaction.

resolved_member(ctx, user_id)

A guild member Discord sent along with the interaction, by user id.

resolved_message(ctx, message_id)

A message Discord sent along with the interaction; for message commands, ctx.target_id is the target.

resolved_role(ctx, role_id)

A role Discord sent along with the interaction.

resolved_user(ctx, user_id)

A user Discord sent along with the interaction.

show_modal(ctx, modal)

Opens a modal built with Lingo.Component.Modal.modal/3. Must be the first response.

show_modal!(ctx, modal)

Like show_modal/2, but returns the context or raises Lingo.Error.

update(ctx, content)

Edits the message a component is on, as the response to clicking it or after defer(ctx, update: true).

update!(ctx, content)

Like update/2, but returns the context or raises Lingo.Error.

update_reply(ctx, content)

Edits the response message. Ephemerality can't change after the first response.

update_reply!(ctx, content)

Like update_reply/2, but returns the context or raises Lingo.Error.