diff options
Diffstat (limited to 'bot')
| -rw-r--r-- | bot/exts/evergreen/error_handler.py | 6 | ||||
| -rw-r--r-- | bot/exts/evergreen/minesweeper.py | 7 | ||||
| -rw-r--r-- | bot/exts/evergreen/space.py | 13 | ||||
| -rw-r--r-- | bot/utils/exceptions.py | 6 | 
4 files changed, 29 insertions, 3 deletions
| diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index 33b1a3f2..459a2b2d 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -9,7 +9,7 @@ from sentry_sdk import push_scope  from bot.constants import Colours, ERROR_REPLIES, NEGATIVE_REPLIES  from bot.utils.decorators import InChannelCheckFailure, InMonthCheckFailure -from bot.utils.exceptions import BrandingError +from bot.utils.exceptions import BrandingError, UserNotPlayingError  log = logging.getLogger(__name__) @@ -103,6 +103,10 @@ class CommandErrorHandler(commands.Cog):              await ctx.send(embed=self.error_embed("You are not authorized to use this command.", NEGATIVE_REPLIES))              return +        if isinstance(error, UserNotPlayingError): +            await ctx.send("Game not found.") +            return +          with push_scope() as scope:              scope.user = {                  "id": ctx.author.id, diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index b59cdb14..ae057b30 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -7,6 +7,7 @@ import discord  from discord.ext import commands  from bot.constants import Client +from bot.utils.exceptions import UserNotPlayingError  MESSAGE_MAPPING = {      0: ":stop_button:", @@ -176,6 +177,8 @@ class Minesweeper(commands.Cog):      @minesweeper_group.command(name="flag")      async def flag_command(self, ctx: commands.Context, *coordinates: CoordinateConverter) -> None:          """Place multiple flags on the board.""" +        if ctx.author.id not in self.games: +            raise UserNotPlayingError          board: GameBoard = self.games[ctx.author.id].revealed          for x, y in coordinates:              if board[y][x] == "hidden": @@ -253,6 +256,8 @@ class Minesweeper(commands.Cog):      @minesweeper_group.command(name="reveal")      async def reveal_command(self, ctx: commands.Context, *coordinates: CoordinateConverter) -> None:          """Reveal multiple cells.""" +        if ctx.author.id not in self.games: +            raise UserNotPlayingError          game = self.games[ctx.author.id]          revealed: GameBoard = game.revealed          board: GameBoard = game.board @@ -269,6 +274,8 @@ class Minesweeper(commands.Cog):      @minesweeper_group.command(name="end")      async def end_command(self, ctx: commands.Context) -> None:          """End your current game.""" +        if ctx.author.id not in self.games: +            raise UserNotPlayingError          game = self.games[ctx.author.id]          game.revealed = game.board          await self.update_boards(ctx) diff --git a/bot/exts/evergreen/space.py b/bot/exts/evergreen/space.py index 89b31e87..3587fc00 100644 --- a/bot/exts/evergreen/space.py +++ b/bot/exts/evergreen/space.py @@ -1,6 +1,6 @@  import logging  import random -from datetime import datetime +from datetime import date, datetime  from typing import Any, Dict, Optional, Union  from urllib.parse import urlencode @@ -17,6 +17,8 @@ NASA_BASE_URL = "https://api.nasa.gov"  NASA_IMAGES_BASE_URL = "https://images-api.nasa.gov"  NASA_EPIC_BASE_URL = "https://epic.gsfc.nasa.gov" +APOD_MIN_DATE = date(1995, 6, 16) +  class DateConverter(Converter):      """Parse SOL or earth date (in format YYYY-MM-DD) into `int` or `datetime`. When invalid input, raise error.""" @@ -74,11 +76,18 @@ class Space(Cog):          # Parse date to params, when provided. Show error message when invalid formatting          if date:              try: -                params["date"] = datetime.strptime(date, "%Y-%m-%d").date().isoformat() +                apod_date = datetime.strptime(date, "%Y-%m-%d").date()              except ValueError:                  await ctx.send(f"Invalid date {date}. Please make sure your date is in format YYYY-MM-DD.")                  return +            now = datetime.now().date() +            if APOD_MIN_DATE > apod_date or now < apod_date: +                await ctx.send(f"Date must be between {APOD_MIN_DATE.isoformat()} and {now.isoformat()} (today).") +                return + +            params["date"] = apod_date.isoformat() +          result = await self.fetch_from_nasa("planetary/apod", params)          await ctx.send( diff --git a/bot/utils/exceptions.py b/bot/utils/exceptions.py index 70c20e12..dc62debe 100644 --- a/bot/utils/exceptions.py +++ b/bot/utils/exceptions.py @@ -2,3 +2,9 @@ class BrandingError(Exception):      """Exception raised by the BrandingManager cog."""      pass + + +class UserNotPlayingError(Exception): +    """Will raised when user try to use game commands when not playing.""" + +    pass | 
