Refactor logging implementation to replace print statements with logging calls for better traceability and debugging.
Fixes DM boolean.
This commit is contained in:
parent
c7dd2a88d8
commit
1207ed0ea0
@ -1,8 +1,16 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import logging
|
||||||
|
|
||||||
|
# ────────────────────────────────────────────────
|
||||||
|
# Setup logging
|
||||||
|
# ────────────────────────────────────────────────
|
||||||
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
|
|
||||||
# ────────────────────────────────────────────────
|
# ────────────────────────────────────────────────
|
||||||
def load_config(path: str):
|
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
|
# Attach tools if provided in the config file
|
||||||
if tools_list:
|
if tools_list:
|
||||||
payload["tool_ids"] = 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
|
# The endpoint path for your instance appears to be /api/chat/completions
|
||||||
async with session.post(f"{OPENWEBUI_URL}/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
|
# If the response is OK, parse the JSON and return the content
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
|
logging.debug(f"Unparsed response from Open-WebUI: {response_data}")
|
||||||
return response_data['choices'][0]['message']['content']
|
return response_data['choices'][0]['message']['content']
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
@ -78,12 +89,13 @@ bot = commands.Bot(command_prefix='!', intents=intents) # Command prefix is '!'
|
|||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
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
|
# Only a test for commands, I add later
|
||||||
@bot.command(name="ping")
|
@bot.command(name="ping")
|
||||||
async def ping(ctx):
|
async def ping(ctx):
|
||||||
await ctx.send("🏓 Pong!")
|
await ctx.send("🏓 Pong!")
|
||||||
|
return
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
# Main logic – only respond to allowed channels / DM flag
|
# Main logic – only respond to allowed channels / DM flag
|
||||||
@ -92,21 +104,33 @@ async def ping(ctx):
|
|||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message):
|
async def on_message(message):
|
||||||
# Ignore messages from bots (incl. the bot itself)
|
# 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)
|
await bot.process_commands(message)
|
||||||
|
|
||||||
# Skip if we are in a DM and that is disabled
|
# Now, check if a command was actually invoked. If so, we don't want
|
||||||
if not ALLOW_DMS and isinstance(message.channel, discord.DMChannel): return
|
# 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 ---
|
# --- debugging ---
|
||||||
print(f"ℹ️ Message received in channel: {message.channel.id}")
|
logging.info(f"ℹ️ Message received in channel: {message.channel.id}")
|
||||||
print(f"📢 Whitelisted channels are: {WHITELIST_CHANNELS}")
|
logging.info(f"📢 Whitelisted channels are: {WHITELIST_CHANNELS}")
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
|
||||||
# Allow only the whitelist channels – empty list means “all channels”
|
# Allow only whitelisted channels (if not a DM)
|
||||||
if WHITELIST_CHANNELS and message.channel.id not in WHITELIST_CHANNELS:
|
# 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
|
return
|
||||||
|
|
||||||
# ----------------------------------------------------------------------- #
|
# ----------------------------------------------------------------------- #
|
||||||
@ -139,6 +163,7 @@ async def on_message(message):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
asyncio.run(bot.run(DISCORD_TOKEN))
|
# DO NOT wrap bot.run in asyncio.run()
|
||||||
|
bot.run(DISCORD_TOKEN)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("🤖 Shutting down…")
|
logging.info("🤖 Shutting down…")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user