From f2663ff76f5edd88ef70a13f3cbdcaa37fe7eb5b Mon Sep 17 00:00:00 2001 From: SylteA Date: Sun, 17 Mar 2024 11:42:46 +0100 Subject: [PATCH 1/2] Don't post to paste & webhook when running locally --- bot/core.py | 14 +++++++++++--- bot/extensions/youtube/tasks.py | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bot/core.py b/bot/core.py index d733058e..4dd28439 100644 --- a/bot/core.py +++ b/bot/core.py @@ -23,6 +23,8 @@ os.environ.update(JISHAKU_NO_UNDERSCORE="True", JISHAKU_NO_DM_TRACEBACK="True", JISHAKU_HIDE="True") ALLOWED_MENTIONS = discord.AllowedMentions(everyone=False, roles=False, users=False) +RUNNING_IN_KUBERNETES = os.environ.get("KUBERNETES_SERVICE_HOST") is not None + class DiscordBot(commands.Bot): def __init__(self, prefixes: tuple[str, ...], extensions: tuple[str, ...], intents: discord.Intents): @@ -117,6 +119,8 @@ async def on_message(self, message): if message.author.bot: return + raise ValueError("Test") + log.debug(f"{message.channel}: {message.author}: {message.clean_content}") await self.process_commands(message) @@ -146,7 +150,7 @@ async def on_app_command_completion( VALUES ($1, $2, $3, $4, $5, $6, $7) """ - res = await Model.pool.execute( + await Model.pool.execute( query, interaction.user.id, interaction.guild_id, @@ -156,11 +160,13 @@ async def on_app_command_completion( command.qualified_name, interaction.data["options"], ) - log.info(f"Command usage inserted {res}") except Exception as e: await self.on_error("on_app_command_completion", e) async def send_error(self, content: str, header: str, invoked_details_document: Document = None) -> None: + if not RUNNING_IN_KUBERNETES: + return + def wrap(code: str) -> str: code = code.replace("`", "\u200b`") return f"```py\n{code}\n```" @@ -180,10 +186,12 @@ def wrap(code: str) -> str: await self.error_webhook.send(embed=embed) async def on_error(self, event_method: str, *args: Any, **kwargs: Any) -> None: - content = "".join(traceback.format_exception(*sys.exc_info())) + error = sys.exc_info() + content = "".join(traceback.format_exception(*error)) header = f"Ignored exception in event method **{event_method}**" await self.send_error(content, header) + log.error(f"Ignored exception in event method {event_method}", exc_info=error) async def on_app_command_error(self, interaction: "InteractionType", error: app_commands.AppCommandError): """Handle errors in app commands.""" diff --git a/bot/extensions/youtube/tasks.py b/bot/extensions/youtube/tasks.py index fc3ffe63..69949ea7 100644 --- a/bot/extensions/youtube/tasks.py +++ b/bot/extensions/youtube/tasks.py @@ -1,3 +1,4 @@ +import logging import re import traceback import xml.etree.ElementTree as ET @@ -15,6 +16,9 @@ RSS_FEED_BASE_URL = "https://www.youtube.com/feeds/videos.xml" +log = logging.getLogger(__name__) + + class Video(BaseModel): link: str title: str @@ -100,6 +104,8 @@ async def find_new_videos(self): header = "Ignored exception when parsing rss feed." document = await paste.create(content=data) + log.error(header, exc_info=e) + return await self.bot.send_error(content, header, invoked_details_document=document) ns = "{http://www.w3.org/2005/Atom}" From 83d2a776fd48640fe9391d25767fbf84584232ed Mon Sep 17 00:00:00 2001 From: SylteA Date: Sun, 17 Mar 2024 11:45:33 +0100 Subject: [PATCH 2/2] No --- bot/core.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bot/core.py b/bot/core.py index 4dd28439..fcab12df 100644 --- a/bot/core.py +++ b/bot/core.py @@ -119,8 +119,6 @@ async def on_message(self, message): if message.author.bot: return - raise ValueError("Test") - log.debug(f"{message.channel}: {message.author}: {message.clean_content}") await self.process_commands(message)