Bugfix: fixed problem when the category had some permissions that the bot did not have.

Commit 66197c1 did not actually fix the problem.
This commit is contained in:
Elnath 2021-04-20 18:23:27 +02:00
parent 938aa6df69
commit f1e036eef9
1 changed files with 13 additions and 6 deletions

View File

@ -238,14 +238,21 @@ class VocalMaisBot(commands.Cog):
category = channel.category
# Creating the permissions of the channel, based on the watched channel's category permissions.
# However, tests showed that discord forbids the bot from creating overwrites on roles that are higher than the bot in the list of roles
bot_highest_role_position = channel.guild.get_member(self.bot.user.id).top_role.position # The position of the highest role that the bot has
# The bot can not set a permission on a channel if the bot itself does not have the permission
bot_permissions = set(perm for perm, value in channel.guild.get_member(self.bot.user.id).guild_permissions if value) # The permissions that the bot has
channel_permissions = {}
for subject, overwrite in category.overwrites.items():
if isinstance(subject, discord.Role): # Overwrites can contain role overwrites and member overwrites
if subject.position >= bot_highest_role_position: # We ignore roles higher than the bot
continue
channel_permissions[subject] = overwrite
granted_permissions = set(perm for perm, value in overwrite if value)
deny_permissions = set(perm for perm, value in overwrite if value == False) # We need to check that value is False, because None means "not specified"
# We set all the permissions that the bot can set
channel_permissions[subject] = discord.PermissionOverwrite(
**{perm: True for perm in granted_permissions if perm in bot_permissions},
**{perm: False for perm in deny_permissions if perm in bot_permissions}
)
# We add a log message if there are permissions that the bot can not set
permissions_that_bot_does_not_have = (granted_permissions.union(deny_permissions)) - bot_permissions
if len(permissions_that_bot_does_not_have) > 0:
logger.warning(f"[{discord_utils.to_string(channel.guild)}] Error: the bot can not set some of the permissions for {discord_utils.to_string(subject)} specified on category {discord_utils.to_string(category)} because it does not have them: {permissions_that_bot_does_not_have}")
# We allow the user for which we created the channel to change the channel's name
if user not in channel_permissions:
channel_permissions[user] = discord.PermissionOverwrite()