Merge pull request 'switched to atiohttp' (#13) from dev into main
All checks were successful
Safety Check / safety-check (push) Successful in -2s
Code Quality Check / quality-check (push) Successful in 11s
Build and Push Docker Image / build (push) Successful in -9s

Reviewed-on: #13
This commit is contained in:
wirehack7 2025-05-01 15:56:10 +02:00
commit b2c2d4bcc2
3 changed files with 21 additions and 19 deletions

View File

@ -20,7 +20,7 @@ jobs:
- name: Set up Python - name: Set up Python
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:
python-version: '3.10' python-version: '3.13'
- name: Install dependencies - name: Install dependencies
run: | run: |

View File

@ -10,8 +10,8 @@ import sys
import time import time
import zipfile import zipfile
import aiohttp
import discord import discord
import requests
from discord import app_commands from discord import app_commands
from discord.ext import commands from discord.ext import commands
@ -30,7 +30,7 @@ logger = logging.getLogger(__name__)
if not all([NOVELAI_API_TOKEN, BOT_TOKEN, ALLOWED_CHANNEL_ID]): if not all([NOVELAI_API_TOKEN, BOT_TOKEN, ALLOWED_CHANNEL_ID]):
raise ValueError( raise ValueError(
"NOVELAI_API_TOKEN, DISCORD_BOT_TOKEN und DISCORD_CHANNEL_ID müssen gesetzt sein." "NOVELAI_API_TOKEN, DISCORD_BOT_TOKEN and DISCORD_CHANNEL_ID have to be set."
) )
intents = discord.Intents.default() intents = discord.Intents.default()
@ -43,9 +43,9 @@ async def on_ready():
print(f"Bot gestartet als {bot.user}") print(f"Bot gestartet als {bot.user}")
try: try:
synced = await bot.tree.sync() synced = await bot.tree.sync()
logger.info("%d Slash-Commands synchronisiert.", len(synced)) logger.info("%d Slash-Commands synchronized.", len(synced))
except Exception as err: # pylint: disable=W0718 except Exception as err: # pylint: disable=W0718
logger.error("Fehler beim Synchronisieren der Commands: %s", err) logger.error("error while syncing the command: %s", err)
activity = discord.Game(name="generating juicy NovelAI images 🥵") activity = discord.Game(name="generating juicy NovelAI images 🥵")
await bot.change_presence(status=discord.Status.online, activity=activity) await bot.change_presence(status=discord.Status.online, activity=activity)
@ -159,17 +159,21 @@ async def generate(
} }
start_time = time.monotonic() start_time = time.monotonic()
response = requests.post(
async with aiohttp.ClientSession() as session:
async with session.post(
"https://image.novelai.net/ai/generate-image", "https://image.novelai.net/ai/generate-image",
json=payload, json=payload,
headers=headers, headers=headers,
timeout=120, timeout=aiohttp.ClientTimeout(total=120),
) ) as response:
response_bytes = await response.read()
status_code = response.status
duration = time.monotonic() - start_time duration = time.monotonic() - start_time
logger.info("Image creation took %.2f seconds", duration) logger.info("Image creation took %.2f seconds", duration)
if response.status_code == 200: if status_code == 200:
response_bytes = response.content
param_text = ( param_text = (
f"```Prompt: {prompt}\n" f"```Prompt: {prompt}\n"
f"Undesired prompt: {negative_prompt}\n" f"Undesired prompt: {negative_prompt}\n"
@ -206,13 +210,11 @@ async def generate(
return return
try: try:
error_data = response.json() error_data = response_bytes.decode("utf-8")
except Exception: # pylint: disable=W0718 except Exception: # pylint: disable=W0718
error_data = {"error": response.text} error_data = "error message unreadable"
error_message = f"Error {response.status_code} at API-request.\n" error_message = f"Error {status_code} at API-request.\n" f"Answer: {error_data}\n"
for key, value in error_data.items():
error_message += f"**{key}**: {value}\n"
await interaction.followup.send(error_message) await interaction.followup.send(error_message)

View File

@ -1,2 +1,2 @@
discord discord
requests aiohttp