From 823238ee376ed0d9fd0b53adef96c3493a7a9a87 Mon Sep 17 00:00:00 2001 From: bibin Date: Mon, 4 Jan 2021 01:28:07 +0100 Subject: [PATCH] Feat: Add setup command with check and error The `setup` command creates a game instance for a channel. It also has a check to see if one is already setup and a error handler to deal in that case. I don't think that the check functions can be methods so the 'hack' is to use `ctx.cog` instead of `self` to get the cog instance. There is also a function to check for the inverted condition preventing double defining every check. --- src/cogs/basic_game.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/cogs/basic_game.py b/src/cogs/basic_game.py index 30a52fd..426a52d 100644 --- a/src/cogs/basic_game.py +++ b/src/cogs/basic_game.py @@ -4,6 +4,17 @@ from discord.ext import commands +# checks + + +def is_not(check): + return lambda ctx: not check(ctx) + + +def is_setup(ctx): + return ctx.channel in ctx.cog.sessions + + class Cog(commands.Cog): """A base game cog that has a collection of basic game commands and checkers""" @@ -14,6 +25,20 @@ class Cog(commands.Cog): self.group = next(c for c in self.get_commands() if c.name == 'group') self.group.name = self.qualified_name.lower() + @commands.group(invoke_without_command=True) + async def group(self, ctx): + await ctx.send("try info") + + @group.command() + @commands.check(is_not(is_setup)) + async def setup(self, ctx): + """Creates a game instance for this channel""" + self.sessions[ctx.channel] = self.session_cls() + + @setup.error + async def setup_handler(self, ctx, error): + await ctx.send(f"A game of is already setup in this channel") + class Session: """A base class for holding channel specific information and setting up a game"""