Compare commits

...

6 Commits

Author SHA1 Message Date
Bibin Muttappillil 2216b70e34 Feat: Add emoji confirming/error reactions
If the commands throws an error then the bot marks the command message with a cross emoji reaction otherwise it adds a check mark.
(Also resolved some overshadowing warning of extension)
2021-01-04 02:32:45 +01:00
Bibin Muttappillil 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
Bibin Muttappillil 4714fe5127 Feat: Add constructor for basic_game Cog
Initializes bot like a regular Cog
Initializes an empty session dictionary
Initializes the class of the session with a possible default value
Changes the group command name to the Cog's name to avoid name clashes in commands
2021-01-04 01:14:14 +01:00
Bibin Muttappillil 1d8ed8f2ea Initial: add basic_game module 2021-01-03 01:56:30 +01:00
Bibin Muttappillil 841a6586fe feat: extension loading commands are now hidden 2021-01-02 19:39:25 +01:00
Bibin Muttappillil b8b8484466 Style: move commands up
Commands should be declared before loading further extensions
2020-12-27 22:47:10 +01:00
2 changed files with 86 additions and 20 deletions

View File

@ -10,6 +10,8 @@ __author__ = 'Bibin Muttappillil'
# standard library imports
import os
from dotenv import load_dotenv
import sys
import traceback
# discord imports
from discord.ext import commands
@ -24,27 +26,41 @@ if TOKEN is None:
bot = commands.Bot(command_prefix=commands.when_mentioned_or('$b '))
default_extensions = ['developer']
for extension in default_extensions:
@bot.event
async def on_command_error(ctx, error):
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
await ctx.message.add_reaction('')
@bot.event
async def on_command_completion(ctx):
await ctx.message.add_reaction('')
@bot.command(hidden=True)
@commands.is_owner()
async def load(ctx, ext):
bot.load_extension(f'cogs.{ext}')
print(f'cogs.{ext} loaded')
@bot.command(hidden=True)
@commands.is_owner()
async def unload(ctx, ext):
bot.unload_extension(f'cogs.{ext}')
print(f'cogs.{ext} unloaded')
@bot.command(hidden=True)
@commands.is_owner()
async def reload(ctx, ext):
bot.reload_extension(f'cogs.{ext}')
print(f'cogs.{ext} reloaded')
for extension in ['developer']:
bot.load_extension(f'cogs.{extension}')
@bot.command()
@commands.is_owner()
async def load(ctx, extension):
bot.load_extension(f'cogs.{extension}')
print(f'cogs.{extension} loaded')
@bot.command()
@commands.is_owner()
async def unload(ctx, extension):
bot.unload_extension(f'cogs.{extension}')
print(f'cogs.{extension} unloaded')
@bot.command()
@commands.is_owner()
async def reload(ctx, extension):
bot.reload_extension(f'cogs.{extension}')
print(f'cogs.{extension} reloaded')
bot.run(TOKEN)

50
src/cogs/basic_game.py Normal file
View File

@ -0,0 +1,50 @@
"""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