DiscordInteractions.API (discord_interactions v0.1.0)

View Source

Discord REST API client.

Summary

Functions

Batch updates permissions for multiple commands in a guild.

Bulk overwrites all global commands.

Bulk overwrites all commands in a guild.

Creates a followup message for an interaction.

Creates a new global command.

Creates a new command in a guild.

Perform a DELETE request.

Perform a DELETE request.

Deletes a followup message for an interaction.

Deletes a global command.

Deletes a command from a guild.

Deletes the initial response to an interaction.

Edits a followup message for an interaction.

Edits the initial response to an interaction.

Perform a GET request.

Perform a GET request.

Gets permissions for a specific command in a guild.

Gets a followup message for an interaction.

Gets a specific global command by ID.

Gets all global commands for the application.

Gets a specific command in a guild.

Gets permissions for all commands in a guild.

Gets all commands for a specific guild.

Gets the initial response to an interaction.

Perform a HEAD request.

Perform a HEAD request.

Creates a new API client with the given token.

Perform a OPTIONS request.

Perform a OPTIONS request.

Perform a PATCH request.

Perform a PATCH request.

Perform a POST request.

Perform a POST request.

Perform a PUT request.

Perform a PUT request.

Perform request and raise in case of error.

Perform a TRACE request.

Perform a TRACE request.

Updates permissions for a specific command in a guild.

Updates an existing global command.

Updates an existing command in a guild.

Types

application_id()

@type application_id() :: String.t()

client()

@type client() :: term()

command()

@type command() :: map()

command_id()

@type command_id() :: String.t()

commands()

@type commands() :: [command()]

error()

@type error() :: {:error, any()}

guild_id()

@type guild_id() :: String.t()

interaction_callback_response()

@type interaction_callback_response() :: map()

interaction_id()

@type interaction_id() :: String.t()

interaction_response()

@type interaction_response() :: map()

interaction_token()

@type interaction_token() :: String.t()

message()

@type message() :: map()

message_id()

@type message_id() :: String.t()

option()

@type option() ::
  {:method, Tesla.Env.method()}
  | {:url, Tesla.Env.url()}
  | {:query, Tesla.Env.query()}
  | {:headers, Tesla.Env.headers()}
  | {:body, Tesla.Env.body()}
  | {:opts, Tesla.Env.opts()}

Options that may be passed to a request function. See request/2 for detailed descriptions.

permissions()

@type permissions() :: map()

Functions

batch_update_command_permissions(map, guild_id, permissions)

@spec batch_update_command_permissions(client(), guild_id(), [permissions()]) ::
  {:ok, [permissions()]} | error()

Batch updates permissions for multiple commands in a guild.

Examples

iex> permissions = [
...>   %{id: "CMD_ID_1", permissions: [%{id: "ROLE_ID", type: 1, permission: true}]},
...>   %{id: "CMD_ID_2", permissions: [%{id: "USER_ID", type: 2, permission: true}]}
...> ]
iex> {:ok, updated} = DiscordInteractions.Client.batch_update_command_permissions(client, "GUILD_ID", permissions)

bulk_overwrite_global_commands(map, commands)

@spec bulk_overwrite_global_commands(client(), commands()) ::
  {:ok, commands()} | error()

Bulk overwrites all global commands.

Examples

iex> commands = [%{name: "cmd1", description: "Command 1"}, %{name: "cmd2", description: "Command 2"}]
iex> {:ok, updated} = DiscordInteractions.Client.bulk_overwrite_global_commands(client, commands)

bulk_overwrite_guild_commands(map, guild_id, commands)

@spec bulk_overwrite_guild_commands(client(), guild_id(), commands()) ::
  {:ok, commands()} | error()

Bulk overwrites all commands in a guild.

Examples

iex> commands = [%{name: "cmd1", description: "Command 1"}, %{name: "cmd2", description: "Command 2"}]
iex> {:ok, updated} = DiscordInteractions.Client.bulk_overwrite_guild_commands(client, "GUILD_ID", commands)

create_followup_message(map, interaction_token, message)

@spec create_followup_message(client(), interaction_token(), message()) ::
  {:ok, message()} | error()

Creates a followup message for an interaction.

Examples

iex> message = %{content: "Followup message"}
iex> {:ok, created} = DiscordInteractions.Client.create_followup_message(client, "INTERACTION_TOKEN", message)

create_global_command(map, command)

@spec create_global_command(client(), command()) :: {:ok, command()} | error()

Creates a new global command.

Examples

iex> command = %{name: "test", description: "A test command", type: 1}
iex> {:ok, created} = DiscordInteractions.Client.create_global_command(client, command)

create_guild_command(map, guild_id, command)

@spec create_guild_command(client(), guild_id(), command()) ::
  {:ok, command()} | error()

Creates a new command in a guild.

Examples

iex> command = %{name: "test", description: "A test command", type: 1}
iex> {:ok, created} = DiscordInteractions.Client.create_guild_command(client, "GUILD_ID", command)

create_interaction_response(map, interaction_id, interaction_token, response)

@spec create_interaction_response(
  client(),
  interaction_id(),
  interaction_token(),
  interaction_response()
) :: :ok | {:ok, interaction_callback_response()} | error()

Creates an initial response to an interaction.

Examples

iex> response = %{type: 4, data: %{content: "Hello!"}}
iex> :ok = DiscordInteractions.Client.create_interaction_response(client, "INTERACTION_ID", "INTERACTION_TOKEN", response)

delete(client, url, opts)

@spec delete(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()

Perform a DELETE request.

See request/1 or request/2 for options definition.

delete("/users")
delete("/users", query: [scope: "admin"])
delete(client, "/users")
delete(client, "/users", query: [scope: "admin"])
delete(client, "/users", body: %{name: "Jon"})

delete!(client, url, opts)

@spec delete!(Tesla.Env.client(), Tesla.Env.url(), [option()]) ::
  Tesla.Env.t() | no_return()

Perform a DELETE request.

See request!/1 or request!/2 for options definition.

delete!("/users")
delete!("/users", query: [scope: "admin"])
delete!(client, "/users")
delete!(client, "/users", query: [scope: "admin"])
delete!(client, "/users", body: %{name: "Jon"})

delete_followup_message(map, interaction_token, message_id)

@spec delete_followup_message(client(), interaction_token(), message_id()) ::
  :ok | error()

Deletes a followup message for an interaction.

Examples

iex> :ok = DiscordInteractions.Client.delete_followup_message(client, "INTERACTION_TOKEN", "MESSAGE_ID")

delete_global_command(map, command_id)

@spec delete_global_command(client(), command_id()) :: :ok | error()

Deletes a global command.

Examples

iex> :ok = DiscordInteractions.Client.delete_global_command(client, "CMD_ID")

delete_guild_command(map, guild_id, command_id)

@spec delete_guild_command(client(), guild_id(), command_id()) :: :ok | error()

Deletes a command from a guild.

Examples

iex> :ok = DiscordInteractions.Client.delete_guild_command(client, "GUILD_ID", "CMD_ID")

delete_original_interaction_response(map, interaction_token)

@spec delete_original_interaction_response(client(), interaction_token()) ::
  :ok | error()

Deletes the initial response to an interaction.

Examples

iex> :ok = DiscordInteractions.Client.delete_original_interaction_response(client, "INTERACTION_TOKEN")

edit_followup_message(map, interaction_token, message_id, message)

@spec edit_followup_message(client(), interaction_token(), message_id(), message()) ::
  {:ok, message()} | error()

Edits a followup message for an interaction.

Examples

iex> message = %{content: "Updated followup"}
iex> {:ok, updated} = DiscordInteractions.Client.edit_followup_message(client, "INTERACTION_TOKEN", "MESSAGE_ID", message)

edit_original_interaction_response(map, interaction_token, message)

@spec edit_original_interaction_response(client(), interaction_token(), message()) ::
  {:ok, message()} | error()

Edits the initial response to an interaction.

Examples

iex> message = %{content: "Updated content"}
iex> {:ok, updated} = DiscordInteractions.Client.edit_original_interaction_response(client, "INTERACTION_TOKEN", message)

get(client, url, opts)

Perform a GET request.

See request/1 or request/2 for options definition.

get("/users")
get("/users", query: [scope: "admin"])
get(client, "/users")
get(client, "/users", query: [scope: "admin"])
get(client, "/users", body: %{name: "Jon"})

get!(client, url, opts)

@spec get!(Tesla.Env.client(), Tesla.Env.url(), [option()]) ::
  Tesla.Env.t() | no_return()

Perform a GET request.

See request!/1 or request!/2 for options definition.

get!("/users")
get!("/users", query: [scope: "admin"])
get!(client, "/users")
get!(client, "/users", query: [scope: "admin"])
get!(client, "/users", body: %{name: "Jon"})

get_command_permissions(map, guild_id, command_id)

@spec get_command_permissions(client(), guild_id(), command_id()) ::
  {:ok, permissions()} | error()

Gets permissions for a specific command in a guild.

Examples

iex> {:ok, permissions} = DiscordInteractions.Client.get_command_permissions(client, "GUILD_ID", "CMD_ID")

get_followup_message(map, interaction_token, message_id)

@spec get_followup_message(client(), interaction_token(), message_id()) ::
  {:ok, message()} | error()

Gets a followup message for an interaction.

Examples

iex> {:ok, message} = DiscordInteractions.Client.get_followup_message(client, "INTERACTION_TOKEN", "MESSAGE_ID")

get_global_command(map, command_id)

@spec get_global_command(client(), command_id()) :: {:ok, command()} | error()

Gets a specific global command by ID.

Examples

iex> {:ok, command} = DiscordInteractions.Client.get_global_command(client, "CMD_ID")

get_global_commands(map)

@spec get_global_commands(client()) :: {:ok, commands()} | error()

Gets all global commands for the application.

Examples

iex> {:ok, commands} = DiscordInteractions.Client.get_global_commands(client)

get_guild_command(map, guild_id, command_id)

@spec get_guild_command(client(), guild_id(), command_id()) ::
  {:ok, command()} | error()

Gets a specific command in a guild.

Examples

iex> {:ok, command} = DiscordInteractions.Client.get_guild_command(client, "GUILD_ID", "CMD_ID")

get_guild_command_permissions(map, guild_id)

@spec get_guild_command_permissions(client(), guild_id()) ::
  {:ok, [permissions()]} | error()

Gets permissions for all commands in a guild.

Examples

iex> {:ok, permissions} = DiscordInteractions.Client.get_guild_command_permissions(client, "GUILD_ID")

get_guild_commands(map, guild_id)

@spec get_guild_commands(client(), guild_id()) :: {:ok, commands()} | error()

Gets all commands for a specific guild.

Examples

iex> {:ok, commands} = DiscordInteractions.Client.get_guild_commands(client, "GUILD_ID")

get_original_interaction_response(map, interaction_token)

@spec get_original_interaction_response(client(), interaction_token()) ::
  {:ok, message()} | error()

Gets the initial response to an interaction.

Examples

iex> {:ok, message} = DiscordInteractions.Client.get_original_interaction_response(client, "INTERACTION_TOKEN")

head(client, url, opts)

Perform a HEAD request.

See request/1 or request/2 for options definition.

head("/users")
head("/users", query: [scope: "admin"])
head(client, "/users")
head(client, "/users", query: [scope: "admin"])
head(client, "/users", body: %{name: "Jon"})

head!(client, url, opts)

@spec head!(Tesla.Env.client(), Tesla.Env.url(), [option()]) ::
  Tesla.Env.t() | no_return()

Perform a HEAD request.

See request!/1 or request!/2 for options definition.

head!("/users")
head!("/users", query: [scope: "admin"])
head!(client, "/users")
head!(client, "/users", query: [scope: "admin"])
head!(client, "/users", body: %{name: "Jon"})

new(opts)

@spec new(keyword()) :: client()

Creates a new API client with the given token.

Examples

iex> client = DiscordInteractions.Client.new(application_id: "APP_ID", token: "BOT_TOKEN")

options(client, url, opts)

@spec options(Tesla.Env.client(), Tesla.Env.url(), [option()]) :: Tesla.Env.result()

Perform a OPTIONS request.

See request/1 or request/2 for options definition.

options("/users")
options("/users", query: [scope: "admin"])
options(client, "/users")
options(client, "/users", query: [scope: "admin"])
options(client, "/users", body: %{name: "Jon"})

options!(client, url, opts)

@spec options!(Tesla.Env.client(), Tesla.Env.url(), [option()]) ::
  Tesla.Env.t() | no_return()

Perform a OPTIONS request.

See request!/1 or request!/2 for options definition.

options!("/users")
options!("/users", query: [scope: "admin"])
options!(client, "/users")
options!(client, "/users", query: [scope: "admin"])
options!(client, "/users", body: %{name: "Jon"})

patch(client, url, body, opts)

Perform a PATCH request.

See request/1 or request/2 for options definition.

patch("/users", %{name: "Jon"})
patch("/users", %{name: "Jon"}, query: [scope: "admin"])
patch(client, "/users", %{name: "Jon"})
patch(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

patch!(client, url, body, opts)

Perform a PATCH request.

See request!/1 or request!/2 for options definition.

patch!("/users", %{name: "Jon"})
patch!("/users", %{name: "Jon"}, query: [scope: "admin"])
patch!(client, "/users", %{name: "Jon"})
patch!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

post(client, url, body, opts)

Perform a POST request.

See request/1 or request/2 for options definition.

post("/users", %{name: "Jon"})
post("/users", %{name: "Jon"}, query: [scope: "admin"])
post(client, "/users", %{name: "Jon"})
post(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

post!(client, url, body, opts)

Perform a POST request.

See request!/1 or request!/2 for options definition.

post!("/users", %{name: "Jon"})
post!("/users", %{name: "Jon"}, query: [scope: "admin"])
post!(client, "/users", %{name: "Jon"})
post!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

put(client, url, body, opts)

Perform a PUT request.

See request/1 or request/2 for options definition.

put("/users", %{name: "Jon"})
put("/users", %{name: "Jon"}, query: [scope: "admin"])
put(client, "/users", %{name: "Jon"})
put(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

put!(client, url, body, opts)

Perform a PUT request.

See request!/1 or request!/2 for options definition.

put!("/users", %{name: "Jon"})
put!("/users", %{name: "Jon"}, query: [scope: "admin"])
put!(client, "/users", %{name: "Jon"})
put!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

request(client \\ %Tesla.Client{}, options)

@spec request(Tesla.Env.client(), [option()]) :: Tesla.Env.result()

Perform a request.

Options

  • :method - the request method, one of [:head, :get, :delete, :trace, :options, :post, :put, :patch]
  • :url - either full url e.g. "http://example.com/some/path" or just "/some/path" if using Tesla.Middleware.BaseUrl
  • :query - a keyword list of query params, e.g. [page: 1, per_page: 100]
  • :headers - a keyword list of headers, e.g. [{"content-type", "text/plain"}]
  • :body - depends on used middleware:
    • by default it can be a binary
    • if using e.g. JSON encoding middleware it can be a nested map
    • if adapter supports it it can be a Stream with any of the above
  • :opts - custom, per-request middleware or adapter options

Examples

ExampleApi.request(method: :get, url: "/users/path")

# use shortcut methods
ExampleApi.get("/users/1")
ExampleApi.post(client, "/users", %{name: "Jon"})

request!(client \\ %Tesla.Client{}, options)

@spec request!(Tesla.Env.client(), [option()]) :: Tesla.Env.t() | no_return()

Perform request and raise in case of error.

This is similar to request/2 behaviour from Tesla 0.x

See request/2 for list of available options.

trace(client, url, opts)

Perform a TRACE request.

See request/1 or request/2 for options definition.

trace("/users")
trace("/users", query: [scope: "admin"])
trace(client, "/users")
trace(client, "/users", query: [scope: "admin"])
trace(client, "/users", body: %{name: "Jon"})

trace!(client, url, opts)

@spec trace!(Tesla.Env.client(), Tesla.Env.url(), [option()]) ::
  Tesla.Env.t() | no_return()

Perform a TRACE request.

See request!/1 or request!/2 for options definition.

trace!("/users")
trace!("/users", query: [scope: "admin"])
trace!(client, "/users")
trace!(client, "/users", query: [scope: "admin"])
trace!(client, "/users", body: %{name: "Jon"})

update_command_permissions(map, guild_id, command_id, permissions)

@spec update_command_permissions(client(), guild_id(), command_id(), permissions()) ::
  {:ok, permissions()} | error()

Updates permissions for a specific command in a guild.

Examples

iex> permissions = %{permissions: [%{id: "ROLE_ID", type: 1, permission: true}]}
iex> {:ok, updated} = DiscordInteractions.Client.update_command_permissions(client, "GUILD_ID", "CMD_ID", permissions)

update_global_command(map, command_id, command)

@spec update_global_command(client(), command_id(), command()) ::
  {:ok, command()} | error()

Updates an existing global command.

Examples

iex> command = %{name: "test", description: "Updated description", type: 1}
iex> {:ok, updated} = DiscordInteractions.Client.update_global_command(client, "CMD_ID", command)

update_guild_command(map, guild_id, command_id, command)

@spec update_guild_command(client(), guild_id(), command_id(), command()) ::
  {:ok, command()} | error()

Updates an existing command in a guild.

Examples

iex> command = %{name: "test", description: "Updated description", type: 1}
iex> {:ok, updated} = DiscordInteractions.Client.update_guild_command(client, "GUILD_ID", "CMD_ID", command)