Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/discord blocking thread #18

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Jainy Bot Changelog

## Version v1.1.0
## Version v1.1.1

**Features**
**Fixes**

- Added dalle command to generate images via ai
- Fixed synchronous code blocking discord health check

**Dev Deps**

- removed requests lib for aiohttp to handle async requests

## Version v1.1.0
**Features**
- Added dalle command to generate images via ai

**Dev Deps**
- added requests for api calls

## Version v1.0.0
Expand Down
22 changes: 16 additions & 6 deletions jainy_bot/commands/dalle.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import base64
from io import BytesIO

import aiohttp
import discord
import requests
from discord.ext import commands
from loguru import logger

from config import DALLE_API_URL


async def make_api_request(payload: dict) -> dict or None:
async with aiohttp.ClientSession() as session:
async with session.post(DALLE_API_URL, json=payload) as response:
if response.status == 200:
logger.info(f'Successfully contacted Dalle Api')
payload = await response.json()
return payload
else:
logger.error(f'Dalle Server responded with status = {response.status}')
return None


class Dalle(commands.Cog, name="Dalle"):
def __init__(self, bot: commands.bot):
self.bot = bot
Expand All @@ -23,12 +35,12 @@ async def dalle(self, ctx: commands.Context, prompt: str):
}

await ctx.send(f'{ctx.author.mention} hang tight I\'m checking with Dall-E')
response = requests.post(DALLE_API_URL, json=payload)
response = await make_api_request(payload)

if response.status_code == 200:
if response:
logger.info(f"Dalle image created successfully")
await ctx.send(f'{ctx.author.mention} generating image please wait')
data = response.json()
data = response
images = data['images']
files = []

Expand All @@ -43,6 +55,4 @@ async def dalle(self, ctx: commands.Context, prompt: str):
embed=discord.Embed(title=f'{prompt} requested by {ctx.author}')
)
else:
logger.error(
f'Error creating a Dalle image with prompt: {prompt} server responded with status = {response.status_code}')
await ctx.send('Could not generate images')
17 changes: 16 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
aiohttp==3.9.4
aiosignal==1.3.1
anyio==4.3.0
attrs==23.2.0
certifi==2024.2.2
charset-normalizer==3.3.2
discord==2.3.2
discord.py==2.3.2
frozenlist==1.4.1
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
loguru==0.7.2
requests~=2.31.0
multidict==6.0.5
sniffio==1.3.1
urllib3==2.2.1
yarl==1.9.4
Loading