StartGame: fix: player channels correctly sorted

This commit is contained in:
Elnath 2021-07-06 10:17:14 +02:00
parent 2e7d31ad2a
commit ccda132bf5
1 changed files with 11 additions and 10 deletions

View File

@ -302,32 +302,33 @@ class Game:
}
self.config["observer_chan"] = (await game_category.create_text_channel("observers", overwrites = perms, position = 4)).id
logger.debug(f"[{self.guild}] Created observers channel")
tasks.append(asyncio.create_task(create_observers_chan()))
await asyncio.wait(tasks) # Waiting for all other channels to be created before creating player channels, since they must be placed after them
async def create_player_channels():
create_player_channel_tasks = []
# Discord channel positions are not relative to the category and not necessarily absolute, but a channel in same category than another with a higher
# position attribute will be sorted lower.
# See https://github.com/Rapptz/discord.py/issues/2392#issuecomment-707455919
observer_channel_position = self.get_observer_channel().position
async def create_player_channel(player: discord.Member):
async def create_player_channel(player: discord.Member, channel_position: int):
perms = {
self.guild.default_role: discord.PermissionOverwrite(read_messages = False),
player: discord.PermissionOverwrite(read_messages = True),
gm_role: discord.PermissionOverwrite(read_messages = True),
observer_role: discord.PermissionOverwrite(read_messages = True, send_messages = False),
}
player_channel = await game_category.create_text_channel(player.name, overwrites = perms)
player_channel = await game_category.create_text_channel(player.name, overwrites = perms, position = channel_position)
self.config["player_info"][str(player.id)]["channel"] = player_channel.id
logger.debug(f"[{self.guild.name}] Created channel for player {player.name}")
asyncio.create_task(player_channel.send(f"Hello! This is your private channel.\nIn here you can cast your votes, interact with the <@&{self.get_gm_role_id()}>, and write freely.\nHave a nice game!", allowed_mentions = discord.AllowedMentions(roles = True)))
for player in player_role.members:
create_player_channel_tasks.append(asyncio.create_task(create_player_channel(player)))
for i, player in enumerate(self.get_players()):
create_player_channel_tasks.append(asyncio.create_task(create_player_channel(player, observer_channel_position + i + 1)))
await asyncio.wait(create_player_channel_tasks)
logger.debug(f"[{self.guild.name}] Reordering player channels")
await asyncio.wait([asyncio.create_task(self.get_player_channel(player).edit(position = i + 4)) for i, player in enumerate(player_role.members)])
tasks.append(asyncio.create_task(create_player_channels()))
await asyncio.wait(tasks)
await create_player_channels()
@game_started
@save_on_success