Getting the initial Bot Code Pushed
This commit is contained in:
4
scripts/.env.sample
Normal file
4
scripts/.env.sample
Normal file
@@ -0,0 +1,4 @@
|
||||
DISCORD_TOKEN=your_discord_bot_token
|
||||
OPENAI_API_KEY=your_openwebui_api_key
|
||||
OPENWEBUI_API_BASE=http://your_openwebui_instance:port/v1
|
||||
MODEL_NAME="Your_Model_Name"
|
84
scripts/discordbot.py
Normal file
84
scripts/discordbot.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from openai import OpenAI
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Configure OpenAI client to point to OpenWebUI
|
||||
client = OpenAI(
|
||||
api_key=os.getenv('OPENAI_API_KEY'),
|
||||
base_url=os.getenv('OPENWEBUI_API_BASE') # e.g., "http://localhost:8080/v1"
|
||||
)
|
||||
|
||||
# Initialize Discord bot
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
bot = commands.Bot(command_prefix="!", intents=intents)
|
||||
|
||||
async def get_ai_response(prompt):
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0", # OpenWebUI should handle this regardless
|
||||
messages=[
|
||||
{"role": "user", "content": prompt}
|
||||
],
|
||||
temperature=0.7,
|
||||
max_tokens=500
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
except Exception as e:
|
||||
print(f"Error getting AI response: {e}")
|
||||
return "Sorry, I encountered an error while processing your request."
|
||||
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f'{bot.user} has connected to Discord!')
|
||||
|
||||
@bot.event
|
||||
async def on_message(message):
|
||||
# Ignore messages from the bot itself
|
||||
if message.author == bot.user:
|
||||
return
|
||||
|
||||
# Check if the bot is mentioned in the message
|
||||
if bot.user in message.mentions:
|
||||
# Remove the bot mention and get the actual prompt
|
||||
prompt = message.content.replace(f'<@{bot.user.id}>', '').strip()
|
||||
|
||||
# If there's no prompt after mentioning the bot
|
||||
if not prompt:
|
||||
await message.channel.send("Hello! How can I help you?")
|
||||
return
|
||||
|
||||
# Show typing indicator while processing
|
||||
async with message.channel.typing():
|
||||
# Get response from OpenWebUI
|
||||
response = await get_ai_response(prompt)
|
||||
|
||||
# Send the response
|
||||
# Split long messages if they exceed Discord's character limit
|
||||
if len(response) > 2000:
|
||||
# Split the response into chunks of 2000 characters
|
||||
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
|
||||
for chunk in chunks:
|
||||
await message.channel.send(chunk)
|
||||
else:
|
||||
await message.channel.send(response)
|
||||
|
||||
await bot.process_commands(message)
|
||||
|
||||
def main():
|
||||
# Get the Discord token from environment variables
|
||||
discord_token = os.getenv('DISCORD_TOKEN')
|
||||
if not discord_token:
|
||||
raise ValueError("Discord token not found in environment variables")
|
||||
|
||||
# Run the bot
|
||||
bot.run(discord_token)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user