Skip to content

Commit

Permalink
[NotSoBot] Catch Tenor API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TrustyJAID committed May 22, 2024
1 parent 710f0a5 commit 9192719
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions notsobot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import unidecode
from discord.ext.commands.converter import Converter
from discord.ext.commands.errors import BadArgument
from red_commons.logging import getLogger
from redbot.core import commands

IMAGE_LINKS: Pattern = re.compile(
Expand All @@ -23,6 +24,8 @@

VALID_CONTENT_TYPES = ("image/png", "image/jpeg", "image/jpg", "image/gif")

log = getLogger("red.trusty-cogs.NotSoBot")


class TenorError(Exception):
pass
Expand Down Expand Up @@ -95,10 +98,14 @@ async def convert(
api = ctx.cog.tenor
if api:
tenor_matches = [m.group("image_id") for m in tenor_matches]
posts = await api.posts(tenor_matches)
for post in posts:
if "gif" in post.media_formats:
urls.append(post.media_formats["gif"].url)
try:
posts = await api.posts(tenor_matches)
for post in posts:
if "gif" in post.media_formats:
urls.append(post.media_formats["gif"].url)
except TenorError as e:
log.error("Error getting tenor image information. %s", e)

if matches:
for match in matches:
urls.append(match.group(1))
Expand Down Expand Up @@ -167,10 +174,13 @@ async def search_for_images(
if tenor:
api = ctx.cog.tenor
if api:
posts = await api.posts([tenor.group("image_id")])
for post in posts:
if "gif" in post.media_formats:
urls.append(post.media_formats["gif"].url)
try:
posts = await api.posts([tenor.group("image_id")])
for post in posts:
if "gif" in post.media_formats:
urls.append(post.media_formats["gif"].url)
except TenorError as e:
log.error("Error getting tenor image information. %s", e)
if not urls:
raise BadArgument("No Images found in recent history.")
return urls

0 comments on commit 9192719

Please sign in to comment.