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)

View File

@ -1,4 +1,5 @@
from random import shuffle
import time
import asyncio
import discord
from werewolf_roles import Role, Doppelganger, Werewolf, Minion, Mason, Seer, Robber, Troublemaker, Drunk, Insomniac, Tanner, Hunter, No_role
@ -29,6 +30,10 @@ class Game:
self.role_list = [Role.match(r) for r in suggestions] # raises ValueError
await self.send(f"Roles: {', '.join(r.name() for r in self.role_list)}") # send confirmation
async def set_time(self, msg):
self.discussion_time = int(msg) * 60 + 1
await self.send(f"You have set the discussion time to {self.discussion_time//60} minutes") # send confirmation
def check(self):
if not 0 <= len(self.player_list) <= 10:
raise ValueError(f"Invalid number of players: {len(self.player_list)}")
@ -48,7 +53,6 @@ class Game:
self.voting_list = self.player_list + [No_player()]
for c in self.voting_list:
c.reset()
self.time = 0
def distribute_roles(self):
shuffle(self.role_list)
@ -80,14 +84,19 @@ class Game:
async def start_day(self):
await self.send("The day has started")
def remaining_time_string(self):
t = int(self.start_time + self.discussion_time - time.time())
return f"{t//60} minute(s) and {t%60} second(s)"
async def discussion_timer(self):
await self.send(f"You have {self.discussion_time / 60} minutes to discuss")
self.start_time = time.time()
await self.send(f"You have {self.remaining_time_string()} to discuss")
await asyncio.sleep(self.discussion_time / 2)
await self.send("Half of the time has passed")
await self.send(f"{self.remaining_time_string()} remaining")
await asyncio.sleep(self.discussion_time / 2 - 60)
await self.send("One minute remaining")
await self.send(f"{self.remaining_time_string()} remaining")
await asyncio.sleep(30)
await self.send("30 seconds remaining")
await self.send(f"{self.remaining_time_string()} remaining")
await asyncio.sleep(30)
async def early_vote(self):