first commit

This commit is contained in:
Brad Ganley 2024-05-31 04:23:06 +00:00
parent d39284471a
commit 5d633133f4
6 changed files with 98 additions and 0 deletions

9
.dockerignore Normal file
View File

@ -0,0 +1,9 @@
# Ignore the virtual environment
venv
# Ignore the .env file for security reasons (you should handle secrets securely)
.env
# Ignore Python cache files
__pycache__
*.pyc

2
.env Executable file
View File

@ -0,0 +1,2 @@
DISCORD_TOKEN=MTI0NDM1NjA1OTQzNTQ5OTUyMA.GIdL5i.6CWx3rvb9JSrCjlAr3OqHfte2SfGHUM79i2y4w
OPENAI_API_KEY=sk-proj-DT1hBidCoRhhwHF1LT0RT3BlbkFJXE5dRygQkJUxrX9hOWqN

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
# Use the official Python image from the Docker Hub
FROM python:3.9-slim
# Set environment variables
ENV PYTHONUNBUFFERED=1
# Set the working directory
WORKDIR /app
# Copy the requirements file to the working directory
COPY requirements.txt .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code to the working directory
COPY . .
# Command to run the bot
CMD ["python", "bot.py"]

52
bot.py Executable file
View File

@ -0,0 +1,52 @@
import os
import discord
from openai import AsyncOpenAI
from discord.ext import commands
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
aclient = AsyncOpenAI(api_key=OPENAI_API_KEY)
# Initialize OpenAI API
# Set up the bot with the message content intent
intents = discord.Intents.default()
intents.message_content = True # Ensure the bot can read message content
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Bot is ready. Logged in as {bot.user}')
@bot.event
async def on_message(message):
# Log the message content for debugging
print(f'Message from {message.author}: {message.content}')
# Process commands if the message is not from the bot itself
if message.author == bot.user:
return
await bot.process_commands(message)
@bot.command(name='bot')
async def ask_gpt(ctx, *, question: str):
try:
# Send the question to GPT-4o using the new OpenAI API
response = await aclient.chat.completions.create(model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant. You exist inside a discord server"},
{"role": "user", "content": question}
])
answer = response.choices[0].message.content.strip()
# Send the response back to the Discord channel
await ctx.send(answer)
except Exception as e:
await ctx.send("An error occurred while processing your request.")
print(f'Error: {e}')
# Run the bot
bot.run(DISCORD_TOKEN)

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
version: '3.8'
services:
discord-bot:
network_mode: bridge
build: .
container_name: discord-bot
env_file:
- .env
volumes:
- .:/app
command: ["python", "bot.py"]

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
OpenAI
python-dotenv
discord