diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..bc66d8b --- /dev/null +++ b/.dockerignore @@ -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 diff --git a/.env b/.env new file mode 100755 index 0000000..365f399 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +DISCORD_TOKEN=MTI0NDM1NjA1OTQzNTQ5OTUyMA.GIdL5i.6CWx3rvb9JSrCjlAr3OqHfte2SfGHUM79i2y4w +OPENAI_API_KEY=sk-proj-DT1hBidCoRhhwHF1LT0RT3BlbkFJXE5dRygQkJUxrX9hOWqN diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c45604c --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/bot.py b/bot.py new file mode 100755 index 0000000..bc62173 --- /dev/null +++ b/bot.py @@ -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) + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b3c345f --- /dev/null +++ b/docker-compose.yml @@ -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"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e3f5a13 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +OpenAI +python-dotenv +discord