From 1207ed0ea07d95f192c8667afdfe9212cd2be12c Mon Sep 17 00:00:00 2001 From: Pakobbix Date: Sun, 17 Aug 2025 16:03:09 +0200 Subject: [PATCH] Refactor logging implementation to replace print statements with logging calls for better traceability and debugging. Fixes DM boolean. --- discord_connector/open-webui_to_discord.py | 49 ++++++++++++++++------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/discord_connector/open-webui_to_discord.py b/discord_connector/open-webui_to_discord.py index 8f98b40..942d4c7 100644 --- a/discord_connector/open-webui_to_discord.py +++ b/discord_connector/open-webui_to_discord.py @@ -1,8 +1,16 @@ +#!/usr/bin/env python3 + import yaml import asyncio import discord from discord.ext import commands import aiohttp +import logging + +# ──────────────────────────────────────────────── +# Setup logging +# ──────────────────────────────────────────────── +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') # ──────────────────────────────────────────────── def load_config(path: str): @@ -49,7 +57,9 @@ async def _query_openwebui(user_text: str, channel_id: int, tools_list: list): # Attach tools if provided in the config file if tools_list: payload["tool_ids"] = tools_list - print(f"🔧 Using tools: {payload['tool_ids']}") + logging.debug(f"🔧 Using tools: {payload['tool_ids']}") + + logging.debug(f"Request payload to Open-WebUI: {payload}") # The endpoint path for your instance appears to be /api/chat/completions async with session.post(f"{OPENWEBUI_URL}/api/chat/completions", @@ -63,6 +73,7 @@ async def _query_openwebui(user_text: str, channel_id: int, tools_list: list): # If the response is OK, parse the JSON and return the content response_data = await resp.json() + logging.debug(f"Unparsed response from Open-WebUI: {response_data}") return response_data['choices'][0]['message']['content'] # --------------------------------------------------------------------------- # @@ -78,12 +89,13 @@ bot = commands.Bot(command_prefix='!', intents=intents) # Command prefix is '!' @bot.event async def on_ready(): - print(f"✅ Logged in as {bot.user} (id={bot.user.id})") + logging.info(f"✅ Logged in as {bot.user} (id={bot.user.id})") # Only a test for commands, I add later @bot.command(name="ping") async def ping(ctx): await ctx.send("🏓 Pong!") + return # --------------------------------------------------------------------------- # # Main logic – only respond to allowed channels / DM flag @@ -92,21 +104,33 @@ async def ping(ctx): @bot.event async def on_message(message): # Ignore messages from bots (incl. the bot itself) - if message.author.bot: return + if message.author.bot: + return - # Allow commands to be processed + # This is crucial: process commands first. await bot.process_commands(message) - # Skip if we are in a DM and that is disabled - if not ALLOW_DMS and isinstance(message.channel, discord.DMChannel): return + # Now, check if a command was actually invoked. If so, we don't want + # to treat it as a prompt for the AI, so we stop here. + ctx = await bot.get_context(message) + if ctx.valid: + return + + # Determine if the message is a DM + is_dm = isinstance(message.channel, discord.DMChannel) + + # Skip if it's a DM and DMs are disabled + if is_dm and not ALLOW_DMS: + return # --- debugging --- - print(f"ℹ️ Message received in channel: {message.channel.id}") - print(f"📢 Whitelisted channels are: {WHITELIST_CHANNELS}") + logging.info(f"ℹ️ Message received in channel: {message.channel.id}") + logging.info(f"📢 Whitelisted channels are: {WHITELIST_CHANNELS}") # ----------------------------- - # Allow only the whitelist channels – empty list means “all channels” - if WHITELIST_CHANNELS and message.channel.id not in WHITELIST_CHANNELS: + # Allow only whitelisted channels (if not a DM) + # An empty whitelist means all guild channels are allowed. + if not is_dm and WHITELIST_CHANNELS and message.channel.id not in WHITELIST_CHANNELS: return # ----------------------------------------------------------------------- # @@ -139,6 +163,7 @@ async def on_message(message): if __name__ == "__main__": try: - asyncio.run(bot.run(DISCORD_TOKEN)) + # DO NOT wrap bot.run in asyncio.run() + bot.run(DISCORD_TOKEN) except KeyboardInterrupt: - print("🤖 Shutting down…") + logging.info("🤖 Shutting down…")