discord-bot/src/cogs/basic_game.py
bibin 823238ee37 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.
2021-01-04 01:28:07 +01:00

50 lines
1.2 KiB
Python

"""A base module for integrating text games into a discord cog"""
# import discord
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"""
def __init__(self, bot, session_cls=None):
self.bot = bot
self.sessions = {}
self.session_cls = Session if session_cls is None else session_cls
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"""
pass
class Display:
"""A base class for displaying game in discord"""
pass