EDA.Command (EDA v0.3.0)

Copy Markdown View Source

Builder for Discord Application Commands (slash commands, user commands, message commands).

Provides a pipe-friendly API with eager validation against Discord's limits.

Example

import EDA.Command
import EDA.Command.Option

# Simple slash command
ping = slash("ping", "Pings the bot")

# Command with options
greet =
  slash("greet", "Greets someone")
  |> option(string("message", "The greeting", required: true))
  |> option(user("target", "Who to greet"))
  |> option(integer("times", "How many times", min_value: 1, max_value: 10))

# Command with subcommands
role =
  slash("role", "Manage roles")
  |> option(
    sub_command("add", "Add a role", [
      role("role", "The role to add", required: true),
      user("target", "The user")
    ])
  )
  |> option(
    sub_command("remove", "Remove a role", [
      role("role", "The role to remove", required: true)
    ])
  )

# Context menu commands
info = user_command("User Info")
quote_msg = message_command("Quote Message")

# Register
EDA.API.Command.create_guild(guild_id, greet)
EDA.API.Command.bulk_overwrite_guild(guild_id, [ping, greet, role])

Summary

Functions

Sets the interaction contexts where the command can be used.

Sets the default member permissions required to use this command.

Adds localized name and/or description for a given locale.

Creates a message context menu command (type 3).

Marks the command as age-restricted (NSFW).

Adds an option to the command (max 25 options).

Creates a new slash command (CHAT_INPUT, type 1).

Converts the command struct to a plain map for the Discord API.

Creates a user context menu command (type 2).

Types

command_type()

@type command_type() :: :slash | :user | :message

context()

@type context() :: :guild | :bot_dm | :private_channel

t()

@type t() :: %EDA.Command{
  contexts: [non_neg_integer()] | nil,
  default_member_permissions: String.t() | nil,
  description: String.t() | nil,
  description_localizations: map() | nil,
  name: String.t(),
  name_localizations: map() | nil,
  nsfw: boolean(),
  options: [EDA.Command.Option.t()],
  type: 1 | 2 | 3
}

Functions

contexts(cmd, ctx_list)

@spec contexts(t(), [context()]) :: t()

Sets the interaction contexts where the command can be used.

Accepts a list of :guild, :bot_dm, :private_channel.

Example

slash("ping", "Pong") |> contexts([:guild, :bot_dm])

default_member_permissions(cmd, perms)

@spec default_member_permissions(t(), String.t()) :: t()

Sets the default member permissions required to use this command.

Accepts a permission bitfield as a string (e.g. "8" for Administrator). Use "0" to disable for everyone except admins.

localize(cmd, locale, opts)

@spec localize(t(), String.t(), keyword()) :: t()

Adds localized name and/or description for a given locale.

Discord locale codes: "fr", "de", "es-ES", "ja", "pt-BR", etc. See Discord docs for the full list.

Examples

slash("ping", "Pings the bot")
|> localize("fr", name: "ping", description: "Ping le bot")
|> localize("de", description: "Pingt den Bot")

message_command(name)

@spec message_command(String.t()) :: t()

Creates a message context menu command (type 3).

Name must be 1-32 characters. Mixed case and spaces allowed.

nsfw(cmd, value \\ true)

@spec nsfw(t(), boolean()) :: t()

Marks the command as age-restricted (NSFW).

option(cmd, opt)

@spec option(t(), EDA.Command.Option.t()) :: t()

Adds an option to the command (max 25 options).

Only valid for slash commands (type 1).

slash(name, description)

@spec slash(String.t(), String.t()) :: t()

Creates a new slash command (CHAT_INPUT, type 1).

Name must be 1-32 lowercase characters (a-z, 0-9, -, _). Description must be 1-100 characters.

to_map(cmd)

@spec to_map(t()) :: map()

Converts the command struct to a plain map for the Discord API.

user_command(name)

@spec user_command(String.t()) :: t()

Creates a user context menu command (type 2).

Name must be 1-32 characters. Mixed case and spaces allowed.