small fixes + correct checks

This commit is contained in:
Bibin Muttappillil 2020-07-15 18:32:29 +02:00
parent e2b8a20fa6
commit 14037bf5d8
4 changed files with 23 additions and 16 deletions

View File

@ -13,7 +13,7 @@ class Developer(commands.Cog):
await self.bot.change_presence(status=discord.Status.online, activity=discord.Game('One Night Ultimate Werewolf'))
print('We have logged in as {0.user}'.format(self.bot))
async def is_dev(ctx):
async def is_dev(self, ctx):
if ctx.author.id == 461892912821698562:
return True
await ctx.send("This command is not for you!")

View File

@ -5,6 +5,7 @@ from typing import Dict, Type
# discord imports
import discord
from discord.ext import commands
# local imports
from ..send_message import Send_message
@ -17,7 +18,7 @@ class Game_cog(Send_message):
def __init__(self, bot, game_cls: Type[Game]):
self.bot = bot
self.game_cls = game_cls
self.game_instances = Dict[discord.TextChannel, self.game_cls]
self.game_instances = {} # TODO: type hint? Dict[discord.TextChannel, self.game_cls]
async def setup_check(self, ctx):
if ctx.channel not in self.game_instances:
@ -60,8 +61,10 @@ class Game_cog(Send_message):
embed.set_footer(text="Have fun!")
await ctx.send(embed=embed)
async def pre_game_check(self, ctx):
return self.setup_check(ctx) and self.not_running_check(ctx)
def pre_game():
async def predicate(ctx):
return await ctx.cog.setup_check(ctx) and await ctx.cog.not_running_check(ctx)
return commands.check(predicate)
async def players(self, ctx):
await self.game_instances[ctx.channel].set_players(ctx.message)
@ -70,8 +73,10 @@ class Game_cog(Send_message):
self.game_instances[ctx.channel].game = self.bot.loop.create_task(self.game_instances[ctx.channel].round())
await self.game_instances[ctx.channel].game
async def in_game_check(self, ctx):
return self.setup_check(ctx) and self.running_check(ctx)
def in_game():
async def predicate(ctx):
return await ctx.cog.setup_check(ctx) and await ctx.cog.running_check(ctx)
return commands.check(predicate)
async def stop(self, ctx):
self.game_instances[ctx.channel].game.cancel()

View File

@ -19,12 +19,12 @@ class Werewolf_cog(Game_cog, commands.Cog):
@werewolf.command()
async def info(self, ctx):
"""Send information about the subcommands for the game"""
await super().info(ctx, Werewolf_game)
await super().info(ctx)
@werewolf.command()
async def setup(self, ctx):
"""This function creates an game instance for this channel"""
await super().setup(ctx, Werewolf_game)
await super().setup(ctx)
@werewolf.command()
async def reset(self, ctx):
@ -32,37 +32,38 @@ class Werewolf_cog(Game_cog, commands.Cog):
await super().reset(ctx)
@werewolf.command()
@commands.check(Game_cog.pre_game_check)
@Game_cog.pre_game()
async def players(self, ctx):
"""registers all mentioned players for the game"""
await super().players(ctx)
@werewolf.command()
@commands.check(Game_cog.pre_game_check)
@Game_cog.pre_game()
async def roles(self, ctx, *args):
"""registers roles you want to play with"""
await self.game_instances[ctx.channel].set_roles(args)
@werewolf.command()
@commands.check(Game_cog.pre_game_check)
async def minutes(self, ctx, i):
@Game_cog.pre_game()
async def minutes(self, ctx, i: int):
"""set discussion time"""
await self.game_instances[ctx.channel].set_time(i)
@werewolf.command()
@commands.check(Game_cog.pre_game_check)
@Game_cog.pre_game()
async def start(self, ctx):
print(await self.setup_check(ctx), await self.not_running_check(ctx))
"""starts a round of werewolf"""
await super().start(ctx)
@werewolf.command()
@commands.check(Game_cog.in_game_check)
@Game_cog.in_game()
async def stop(self, ctx):
"""aborts the current round of werewolf"""
await super().stop(ctx)
@werewolf.command()
@commands.check(Game_cog.in_game_check)
@Game_cog.in_game()
async def time(self, ctx):
"""checks how much discussion time there is left"""
await self.send_friendly(ctx, self.game_instances[ctx.channel].remaining_time_string())

View File

@ -39,6 +39,7 @@ class Werewolf_game:
await self.send(f"You have set the discussion time to {self.discussion_time//60} minutes") # send confirmation
def check(self):
# TODO: min. player
if not 0 <= len(self.player_list) <= 10:
raise ValueError(f"Invalid number of players: {len(self.player_list)}")
if not len(self.role_list) == (len(self.player_list) + 3):
@ -62,7 +63,7 @@ class Werewolf_game:
shuffle(self.role_list)
for i in range(len(self.player_list)):
role_obj = self.role_list[i](self, self.player_list[i])
self.player_list[i].setRole(role_obj)
self.player_list[i].set_role(role_obj)
role_obj.add_yourself()
self.middle_card = [r(self) for r in self.role_list[-3:]]