Discord Adapter
Discord REST API v10 adapter with Ed25519 signature verification and embed rendering via the chat_sdk-discord gem.
The Discord adapter (chat_sdk-discord) integrates with Discord via its REST API v10 and Interactions endpoint with Ed25519 signature verification.
Installation
# Gemfile
gem "chat_sdk"
gem "chat_sdk-discord"Configuration
require "chat_sdk"
require "chat_sdk/discord"
discord = ChatSDK::Discord::Adapter.new(
bot_token: ENV["DISCORD_BOT_TOKEN"], # Bot token from Discord Developer Portal
public_key: ENV["DISCORD_PUBLIC_KEY"], # For Ed25519 signature verification
application_id: ENV["DISCORD_APPLICATION_ID"] # Application ID
)bot_token is required. public_key is required for signature verification. application_id is optional but recommended.
Environment Variables
| Variable | Description |
|---|---|
DISCORD_BOT_TOKEN | Bot token from the Discord Developer Portal |
DISCORD_PUBLIC_KEY | Application public key for verifying interaction signatures |
DISCORD_APPLICATION_ID | Application ID from the Discord Developer Portal |
Discord Setup
- Create an application in the Discord Developer Portal.
- Create a bot user under the Bot section and copy the bot token.
- Copy the public key from the General Information page.
- Set the Interactions Endpoint URL to your webhook endpoint (e.g.,
https://your-app.com/webhooks/discord). - Register slash commands via the Discord API or Developer Portal.
- Invite the bot to your server with appropriate permissions.
Webhook URL
https://your-domain.com/webhooks/discordmap "/webhooks/discord" do
run bot.webhooks[:discord]
endCapabilities
| Capability | Supported | Notes |
|---|---|---|
edit_messages | Yes | |
delete_messages | Yes | |
ephemeral_messages | No | Raises NotSupportedError |
file_uploads | Yes | Multipart upload |
reactions | Yes | URL-encoded emoji |
modals | No | Raises NotSupportedError |
typing_indicator | Yes | Via POST /channels/{id}/typing |
streaming_edit | Yes | |
threads | Yes | |
direct_messages | Yes | Creates DM channels via /users/@me/channels |
message_history | Yes | Paginated channel message retrieval |
Signature Verification
Discord requires Ed25519 signature verification for all interaction requests. The adapter uses the ed25519 gem to verify signatures using the X-Signature-Ed25519 and X-Signature-Timestamp headers.
Cards Rendering
Cards are rendered as Discord embeds with interactive components (buttons, select menus). Components are wrapped in action rows as required by the Discord API.
Direct Client Access
discord_adapter = bot.adapter(:discord)
client = discord_adapter.client # ApiClientMention Formatting
Discord mentions use the <@user_id> format:
discord_adapter.mention("123456789") # => "<@123456789>"Gateway Mode
For real-time event streaming over WebSocket (instead of HTTP interactions), use the Discord Gateway. Requires the optional discordrb gem.
# Gemfile
gem "discordrb"
# Connect to the Gateway and receive events
discord.start_gateway do |event|
# event is a ChatSDK::Events::Mention
handle(event)
endGateway mode is useful for bots that need to react to all messages in a server without requiring slash commands or the Interactions endpoint.
Get User
Look up a Discord user by ID:
author = discord.get_user("123456789")
author.name # => "alice"
author.bot? # => false
author.raw # => full Discord API responseReturns a ChatSDK::Author or nil if the user is not found.
Fetch Thread Info
Get channel or thread metadata:
info = discord.fetch_thread(channel_id: "ch123")
info["name"] # => "general"
info["type"] # => 0 (guild text), 11 (public thread), etc.Thread Management
Create a thread from a message and rename threads:
thread = discord.create_thread(channel_id: "ch123", message_id: "msg-1", name: "incident-42")
discord.set_thread_title(channel_id: thread["id"], title: "resolved-incident-42")