added decorator, decorator with args, time command added

This commit is contained in:
Bibin Muttappillil 2020-04-15 18:42:56 +02:00
parent e56a5d1629
commit b474d41087
2 changed files with 59 additions and 26 deletions

View file

@ -72,30 +72,41 @@ async def setup(ctx):
def channel_setup(command):
@functools.wraps(command)
async def wrapper(ctx):
async def wrapper(ctx, *args, **kwargs):
if ctx.channel not in game_instances:
await send_wrong(ctx, f"No game setup yet. Use {PREFIX}game setup")
else:
await command(ctx)
await command(ctx, *args, **kwargs)
return wrapper
def game_not_running(command):
@functools.wraps(command)
@channel_setup
async def wrapper(ctx, *args, **kwargs):
if game_instances[ctx.channel].running:
await send_wrong(ctx, "Sorry! A game is already running")
else:
await command(ctx, *args, **kwargs)
return wrapper
def game_running(command):
@functools.wraps(command)
@channel_setup
async def wrapper(ctx):
if game_instances[ctx.channel].running:
await send_wrong(ctx, "Sorry! A game is already running")
async def wrapper(ctx, *args, **kwargs):
if not game_instances[ctx.channel].running:
await send_wrong(ctx, "No game is running")
else:
await command(ctx)
await command(ctx, *args, **kwargs)
return wrapper
def error_handling(command):
@functools.wraps(command)
async def wrapper(ctx):
async def wrapper(ctx, *args, **kwargs):
try:
await command(ctx)
await command(ctx, *args, **kwargs)
except ValueError as error:
await send_wrong(ctx, str(error))
except asyncio.TimeoutError:
@ -104,7 +115,7 @@ def error_handling(command):
@game.command()
@game_running
@game_not_running
@error_handling
async def start(ctx):
game_instances[ctx.channel].game = bot.loop.create_task(game_instances[ctx.channel].round())
@ -112,27 +123,39 @@ async def start(ctx):
@game.command()
@game_running
@channel_setup
async def stop(ctx):
if not game_instances[ctx.channel].running:
await send_wrong(ctx, "No game is running")
else:
game_instances[ctx.channel].game.cancel()
await send_friendly(ctx, "Game canceled")
game_instances[ctx.channel].game.cancel()
await send_friendly(ctx, "Game canceled")
@game.command()
@game_running
@game_not_running
@error_handling
async def players(ctx):
await game_instances[ctx.channel].set_players(ctx.message)
@game.command()
@game_not_running
@error_handling
async def roles(ctx, *args):
await game_instances[ctx.channel].set_roles(args)
@game.command()
@game_not_running
@error_handling
async def minutes(ctx, i):
await game_instances[ctx.channel].set_time(i)
@game.command()
@game_running
@error_handling
async def roles(ctx):
await game_instances[ctx.channel].set_roles(ctx.message.content.split()[3:]) # exclude commands
async def time(ctx):
await send_friendly(ctx, game_instances[ctx.channel].remaining_time_string())
# smaller commands
@ -153,10 +176,10 @@ async def ping(ctx):
def developer(command):
@functools.wraps(command)
async def wrapper(ctx):
async def wrapper(ctx, *args, **kwargs):
DEV_ID = 461892912821698562
if ctx.author.id == DEV_ID:
await command(ctx)
await command(ctx, *args, **kwargs)
else:
await send_wrong(ctx, "This command is not for you!")
return wrapper
@ -170,8 +193,9 @@ async def logout(ctx):
@bot.command()
@developer
async def debug(ctx):
async def debug(ctx, *args):
print("DEBUG")
print(ctx.message.content)
print(ctx.args)
print(ctx.kwargs)
bot.run(TOKEN)