Discord
This tutorial shows how to setup a Discord bot with Stammer.ai and Replit.
Last updated
This tutorial shows how to setup a Discord bot with Stammer.ai and Replit.
Last updated
4 Items are Needed for this tutorial:
Discord Bot Token
API Key from Stammer.ai
Chatbot UUID from Stammer.ai
(Optional) White Label URL from Stammer.ai
1. Set up Discord account and server: - Create a Discord account (if you don’t have one) - Create a new server or use an existing one
2. Create Discord application: - Go to https://discord.com/developers/applications - Click “New Application” - Name your application - Go to “Bot” tab and click “Add Bot”
3. Get bot token: - In “Bot” tab, find token section - Click “Copy” to copy bot token to paste into Replit - Keep this token secret!
4. Invite bot to server: - Go to “OAuth2" tab - In “Scopes” section, select “bot” - Choose desired permissions - Copy generated URL and open in new tab - Select server and authorize bot on the channels you want it to access
5. Create New Replit: - Create new Python file - Paste in the template code - Paste in the Discord bot code on line 68 - Update the Stammer Authorization key on line 10 - Update the chatbot UUID on line 11 - (Optional) Update the URL to be your white label API URL on line 14
6. Running the AI Agent: - Run the code in Replit - Go to your Discord channel and ask your bot a question by tagging its name
Code for Replit:
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')