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(-) (limited to 'bot/exts/evergreen/minesweeper.py') 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 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(-) (limited to 'bot/exts/evergreen/minesweeper.py') 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(+) (limited to 'bot/exts/evergreen/minesweeper.py') 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(-) (limited to 'bot/exts/evergreen/minesweeper.py') 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(+) (limited to 'bot/exts/evergreen/minesweeper.py') 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(-) (limited to 'bot/exts/evergreen/minesweeper.py') 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