Пример #1
0
 async def on_guild_channel_create(self, channel: discord.abc.GuildChannel):
     guild: discord.Guild = channel.guild
     roleid = Configuration.get_var(guild.id, "MUTE_ROLE")
     if roleid is not 0:
         role = guild.get_role(roleid)
         if role is not None and channel.permissions_for(
                 guild.me).manage_channels:
             if isinstance(channel, discord.TextChannel):
                 try:
                     await channel.set_permissions(
                         role,
                         reason=Translator.translate(
                             'mute_setup', guild.id),
                         send_messages=False,
                         add_reactions=False)
                 except discord.Forbidden:
                     pass
             else:
                 try:
                     await channel.set_permissions(
                         role,
                         reason=Translator.translate(
                             'mute_setup', guild.id),
                         speak=False,
                         connect=False)
                 except discord.Forbidden:
                     pass
Пример #2
0
 async def setup_overwrites(self, role: discord.Role, channel: discord.abc.GuildChannel):
     # noinspection PyUnresolvedReferences
     if not channel.permissions_for(channel.guild.me).manage_roles:
         return
     await channel.set_permissions(
         target=role,
         overwrite=self.OVERWRITE_PERMISSIONS,
         reason=translate("setup_audit_reason"),
     )
Пример #3
0
 async def on_guild_channel_create(self, channel: discord.abc.GuildChannel):
     # noinspection PyUnresolvedReferences
     guild: discord.Guild = channel.guild
     if not channel.permissions_for(guild.me).manage_roles:
         return
     role = await self.get_punished_role(guild)
     if not role:
         return
     await self.setup_overwrites(role, channel)
Пример #4
0
    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
Пример #5
0
 async def on_guild_channel_create(self, channel: discord.abc.GuildChannel):
     guild: discord.Guild = channel.guild
     roleid = Configuration.getConfigVar(guild.id, "MUTE_ROLE")
     if roleid is not 0:
         role = discord.utils.get(guild.roles, id=roleid)
         if role is not None and channel.permissions_for(
                 guild.me).manage_channels:
             if isinstance(channel, discord.TextChannel):
                 await channel.set_permissions(
                     role,
                     reason="Automatic mute role setup",
                     send_messages=False,
                     add_reactions=False)
             else:
                 await channel.set_permissions(
                     role,
                     reason="Automatic mute role setup",
                     speak=False,
                     connect=False)
Пример #6
0
def verify_channel_visibility(channel: discord.abc.GuildChannel, user: discord.Member) -> bool:
    """Checks whether a user can see a channel.

    The Discord API provides a full list of channels including ones normally invisible to the
    client. This helps hide channels that a bot or user shouldn't see.

    Args:
        channel: discord.py channel.
        user: discord.py server member.

    Returns:
        ``True`` if the user can view and use the given channel.

    """
    permissions = channel.permissions_for(user)
    if channel.type is discord.ChannelType.voice:
        # read_messages is actually view_channel in the official API
        # discord.py mislabeled this, maybe not realizing voice channels use it too
        return (permissions.connect and permissions.read_messages)
    return permissions.read_messages
Пример #7
0
async def validate_notification_channel(ctx,
                                        channel: discord.abc.GuildChannel):
    """Returns True if channel is valid"""

    if not channel:
        return channel

    # If there isn't a guild aka it's a PrivateGuild
    if not ctx.guild:
        raise errors.InvalidChannelError("This command doesn't work here.")

    # Only people with manage_channels or with the Notification Manager role can subscribe channels
    perms = channel.permissions_for(ctx.author)
    role = discord.utils.find(lambda r: r.name == 'Notification Manager',
                              ctx.author.roles)
    if not role and not perms.manage_channels:
        raise errors.InvalidChannelError(
            "You don't have the Manage Channels permission to subscribe the channel."
        )

    return channel
Пример #8
0
 async def _channel_is_sendable_to_you(
         self, channel: discord.abc.GuildChannel) -> bool:
     our_permissions = channel.permissions_for(
         await channel.guild.fetch_member(self.app.discord_client.user.id))
     return our_permissions.send_messages