💎 ChatSDK Ruby

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

VariableDescription
DISCORD_BOT_TOKENBot token from the Discord Developer Portal
DISCORD_PUBLIC_KEYApplication public key for verifying interaction signatures
DISCORD_APPLICATION_IDApplication ID from the Discord Developer Portal

Discord Setup

  1. Create an application in the Discord Developer Portal.
  2. Create a bot user under the Bot section and copy the bot token.
  3. Copy the public key from the General Information page.
  4. Set the Interactions Endpoint URL to your webhook endpoint (e.g., https://your-app.com/webhooks/discord).
  5. Register slash commands via the Discord API or Developer Portal.
  6. Invite the bot to your server with appropriate permissions.

Webhook URL

https://your-domain.com/webhooks/discord
map "/webhooks/discord" do
  run bot.webhooks[:discord]
end

Capabilities

CapabilitySupportedNotes
edit_messagesYes
delete_messagesYes
ephemeral_messagesNoRaises NotSupportedError
file_uploadsYesMultipart upload
reactionsYesURL-encoded emoji
modalsNoRaises NotSupportedError
typing_indicatorYesVia POST /channels/{id}/typing
streaming_editYes
threadsYes
direct_messagesYesCreates DM channels via /users/@me/channels
message_historyYesPaginated 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  # ApiClient

Mention 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)
end

Gateway 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 response

Returns 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")

On this page