Skip to content

Commit

Permalink
Merge pull request #274 from SylteA/dev
Browse files Browse the repository at this point in the history
Update aiohttp version & Fix youtube extension
  • Loading branch information
mohamed040406 authored Nov 17, 2023
2 parents fc84472 + bb04abc commit fb79930
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 112 deletions.
53 changes: 31 additions & 22 deletions bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
import json
import logging
import os
import sys
import traceback
from typing import Any

import discord
from discord import app_commands
from discord.ext import commands, tasks

from bot.config import settings
from bot.services import http, paste
from bot.services.paste import Document
from utils.errors import IgnorableException
from utils.time import human_timedelta

Expand Down Expand Up @@ -95,6 +98,31 @@ async def process_commands(self, message: discord.Message, /):
log.info(f"{ctx.author} invoking command: {ctx.clean_prefix}{ctx.command.qualified_name}")
await self.invoke(ctx)

async def send_error(self, content: str, header: str, invoked_details_document: Document = None) -> None:
def wrap(code: str) -> str:
code = code.replace("`", "\u200b`")
return f"```py\n{code}\n```"

if len(content) > 1024: # Keeping it short for readability.
document = await paste.create(content)
content = wrap(content[:1024]) + f"\n\n [Full traceback]({document.url})"
else:
content = wrap(content)

embed = discord.Embed(
title=header, description=content, color=discord.Color.red(), timestamp=discord.utils.utcnow()
)
if invoked_details_document:
embed.add_field(name="Command Details: ", value=invoked_details_document.url, inline=True)

await self.error_webhook.send(embed=embed)

async def on_error(self, event_method: str, *args: Any, **kwargs: Any) -> None:
content = "\n".join(traceback.format_exception(*sys.exc_info()))
header = f"Ignored exception in event method **{event_method}**"

await self.send_error(content, header)

async def on_app_command_error(self, interaction: "InteractionType", error: app_commands.AppCommandError):
"""Handle errors in app commands."""

Expand All @@ -109,33 +137,14 @@ async def on_app_command_error(self, interaction: "InteractionType", error: app_
log.info(f"{interaction.user} failed to use the command {interaction.command.qualified_name}")
return

await self.publish_error(interaction=interaction, error=error)
log.error("Ignoring unhandled exception", exc_info=error)

async def publish_error(self, interaction: "InteractionType", error: app_commands.AppCommandError) -> None:
"""Publishes the error to our error webhook."""
content = "".join(traceback.format_exception(type(error), error, error.__traceback__))
header = (
f"Ignored exception in command **{interaction.command.qualified_name}** Invoked by **{interaction.user}**"
f"Ignored exception in command **{interaction.command.qualified_name}** Invoked by **{interaction.user}** "
f"in channel **{interaction.channel.name}**"
)
invoked_details_document = await paste.create(str(json.dumps(interaction.data, indent=2)))

def wrap(code: str) -> str:
code = code.replace("`", "\u200b`")
return f"```py\n{code}\n```"

if len(content) > 1024: # Keeping it short for readability.
document = await paste.create(content)
content = wrap(content[:1024]) + f"\n\n [Full traceback]({document.url})"
else:
content = wrap(content)

embed = discord.Embed(
title=header, description=content, color=discord.Color.red(), timestamp=discord.utils.utcnow()
)
embed.add_field(name="Command Details: ", value=invoked_details_document.url, inline=True)
await self.error_webhook.send(embed=embed)
await self.send_error(content, header, invoked_details_document)
log.error("Ignoring unhandled exception", exc_info=error)

@tasks.loop(hours=24)
async def presence(self):
Expand Down
9 changes: 7 additions & 2 deletions bot/extensions/youtube/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ async def check_for_new_videos(self):
await self.check_old_thumbnails()
await self.find_new_videos()

@check_for_new_videos.error
async def on_check_error(self, _error: Exception):
"""Logs any errors that occur during the check_for_new_videos task"""
await self.bot.on_error("check_for_new_videos")

@check_for_new_videos.before_loop
async def before_check(self):
"""Fetches the 10 last videos posted so we don't accidentally re-post it."""
Expand All @@ -54,7 +59,7 @@ async def before_check(self):
if message.embeds:
embed = message.embeds[0]
self.video_links.append(embed.url)
if embed.image.url.endswith("/mqdefault.jpg"):
if message.author == self.bot.user and embed.image.url.endswith("/mqdefault.jpg"):
self.old_thumbnails.append((message, embed.image.url))

else:
Expand Down Expand Up @@ -93,7 +98,7 @@ async def find_new_videos(self):
title=entry.find(ns + "title").text,
published=entry.find(ns + "published").text,
description=media_group.find(md + "description").text,
thumbnail=media_group.find(md + "thumbnail").attrib["url"],
thumbnail=media_group.find(md + "thumbnail").attrib["url"].replace("/hqdefault.jpg", "/mqdefault.jpg"),
)

if video.link in self.video_links:
Expand Down
Loading

0 comments on commit fb79930

Please sign in to comment.