From 3c979d855e6151131784ed05d7125d1dec5b3b58 Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Thu, 1 May 2025 13:16:48 +0000 Subject: [PATCH 1/4] Update dependency python to 3.13 --- .gitea/workflows/check-code_quality.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/check-code_quality.yaml b/.gitea/workflows/check-code_quality.yaml index 9962c55..d5c9f05 100644 --- a/.gitea/workflows/check-code_quality.yaml +++ b/.gitea/workflows/check-code_quality.yaml @@ -20,7 +20,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v5 with: - python-version: '3.10' + python-version: '3.13' - name: Install dependencies run: | From 3fbc996de182ea03bb1fefda563ec54100ed1bc3 Mon Sep 17 00:00:00 2001 From: wirehack7 Date: Thu, 1 May 2025 15:52:18 +0200 Subject: [PATCH 2/4] switched to aiohttp --- src/main.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main.py b/src/main.py index 8749a1b..d74c403 100644 --- a/src/main.py +++ b/src/main.py @@ -12,6 +12,7 @@ import zipfile import discord import requests +import aiohttp from discord import app_commands from discord.ext import commands @@ -30,7 +31,7 @@ logger = logging.getLogger(__name__) if not all([NOVELAI_API_TOKEN, BOT_TOKEN, ALLOWED_CHANNEL_ID]): 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() @@ -43,9 +44,9 @@ async def on_ready(): print(f"Bot gestartet als {bot.user}") try: 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 - 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 🥵") await bot.change_presence(status=discord.Status.online, activity=activity) @@ -165,11 +166,21 @@ async def generate( headers=headers, timeout=120, ) + + async with aiohttp.ClientSession() as session: + async with session.post( + "https://image.novelai.net/ai/generate-image", + json=payload, + headers=headers, + timeout=aiohttp.ClientTimeout(total=120), + ) as response: + response_bytes = await response.read() + status_code = response.status + duration = time.monotonic() - start_time logger.info("Image creation took %.2f seconds", duration) - if response.status_code == 200: - response_bytes = response.content + if status_code == 200: param_text = ( f"```Prompt: {prompt}\n" f"Undesired prompt: {negative_prompt}\n" @@ -206,13 +217,14 @@ async def generate( return try: - error_data = response.json() + error_data = response_bytes.decode("utf-8") 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" - for key, value in error_data.items(): - error_message += f"**{key}**: {value}\n" + error_message = ( + f"Error {status_code} at API-request.\n" + f"Answer: {error_data}\n" + ) await interaction.followup.send(error_message) From 1ef60193a049acd56e70b77592a90af7ccb24925 Mon Sep 17 00:00:00 2001 From: wirehack7 Date: Thu, 1 May 2025 15:55:54 +0200 Subject: [PATCH 3/4] more fixes --- src/main.py | 15 +++------------ src/requirements.txt | 3 ++- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/main.py b/src/main.py index d74c403..fccef60 100644 --- a/src/main.py +++ b/src/main.py @@ -10,9 +10,9 @@ import sys import time import zipfile +import aiohttp import discord import requests -import aiohttp from discord import app_commands from discord.ext import commands @@ -160,13 +160,7 @@ async def generate( } start_time = time.monotonic() - response = requests.post( - "https://image.novelai.net/ai/generate-image", - json=payload, - headers=headers, - timeout=120, - ) - + async with aiohttp.ClientSession() as session: async with session.post( "https://image.novelai.net/ai/generate-image", @@ -221,10 +215,7 @@ async def generate( except Exception: # pylint: disable=W0718 error_data = "error message unreadable" - error_message = ( - f"Error {status_code} at API-request.\n" - f"Answer: {error_data}\n" - ) + error_message = f"Error {status_code} at API-request.\n" f"Answer: {error_data}\n" await interaction.followup.send(error_message) diff --git a/src/requirements.txt b/src/requirements.txt index a19c97b..47ef825 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1,2 +1,3 @@ discord -requests \ No newline at end of file +requests +aiohttp \ No newline at end of file From 6bf61fdbf069f1b049bbbb79588e0875223973ba Mon Sep 17 00:00:00 2001 From: wirehack7 Date: Thu, 1 May 2025 15:56:57 +0200 Subject: [PATCH 4/4] removed requests --- src/main.py | 1 - src/requirements.txt | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main.py b/src/main.py index fccef60..ec7718d 100644 --- a/src/main.py +++ b/src/main.py @@ -12,7 +12,6 @@ import zipfile import aiohttp import discord -import requests from discord import app_commands from discord.ext import commands diff --git a/src/requirements.txt b/src/requirements.txt index 47ef825..f0264e6 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1,3 +1,2 @@ discord -requests aiohttp \ No newline at end of file