From cca219d20f5c154f9d09d8cda85d9825de5c3da9 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Wed, 25 Apr 2018 17:03:04 -0400 Subject: [PATCH] progress --- werewolf/game.py | 57 +++++++++++++++++++++++++-------------- werewolf/werewolf.py | 63 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 25 deletions(-) diff --git a/werewolf/game.py b/werewolf/game.py index 8094b6a..217bf5c 100644 --- a/werewolf/game.py +++ b/werewolf/game.py @@ -26,7 +26,9 @@ class Game: day_vote_count = 3 - def __init__(self, guild: discord.Guild, role: discord.Role, game_code=None): + def __init__(self, guild: discord.Guild, role: discord.Role=None, + category: discord.CategoryChannel=None, village: discord.TextChannel=None, + game_code=None): self.guild = guild self.game_code = game_code self.game_role = role @@ -46,8 +48,8 @@ class Game: self.day_count = 0 self.ongoing_vote = False - self.channel_category = None # discord.CategoryChannel - self.village_channel = None # discord.TextChannel + self.channel_category = category # discord.CategoryChannel + self.village_channel = village # discord.TextChannel self.p_channels = {} # uses default_secret_channel self.vote_groups = {} # ID : VoteGroup() @@ -93,11 +95,16 @@ class Game: return False if self.game_role is None: - await ctx.send("Game role not configured, cannot start") - self.roles = [] - return False + try: + self.game_role = await ctx.guild.create_role(name="Players", + hoist=True, + mentionable=True, + reason="(BOT) Werewolf game role") + except (discord.Forbidden, discord.HTTPException): + await ctx.send("Game role not configured and unable to generate one, cannot start") + self.roles = [] + return False - self.started = True await self.assign_roles() # Create category and channel with individual overwrites @@ -107,19 +114,29 @@ class Game: self.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True, add_reactions=True), self.game_role: discord.PermissionOverwrite(read_messages=True, send_messages=True) } - - self.channel_category = await self.guild.create_category("ww-game", - overwrites=overwrite, - reason="(BOT) New game of werewolf") - - # for player in self.players: - # overwrite[player.member] = discord.PermissionOverwrite(read_messages=True) - - self.village_channel = await self.guild.create_text_channel("Village Square", - overwrites=overwrite, - reason="(BOT) New game of werewolf", - category=self.channel_category) - + if self.channel_category is None: + self.channel_category = await self.guild.create_category("ww-game", + overwrites=overwrite, + reason="(BOT) New game of werewolf") + else: + for target, ow in overwrite.items(): + await self.channel_category.set_permissions(target=target, + overwrite=ow, + reason="(BOT) New game of werewolf") + if self.village_channel is None: + self.village_channel = await self.guild.create_text_channel("Village Square", + overwrites=overwrite, + reason="(BOT) New game of werewolf", + category=self.channel_category) + else: + await self.village_channel.edit(name="Village Square", + category=self.channel_category, + reason="(BOT) New game of werewolf") + for target, ow in overwrite.items(): + await self.village_channel.set_permissions(target=target, + overwrite=ow, + reason="(BOT) New game of werewolf") + self.started = True # Assuming everything worked so far print("Pre at_game_start") await self._at_game_start() # This will queue channels and votegroups to be made diff --git a/werewolf/werewolf.py b/werewolf/werewolf.py index 2c230af..89420d9 100644 --- a/werewolf/werewolf.py +++ b/werewolf/werewolf.py @@ -18,7 +18,10 @@ class Werewolf: self.config = Config.get_conf(self, identifier=87101114101119111108102, force_registration=True) default_global = {} default_guild = { - "role_id": None + "role_id": None, + "category_id": None, + "channel_id": None, + "log_channel_id": None } self.config.register_global(**default_global) @@ -59,6 +62,38 @@ class Werewolf: await self.config.guild(ctx.guild).role_id.set(role.id) await ctx.send("Game role has been set to **{}**".format(role.name)) + @commands.guild_only() + @wwset.command(name="category") + async def wwset_category(self, ctx: RedContext, category_id): + """ + Assign the channel category + """ + + category = discord.utils.get(ctx.guild.categories, id=int(category_id)) + if category is None: + await ctx.send("Category not found") + return + await self.config.guild(ctx.guild).category_id.set(category.id) + await ctx.send("Channel Category has been set to **{}**".format(category.name)) + + @commands.guild_only() + @wwset.command(name="channel") + async def wwset_channel(self, ctx: RedContext, channel: discord.TextChannel): + """ + Assign the village channel + """ + await self.config.guild(ctx.guild).channel_id.set(channel.id) + await ctx.send("Game Channel has been set to **{}**".format(channel.mention)) + + @commands.guild_only() + @wwset.command(name="logchannel") + async def wwset_channel(self, ctx: RedContext, channel: discord.TextChannel): + """ + Assign the log channel + """ + await self.config.guild(ctx.guild).log_channel_id.set(channel.id) + await ctx.send("Log Channel has been set to **{}**".format(channel.mention)) + @commands.group() async def ww(self, ctx: RedContext): """ @@ -221,11 +256,29 @@ class Werewolf: return None if ctx.guild.id not in self.games or self.games[ctx.guild.id].game_over: await ctx.send("Starting a new game...") + role = None + category = None + channel = None + log_channel = None + role_id = await self.config.guild(ctx.guild).role_id() - role = discord.utils.get(ctx.guild.roles, id=role_id) - if role is None: - await ctx.send("Game role is invalid, cannot start new game") - return None + category_id = await self.config.guild(ctx.guild).category_id() + channel_id = await self.config.guild(ctx.guild).channel_id() + log_channel_id = await self.config.guild(ctx.guild).log_channel_id() + + if role_id is not None: + role = discord.utils.get(ctx.guild.roles, id=role_id) + if role is None: + await ctx.send("Game role is invalid, cannot start new game") + return None + if category_id is not None: + category = discord.utils.get(ctx.guild.categories, id=category_id) + if role is None: + await ctx.send("Game role is invalid, cannot start new game") + return None + + + self.games[ctx.guild.id] = Game(ctx.guild, role, game_code) return self.games[ctx.guild.id]