Skip to content

Commit

Permalink
[Hockey] Add a nicer error message when API access has failed
Browse files Browse the repository at this point in the history
  • Loading branch information
TrustyJAID committed Nov 9, 2023
1 parent 978c49e commit 95a0073
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 76 deletions.
28 changes: 25 additions & 3 deletions hockey/gamedaychannels.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from typing import Optional

import aiohttp
import discord
from red_commons.logging import getLogger
from redbot.core import commands
Expand Down Expand Up @@ -141,7 +142,14 @@ async def gdc_create(self, ctx: commands.Context) -> None:
await ctx.send(_("No team was setup for game day channels in this server."))
return
if await self.config.guild(ctx.guild).create_channels():
await self.create_gdc(ctx.guild)
try:
await self.create_gdc(ctx.guild)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
else:
await ctx.send(
_("You need to first toggle channel creation with `{prefix}gdc toggle`.").format(
Expand Down Expand Up @@ -258,11 +266,25 @@ async def gdc_setup(
await self.config.guild(guild).delete_gdc.set(delete_gdc)
await self.config.guild(guild).create_channels.set(True)
if team.lower() != "all":
await self.create_gdc(guild)
try:
await self.create_gdc(guild)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
else:
game_list = await Game.get_games(session=self.session)
for game in game_list:
await self.create_gdc(guild, game)
try:
await self.create_gdc(guild, game)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
await ctx.send(_("Game Day Channels for ") + team + _(" setup in ") + category.name)

#######################################################################
Expand Down
28 changes: 25 additions & 3 deletions hockey/gamedaythreads.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from typing import Optional

import aiohttp
import discord
from red_commons.logging import getLogger
from redbot.core import commands
Expand Down Expand Up @@ -163,7 +164,14 @@ async def gdt_create(self, ctx: commands.Context) -> None:
msg = _("No team was setup for game day threads in this server.")
await ctx.send(msg)
if await self.config.guild(ctx.guild).create_threads():
await self.create_gdt(ctx.guild)
try:
await self.create_gdt(ctx.guild)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
else:
msg = _("You need to first toggle thread creation with `{prefix}gdt toggle`.").format(
prefix=ctx.clean_prefix
Expand Down Expand Up @@ -254,9 +262,23 @@ async def gdt_setup(
await self.config.guild(guild).gdt_team.set(team)
await self.config.guild(guild).create_threads.set(True)
if team.lower() != "all":
await self.create_gdt(guild)
try:
await self.create_gdt(guild)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
else:
game_list = await Game.get_games(session=self.session)
try:
game_list = await Game.get_games(session=self.session)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
for game in game_list:
if game.game_state == "Postponed":
continue
Expand Down
209 changes: 143 additions & 66 deletions hockey/hockey_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List, Literal, Optional
from urllib.parse import quote

import aiohttp
import discord
from red_commons.logging import getLogger
from redbot.core import commands
Expand Down Expand Up @@ -126,7 +127,14 @@ async def standings(self, ctx: commands.Context, *, search: StandingsFinder = No
separated by division
"""
await ctx.defer()
standings = await Standings.get_team_standings(session=self.session)
try:
standings = await Standings.get_team_standings(session=self.session)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
await StandingsMenu(standings=standings, start=search).start(ctx=ctx)

@hockey_commands.command(aliases=["score"])
Expand Down Expand Up @@ -155,13 +163,20 @@ async def games(
teams = []
if team is not None:
teams = [team]
await GamesMenu(
source=Schedule(team=teams, date=date, session=self.session),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
try:
await GamesMenu(
source=Schedule(team=teams, date=date, session=self.session),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command()
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand Down Expand Up @@ -199,8 +214,14 @@ async def playoffs(
await ctx.send(_("Please select a year prior to now."))
return
season_str = int(season.group(1)) - 1

await PlayoffsView(start_date=season_str).start(ctx=ctx)
try:
await PlayoffsView(start_date=season_str).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command()
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand Down Expand Up @@ -234,20 +255,27 @@ async def heatmap(
teams = []
if team is not None:
teams = [team]
await GamesMenu(
source=Schedule(
team=teams,
date=date,
session=self.session,
include_goals=False,
include_heatmap=True,
style=style,
),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
try:
await GamesMenu(
source=Schedule(
team=teams,
date=date,
session=self.session,
include_goals=False,
include_heatmap=True,
style=style,
),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command()
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand Down Expand Up @@ -283,21 +311,28 @@ async def gameflow(
teams = []
if team is not None:
teams = [team]
await GamesMenu(
source=Schedule(
team=teams,
date=date,
session=self.session,
include_goals=False,
include_gameflow=True,
corsi=corsi,
strength=strength,
),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
try:
await GamesMenu(
source=Schedule(
team=teams,
date=date,
session=self.session,
include_goals=False,
include_gameflow=True,
corsi=corsi,
strength=strength,
),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command()
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand All @@ -322,13 +357,20 @@ async def schedule(
teams = []
if team is not None:
teams = [team]
await GamesMenu(
source=ScheduleList(team=teams, date=date, session=self.session),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
try:
await GamesMenu(
source=ScheduleList(team=teams, date=date, session=self.session),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command()
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand All @@ -353,13 +395,20 @@ async def recap(
teams = []
if team is not None:
teams = [team]
await GamesMenu(
source=ScheduleList(team=teams, date=date, session=self.session, get_recap=True),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
try:
await GamesMenu(
source=ScheduleList(team=teams, date=date, session=self.session, get_recap=True),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command(hidden=True, with_app_command=False)
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand Down Expand Up @@ -393,8 +442,15 @@ async def season(
log.verbose("season team: %s", team)
log.verbose("season team id: %s", TEAMS[team]["id"])
log.verbose("season team url: %s", url)
async with self.session.get(url) as resp:
data = await resp.json()
try:
async with self.session.get(url) as resp:
data = await resp.json()
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
games = [game for date in data["dates"] for game in date["games"]]
msg = ""
for game in games:
Expand Down Expand Up @@ -453,13 +509,20 @@ async def player(
if not player:
await ctx.send(_("No player could be found by that name."))
return
await BaseMenu(
source=PlayerPages(pages=player, season=season_str),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
try:
await BaseMenu(
source=PlayerPages(pages=player, season=season_str),
cog=self,
delete_message_after=False,
clear_reactions_after=True,
timeout=180,
).start(ctx=ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command()
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
Expand Down Expand Up @@ -504,8 +567,15 @@ async def roster(
return
players = []
url = f"{BASE_URL}/api/v1/teams/{TEAMS[team]['id']}/roster{season_url}"
async with self.session.get(url) as resp:
data = await resp.json()
try:
async with self.session.get(url) as resp:
data = await resp.json()
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return
if "roster" in data:
for player in data["roster"]:
players.append(
Expand Down Expand Up @@ -581,8 +651,15 @@ async def hockey_stats(
if not new_season.isdigit() and len(new_season) != 8:
await ctx.send(f"`{season}` is not a valid season.", ephemeral=True)
return
view = LeaderView(category, season, limit, self.session)
await view.start(ctx)
try:
view = LeaderView(category, season, limit, self.session)
await view.start(ctx)
except aiohttp.ClientConnectorError:
await ctx.send(
_("There's an issue accessing the NHL API at the moment. Try again later.")
)
log.exception("Error accessing NHL API")
return

@hockey_commands.command(hidden=True, with_app_command=False)
@commands.mod_or_permissions(manage_messages=True)
Expand Down
Loading

0 comments on commit 95a0073

Please sign in to comment.