Skip to main content
Build a Discord bot with tools and persistent memory.
1

Create the bot

  1. Go to the Discord Developer Portal
  2. Click New Application and name it
  3. Go to Bot and click Add Bot
  4. Enable MESSAGE_CONTENT under Privileged Gateway Intents
  5. Copy the bot token
  6. Use the OAuth2 URL Generator to invite the bot (select bot scope with Send Messages and Read Message History)
2

Create the agent

discord_bot.py
import asyncio
import os
from definable.agent import Agent
from definable.agent.interface import DiscordInterface
from definable.memory import Memory, SQLiteStore
from definable.tool.decorator import tool

@tool
def search_docs(query: str) -> str:
    """Search documentation for an answer."""
    return f"Found results for: {query}"

agent = Agent(
    model="gpt-4o",
    instructions="You are a helpful Discord bot.",
    tools=[search_docs],
    memory=Memory(store=SQLiteStore("./discord_memory.db")),
)

discord = DiscordInterface(
    agent=agent,
    bot_token=os.environ["DISCORD_BOT_TOKEN"],
)

asyncio.run(discord.serve_forever())
3

Install dependencies

pip install 'definable[discord]'
4

Set environment variables

export DISCORD_BOT_TOKEN="your-bot-token"
export OPENAI_API_KEY="sk-..."
5

Run

python discord_bot.py
Mention the bot or send a DM to start chatting.
See Discord reference for all configuration options.