Skip to content

Commit

Permalink
Don't post to paste & webhook when running locally
Browse files Browse the repository at this point in the history
  • Loading branch information
SylteA committed Mar 17, 2024
1 parent 05c9902 commit f2663ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 11 additions & 3 deletions bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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```"
Expand All @@ -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."""
Expand Down
6 changes: 6 additions & 0 deletions bot/extensions/youtube/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re
import traceback
import xml.etree.ElementTree as ET
Expand All @@ -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
Expand Down Expand Up @@ -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}"
Expand Down

0 comments on commit f2663ff

Please sign in to comment.