> For the complete documentation index, see [llms.txt](https://docs.stammer.ai/stammer.ai-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stammer.ai/stammer.ai-docs/chat-ai-agents/integrations/discord.md).

# Discord

4 Items are Needed for this tutorial:

1. Discord Bot Token
2. API Key from Stammer.ai
3. Chatbot UUID from Stammer.ai
4. (Optional) White Label URL from Stammer.ai

{% embed url="<https://youtu.be/7m4nf-1A2kA>" %}

### Instructions

1\. Set up Discord account and server:\
&#x20;  \- Create a Discord account (if you don’t have one)\
&#x20;  \- Create a new server or use an existing one

2\. Create Discord application:\
&#x20;  \- Go to <https://discord.com/developers/applications>\
&#x20;  \- Click “New Application”\
&#x20;  \- Name your application\
&#x20;  \- Go to “Bot” tab and click “Add Bot”

3\. Get bot token:\
&#x20;  \- In “Bot” tab, find token section\
&#x20;  \- Click “Copy” to copy bot token to paste into Replit\
&#x20;  \- Keep this token secret!

4\. Invite bot to server:\
&#x20;  \- Go to “OAuth2" tab\
&#x20;  \- In “Scopes” section, select “bot”\
&#x20;  \- Choose desired permissions\
&#x20;  \- Copy generated URL and open in new tab\
&#x20;  \- Select server and authorize bot on the channels you want it to access

5\. Create New Replit:\
&#x20;  \- Create new Python file\
&#x20;  \- Paste in the template code\
&#x20;  \- Paste in the Discord bot code on line 68\
&#x20;  \- Update the Stammer Authorization key on line 10\
&#x20;  \- Update the chatbot UUID on line 11\
&#x20;  \- (Optional) Update the URL to be your white label API URL on line 14

6\. Running the AI Agent:\
&#x20;  \- Run the code in Replit\
&#x20;  \- Go to your Discord channel and ask your bot a question by tagging its name

**Code for Replit:**

```python
import discord
from discord.ext import commands
import requests

intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix='!', intents=intents)

stoken = 'YOUR-STAMMER-KEY'
chatbot_uuid = 'SOME_UUID'

def send_message_to_ai_agent(message):
    url = "https://app.stammer.ai/en/chatbot/api/v1/message/"
    headers = {
        'Authorization': f'Token {stoken}',
        'Content-Type': 'application/json'
    }
    data = {
        "chatbot_uuid": chatbot_uuid,
        "query": message,
        "user_key": "discord_user",  # You can change this if needed
    }

    try:
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()

        if response.status_code == 200:
            return response.json().get('data', {}).get('answer', 'No response from AI agent.')
        else:
            return f"Unexpected response: {response.text}"

    except requests.exceptions.RequestException as e:
        return f"Error: {str(e)}"

@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')

@bot.event
async def on_message(message):
    # Ignore messages sent by the bot itself
    if message.author == bot.user:
        return

    # Check if the bot is mentioned in the message
    if bot.user.mentioned_in(message):
        # Get the content of the message without the bot mention
        content = message.content.replace(f'<@!{bot.user.id}>', '').strip()
        content = content.replace(f'<@{bot.user.id}>', '').strip()

        if content:
            # Send the message to the AI agent and get the response
            ai_response = send_message_to_ai_agent(content)
            await message.channel.send(ai_response)
        else:
            await message.channel.send("You mentioned me, but didn't say anything!")

    # Process commands if any
    await bot.process_commands(message)

@bot.command(name='hello')
async def hello(ctx):
    await ctx.send('Hello World!')

# Replace 'YOUR_BOT_TOKEN' with your actual bot token
bot.run('YOUR_DISCORD_BOT_TOKEN')
```
