async def add_mute_new_channel(self, channel: discord.abc.GuildChannel): """Updated the new channels mute perms once created if the guild has a mute role set.""" try: mute_role_id = self.bot.config[channel.guild.id]["mute_role_id"] except KeyError: return role = channel.guild.get_role(mute_role_id) if role is None: await self.bot.pool.execute( "UPDATE guild_settings SET mute_role_id = NULL WHERE guild_id = $1", channel.guild.id) self.bot.config[channel.guild.id]["mute_role_id"] = None return role_overwrites = channel.overwrites_for(role) role_overwrites.update(send_messages=False) with contextlib.suppress(discord.Forbidden, discord.HTTPException): await channel.set_permissions( target=role, overwrite=role_overwrites, reason="Disable mute role permissions to talk in this channel." )
def role_permissions_in(channel: discord.abc.GuildChannel, role: discord.Role) -> discord.Permissions: """Returns a role's permissions in a particular channel This function is based off of a previous solution for gathering role channel permissions. Original Solution: https://gist.github.com/e-Lisae/f20c579ab70304a73506e5565631b753 Parameters ---------- channel : discord.abc.GuildChannel The channel to get the role's permissions for role : discord.Role The role to get the permissions for Returns ------- A `discord.Permissions` object representing the role's permissions """ # gather base permissions permissions = discord.Permissions.none() permissions.value |= role.permissions.value # merge with role permission overwrites pair = channel.overwrites_for(role).pair() permissions.value = (permissions.value & ~pair[1].value) | pair[0].value return permissions
async def convert(self, ctx: context.Context, channel: discord.abc.GuildChannel) -> str: if isinstance(channel, discord.VoiceChannel): emoji = 'voice' if channel.overwrites_for(channel.guild.default_role).connect is False: emoji = 'voice_locked' else: if channel.is_news(): emoji = 'news' if channel.overwrites_for(channel.guild.default_role).read_messages is False: emoji = 'news_locked' else: emoji = 'text' if channel.is_nsfw(): emoji = 'text_nsfw' elif channel.overwrites_for(channel.guild.default_role).read_messages is False: emoji = 'text_locked' return ctx.bot.utils.channel_emojis[emoji]
async def unmute_user( self, guild: discord.Guild, channel: discord.abc.GuildChannel, author: discord.Member, user: discord.Member, reason: str, ) -> (bool, str): overwrites = channel.overwrites_for(user) perms_cache = await self.settings.member(user).perms_cache() if channel.id in perms_cache: old_values = perms_cache[channel.id] else: old_values = { "send_messages": None, "add_reactions": None, "speak": None } if all(getattr(overwrites, k) == v for k, v in old_values.items()): return False, _(mute_unmute_issues["already_unmuted"]) elif not await is_allowed_by_hierarchy(self.bot, self.settings, guild, author, user): return False, _(mute_unmute_issues["hierarchy_problem"]) overwrites.update(**old_values) try: if overwrites.is_empty(): await channel.set_permissions(user, overwrite=cast( discord.PermissionOverwrite, None), reason=reason) else: await channel.set_permissions(user, overwrite=overwrites, reason=reason) except discord.Forbidden: return False, _(mute_unmute_issues["permissions_issue"]) except discord.NotFound as e: if e.code == 10003: return False, _(mute_unmute_issues["unknown_channel"]) elif e.code == 10009: return False, _(mute_unmute_issues["left_guild"]) else: await self.settings.member(user).clear_raw("perms_cache", str(channel.id)) return True, None
async def mute_user( self, guild: discord.Guild, channel: discord.abc.GuildChannel, author: discord.Member, user: discord.Member, reason: str, ) -> (bool, str): """Mutes the specified user in the specified channel""" overwrites = channel.overwrites_for(user) permissions = channel.permissions_for(user) if permissions.administrator: return False, _(mute_unmute_issues["is_admin"]) new_overs = {} if not isinstance(channel, discord.TextChannel): new_overs.update(speak=False) if not isinstance(channel, discord.VoiceChannel): new_overs.update(send_messages=False, add_reactions=False) if all(getattr(permissions, p) is False for p in new_overs.keys()): return False, _(mute_unmute_issues["already_muted"]) elif not await is_allowed_by_hierarchy(self.bot, self.settings, guild, author, user): return False, _(mute_unmute_issues["hierarchy_problem"]) old_overs = {k: getattr(overwrites, k) for k in new_overs} overwrites.update(**new_overs) try: await channel.set_permissions(user, overwrite=overwrites, reason=reason) except discord.Forbidden: return False, _(mute_unmute_issues["permissions_issue"]) except discord.NotFound as e: if e.code == 10003: return False, _(mute_unmute_issues["unknown_channel"]) elif e.code == 10009: return False, _(mute_unmute_issues["left_guild"]) else: await self.settings.member(user).set_raw("perms_cache", str(channel.id), value=old_overs) return True, None