From 945e6750372d7f8ce34319c570aa3786fbde4bcf Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 6 Apr 2020 18:53:48 +0300 Subject: (Minesweeper): Added try-except block to reveal command. --- bot/exts/evergreen/minesweeper.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index b59cdb14..2626b1c4 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -253,7 +253,11 @@ class Minesweeper(commands.Cog): @minesweeper_group.command(name="reveal") async def reveal_command(self, ctx: commands.Context, *coordinates: CoordinateConverter) -> None: """Reveal multiple cells.""" - game = self.games[ctx.author.id] + try: + game = self.games[ctx.author.id] + except KeyError: + await ctx.author.send("Game don't exist. Can't reveal.") + return revealed: GameBoard = game.revealed board: GameBoard = game.board -- cgit v1.2.3 From 40f341f0c510973c4f432f7fddc0038674735be2 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 6 Apr 2020 19:18:03 +0300 Subject: (Space): Added check is date in range 16th June 1995 and today. --- bot/exts/evergreen/space.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bot/exts/evergreen/space.py b/bot/exts/evergreen/space.py index 89b31e87..c6d8433d 100644 --- a/bot/exts/evergreen/space.py +++ b/bot/exts/evergreen/space.py @@ -74,7 +74,12 @@ 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() + now = datetime.now().date() + apod_date = datetime.strptime(date, "%Y-%m-%d").date() + if datetime(1995, 6, 16).date() > apod_date or now < apod_date: + await ctx.send(f"Date must be in range 16th June 1995 and {now.strftime('%dth %B %Y')}.") + return + params["date"] = apod_date.isoformat() except ValueError: await ctx.send(f"Invalid date {date}. Please make sure your date is in format YYYY-MM-DD.") return -- cgit v1.2.3 From 3928c4b758db89bc68eb848e7478a53a64eb15dd Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 6 Apr 2020 19:47:21 +0300 Subject: (Space): Fixed formatting of try-except block. --- bot/exts/evergreen/space.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bot/exts/evergreen/space.py b/bot/exts/evergreen/space.py index c6d8433d..6d93a980 100644 --- a/bot/exts/evergreen/space.py +++ b/bot/exts/evergreen/space.py @@ -74,16 +74,18 @@ class Space(Cog): # Parse date to params, when provided. Show error message when invalid formatting if date: try: - now = datetime.now().date() apod_date = datetime.strptime(date, "%Y-%m-%d").date() - if datetime(1995, 6, 16).date() > apod_date or now < apod_date: - await ctx.send(f"Date must be in range 16th June 1995 and {now.strftime('%dth %B %Y')}.") - return - params["date"] = apod_date.isoformat() 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 datetime(1995, 6, 16).date() > apod_date or now < apod_date: + await ctx.send(f"Date must be in range 16th June 1995 and {now.strftime('%dth %B %Y')}.") + return + + params["date"] = apod_date.isoformat() + result = await self.fetch_from_nasa("planetary/apod", params) await ctx.send( -- cgit v1.2.3 From 25d07411ce68053b4a136ade47faf2e8396f1b3d Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 7 Apr 2020 19:59:36 +0300 Subject: (Space): Moved APOD command min date to constant, made changes in error message when date is going out of range. --- bot/exts/evergreen/space.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bot/exts/evergreen/space.py b/bot/exts/evergreen/space.py index 6d93a980..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.""" @@ -80,8 +82,8 @@ class Space(Cog): return now = datetime.now().date() - if datetime(1995, 6, 16).date() > apod_date or now < apod_date: - await ctx.send(f"Date must be in range 16th June 1995 and {now.strftime('%dth %B %Y')}.") + 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() -- cgit v1.2.3 From 005dfcaa11f4324cff4ef75b440be613e6ae9a3b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 7 Apr 2020 20:26:47 +0300 Subject: (Minesweeper): Reverted KeyError catching in reveal command, made error handler that catch it instead and attached reveal and end command to it. --- bot/exts/evergreen/minesweeper.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index 2626b1c4..776501a9 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -253,11 +253,7 @@ class Minesweeper(commands.Cog): @minesweeper_group.command(name="reveal") async def reveal_command(self, ctx: commands.Context, *coordinates: CoordinateConverter) -> None: """Reveal multiple cells.""" - try: - game = self.games[ctx.author.id] - except KeyError: - await ctx.author.send("Game don't exist. Can't reveal.") - return + game = self.games[ctx.author.id] revealed: GameBoard = game.revealed board: GameBoard = game.board @@ -282,6 +278,13 @@ class Minesweeper(commands.Cog): await game.chat_msg.edit(content=new_msg) del self.games[ctx.author.id] + @reveal_command.error + @end_command.error + async def keyerror_handler(self, ctx: commands.Context, error: typing.Any) -> None: + """Handle `KeyError` that is raised in reveal or end command when getting game.""" + if isinstance(error, KeyError): + await ctx.send("Game don't exist.") + def setup(bot: commands.Bot) -> None: """Load the Minesweeper cog.""" -- cgit v1.2.3 From 382c036d3c46acedd1786cfdc99a67fabc616e7d Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 8 Apr 2020 12:53:35 +0300 Subject: (Minesweeper): Created new exception `UserNotPlayingError`. --- bot/exts/evergreen/minesweeper.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index 776501a9..f08bad6f 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -29,6 +29,12 @@ MESSAGE_MAPPING = { log = logging.getLogger(__name__) +class UserNotPlayingError(Exception): + """Will raised when user try to use game commands when not playing.""" + + pass + + class CoordinateConverter(commands.Converter): """Converter for Coordinates.""" -- cgit v1.2.3 From 783337b7386331226646af586dd9a2bd6b738ae2 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 8 Apr 2020 12:55:34 +0300 Subject: (Minesweeper): Renamed user not playing error handler, changed `error` type hint from `typing.Any` to `Exception`, changed handling `KeyError` to handling `UserNotPlayingError`. --- bot/exts/evergreen/minesweeper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index f08bad6f..4426f0c9 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -286,9 +286,9 @@ class Minesweeper(commands.Cog): @reveal_command.error @end_command.error - async def keyerror_handler(self, ctx: commands.Context, error: typing.Any) -> None: + async def user_not_playing_handler(self, ctx: commands.Context, error: Exception) -> None: """Handle `KeyError` that is raised in reveal or end command when getting game.""" - if isinstance(error, KeyError): + if isinstance(error, UserNotPlayingError): await ctx.send("Game don't exist.") -- cgit v1.2.3 From fd88f949229885f0588a5f94f02d79268caa3471 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 8 Apr 2020 12:58:48 +0300 Subject: (Minesweeper): Added raising `UserNotPlayingError`, attached flag command to error handler. --- bot/exts/evergreen/minesweeper.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index 4426f0c9..e98e9027 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -182,6 +182,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": @@ -259,6 +261,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 @@ -275,6 +279,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) @@ -286,6 +292,7 @@ class Minesweeper(commands.Cog): @reveal_command.error @end_command.error + @flag_command.error async def user_not_playing_handler(self, ctx: commands.Context, error: Exception) -> None: """Handle `KeyError` that is raised in reveal or end command when getting game.""" if isinstance(error, UserNotPlayingError): -- cgit v1.2.3 From b08c34b2009c0440da843b1405c98b104acbf173 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 8 Apr 2020 20:01:17 +0300 Subject: (Minesweeper): Moved `UserNotPlayingError` to `bot.utils.exceptions`, removed definition in extensions. Removed custom error handler. --- bot/exts/evergreen/minesweeper.py | 15 +-------------- bot/utils/exceptions.py | 6 ++++++ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index e98e9027..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:", @@ -29,12 +30,6 @@ MESSAGE_MAPPING = { log = logging.getLogger(__name__) -class UserNotPlayingError(Exception): - """Will raised when user try to use game commands when not playing.""" - - pass - - class CoordinateConverter(commands.Converter): """Converter for Coordinates.""" @@ -290,14 +285,6 @@ class Minesweeper(commands.Cog): await game.chat_msg.edit(content=new_msg) del self.games[ctx.author.id] - @reveal_command.error - @end_command.error - @flag_command.error - async def user_not_playing_handler(self, ctx: commands.Context, error: Exception) -> None: - """Handle `KeyError` that is raised in reveal or end command when getting game.""" - if isinstance(error, UserNotPlayingError): - await ctx.send("Game don't exist.") - def setup(bot: commands.Bot) -> None: """Load the Minesweeper cog.""" 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 -- cgit v1.2.3 From 56cacaef95215f6328e8da1237f73543e2d42320 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 8 Apr 2020 20:03:27 +0300 Subject: (Error Handler): Added `UserNotPlayingError` catching to error handler. --- bot/exts/evergreen/error_handler.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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, -- cgit v1.2.3