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.
This commit is contained in:
Bibin Muttappillil 2021-01-04 01:28:07 +01:00
parent 4714fe5127
commit 823238ee37
1 changed files with 25 additions and 0 deletions

View File

@ -4,6 +4,17 @@
from discord.ext import commands 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): class Cog(commands.Cog):
"""A base game cog that has a collection of basic game commands and checkers""" """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 = next(c for c in self.get_commands() if c.name == 'group')
self.group.name = self.qualified_name.lower() 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: class Session:
"""A base class for holding channel specific information and setting up a game""" """A base class for holding channel specific information and setting up a game"""