generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94414a9
commit 647b4df
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import discord | ||
from discord.ext import commands | ||
import openai | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() | ||
|
||
# Fetch keys from environment variables | ||
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") | ||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | ||
|
||
openai.api_key = OPENAI_API_KEY | ||
|
||
intents = discord.Intents.default() | ||
intents.messages = True | ||
intents.guilds = True | ||
intents.message_content = True | ||
bot = commands.Bot(command_prefix="!", intents=intents) | ||
|
||
@bot.event | ||
async def on_ready(): | ||
print(f"Logged in as {bot.user.name}") | ||
|
||
@bot.command() | ||
async def generate(ctx, *, prompt: str = None): | ||
"""Generates a response or image based on the provided prompt or image""" | ||
if prompt: | ||
# Text prompt provided, generate image with DALL·E 3 | ||
response = openai.Image.create( | ||
model="dall-e-3", | ||
prompt=prompt, | ||
size="1024x1024", | ||
quality="standard", | ||
n=1 | ||
) | ||
image_url = response.data[0].url | ||
await ctx.send(image_url) | ||
elif ctx.message.attachments: | ||
# Image attached, use GPT-4 with Vision | ||
image_url = ctx.message.attachments[0].url | ||
response = openai.ChatCompletion.create( | ||
model="gpt-4-vision-preview", | ||
messages=[ | ||
{"role": "system", "content": {"image": image_url}} | ||
], | ||
max_tokens=300 | ||
) | ||
await ctx.send(response.choices[0].message['content']) | ||
else: | ||
await ctx.send("Please provide a text prompt or an image.") | ||
|
||
@generate.error | ||
async def generate_error(ctx, error): | ||
if isinstance(error, commands.MissingRequiredArgument): | ||
await ctx.send("You must provide a prompt or an image!") | ||
else: | ||
await ctx.send(f"An error occurred: {error}") | ||
|
||
bot.run(DISCORD_TOKEN) |