https://github.com/profiq/discord-interactions-elixir
Discord interactions API written in Elixir
https://github.com/profiq/discord-interactions-elixir
discord discord-api discord-app discord-bot elixir sdk-elixir
Last synced: 3 months ago
JSON representation
Discord interactions API written in Elixir
- Host: GitHub
- URL: https://github.com/profiq/discord-interactions-elixir
- Owner: profiq
- License: mit
- Created: 2025-04-23T08:09:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-12T08:21:16.000Z (12 months ago)
- Last Synced: 2025-06-12T09:32:14.972Z (12 months ago)
- Topics: discord, discord-api, discord-app, discord-bot, elixir, sdk-elixir
- Language: Elixir
- Homepage:
- Size: 129 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Discord Interactions
[](https://hex.pm/packages/discord_interactions)
[](https://hexdocs.pm/discord_interactions/)
[](https://hex.pm/packages/discord_interactions)
[](https://opensource.org/licenses/MIT)
An [Elixir](http://elixir-lang.org/) library for handling Discord interaction webhooks, which can be used to implement application commands
with simple chat responses, as well as more complex user interfaces using [components](https://discord.com/developers/docs/components/overview).
DiscordInteractions includes:
* A plug for handling incoming interaction requests, which implements [security header validation](https://discord.com/developers/docs/interactions/overview#handling-interactions)
* Automatic registration of application commands with Discord at startup
* Discord REST API client
* Modules for generating interaction responses, and component and embed payloads
## Installation
The package can be installed by adding `discord_interactions` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:discord_interactions, "~> 0.1.0"}
]
end
```
### Setting Up a Command Handler
1. **Create a Discord module** that will handle your commands:
```elixir
defmodule YourApp.Discord do
use DiscordInteractions
# Always require Logger for proper error reporting
require Logger
# Import response helpers
alias DiscordInteractions.InteractionResponse
interactions do
# Define a simple slash command
application_command "hello" do
description("Greets the user")
handler(&hello/1)
end
end
# Implement your command handler
def hello(itx) do
user_id = itx["member"]["user"]["id"]
response =
InteractionResponse.channel_message_with_source()
|> InteractionResponse.content("Hello, <@#{user_id}>!")
|> InteractionResponse.allowed_mentions(parse: [:users])
{:ok, response}
end
end
```
2. **Add the command registration to your application supervision tree**:
```elixir
# In your application.ex
def start(_type, _args) do
children = [
# Other children...
YourAppWeb.Endpoint,
{DiscordInteractions.CommandRegistration, YourApp.Discord}
]
opts = [strategy: :one_for_one, name: YourApp.Supervisor]
Supervisor.start_link(children, opts)
end
```
3. **Add the Discord Interactions plug to your router**:
```elixir
# In your router.ex
defmodule YourAppWeb.Router do
use YourAppWeb, :router
# Other routes...
# Route for Discord interactions
forward "/discord", DiscordInteractions.Plug, YourApp.Discord
end
```
4. **Configure your endpoint** to cache the raw request body for signature verification:
```elixir
# In your endpoint.ex
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library(),
body_reader: {DiscordInteractions.CacheBodyReader, :read_body, []}
```
5. **Set up environment variables** in your config:
```elixir
# In your config/runtime.exs or config/config.exs
config :discord_interactions,
public_key: System.get_env("DISCORD_PUBLIC_KEY"),
bot_token: System.get_env("DISCORD_BOT_TOKEN"),
application_id: System.get_env("DISCORD_APPLICATION_ID")
```
Documentation can be found at .
## Development
### Publishing to Hex.pm
This package is automatically published to [Hex.pm](https://hex.pm) when a new tag is pushed to the repository. The tag should follow the format `vX.Y.Z` (e.g., `v0.1.0`), and the version in the tag should match the version in `mix.exs`.
To publish a new version:
1. Update the version in `mix.exs`
2. Commit your changes
3. Create and push a new tag:
```bash
git tag v0.1.0
git push origin v0.1.0
```
The GitHub Action will automatically run tests and publish the package to Hex.pm if all tests pass.
**Note:** You need to set up the `HEX_API_KEY` secret in your GitHub repository settings. You can get your Hex.pm API key by running `mix hex.user key` or from your [Hex.pm dashboard](https://hex.pm/dashboard/keys).