Пример #1
0
 def cog_check(self, ctx: Context) -> bool:
     """Only allow moderators from moderator channels to invoke the commands in this cog."""
     checks = [
         with_role_check(ctx, *constants.MODERATION_ROLES),
         in_channel_check(ctx, *constants.MODERATION_CHANNELS)
     ]
     return all(checks)
Пример #2
0
def test_with_role_check_with_guild_with_required_role(context):
    context.guild = True
    role = MagicMock()
    role.id = 42
    context.author.roles = (role,)

    assert checks.with_role_check(context, role.id)
Пример #3
0
    async def user_info(self, ctx: Context, user: Member = None) -> None:
        """Returns info about a user."""
        if user is None:
            user = ctx.author

        # Do a role check if this is being executed on someone other than the caller
        elif user != ctx.author and not with_role_check(ctx, *constants.MODERATION_ROLES):
            await ctx.send("You may not use this command on users other than yourself.")
            return

        # Non-staff may only do this in #bot-commands
        if not with_role_check(ctx, *constants.STAFF_ROLES):
            if not ctx.channel.id == constants.Channels.bot_commands:
                raise InChannelCheckFailure(constants.Channels.bot_commands)

        embed = await self.create_user_embed(ctx, user)

        await ctx.send(embed=embed)
Пример #4
0
 def cog_check(self, ctx: commands.Context) -> bool:
     """Only allow moderators to invoke the commands in this cog."""
     checks = [
         with_role_check(ctx, *constants.MODERATION_ROLES),
         in_whitelist_check(
             ctx,
             channels=[constants.Channels.dm_log],
             redirect=None,
             fail_silently=True,
         )
     ]
     return all(checks)
Пример #5
0
 def cog_check(self, ctx: Context) -> bool:
     """Only allow moderators inside moderator channels to invoke the commands in this cog."""
     checks = [
         with_role_check(ctx, *constants.MODERATION_ROLES),
         in_whitelist_check(
             ctx,
             channels=constants.MODERATION_CHANNELS,
             categories=[constants.Categories.modmail],
             redirect=None,
             fail_silently=True,
         )
     ]
     return all(checks)
Пример #6
0
    async def dormant_check(self, ctx: commands.Context) -> bool:
        """Return True if the user is the help channel claimant or passes the role check."""
        if self.help_channel_claimants.get(ctx.channel) == ctx.author:
            log.trace(f"{ctx.author} is the help channel claimant, passing the check for dormant.")
            self.bot.stats.incr("help.dormant_invoke.claimant")
            return True

        log.trace(f"{ctx.author} is not the help channel claimant, checking roles.")
        role_check = with_role_check(ctx, *constants.HelpChannels.cmd_whitelist)

        if role_check:
            self.bot.stats.incr("help.dormant_invoke.staff")

        return role_check
Пример #7
0
    async def _can_modify(self, ctx: Context, reminder_id: t.Union[str, int]) -> bool:
        """
        Check whether the reminder can be modified by the ctx author.

        The check passes when the user is an admin, or if they created the reminder.
        """
        if with_role_check(ctx, Roles.admins):
            return True

        api_response = await self.bot.api_client.get(f"bot/reminders/{reminder_id}")
        if not api_response["author"] == ctx.author.id:
            log.debug(f"{ctx.author} is not the reminder author and does not pass the check.")
            await send_denial(ctx, "You can't modify reminders of other users!")
            return False

        log.debug(f"{ctx.author} is the reminder author and passes the check.")
        return True
Пример #8
0
 def cog_check(self, ctx: Context) -> bool:
     """Only allow moderators to invoke the commands in this cog."""
     return with_role_check(ctx, *constants.MODERATION_ROLES)
Пример #9
0
 def cog_check(self, ctx: Context) -> bool:
     """Only allow moderators and core developers to invoke the commands in this cog."""
     return with_role_check(ctx, *MODERATION_ROLES, Roles.core_developers)
Пример #10
0
    async def user_info(self,
                        ctx: Context,
                        user: Member = None,
                        hidden: bool = False):
        """
        Returns info about a user.
        """

        if user is None:
            user = ctx.author

        # Do a role check if this is being executed on someone other than the caller
        if user != ctx.author and not with_role_check(ctx, *MODERATION_ROLES):
            await ctx.send(
                "You may not use this command on users other than yourself.")
            return

        # Non-moderators may only do this in #bot-commands and can't see hidden infractions.
        if not with_role_check(ctx, *STAFF_ROLES):
            if not ctx.channel.id == Channels.bot:
                raise InChannelCheckFailure(Channels.bot)
            # Hide hidden infractions for users without a moderation role
            hidden = False

        # User information
        created = time_since(user.created_at, max_units=3)

        name = str(user)
        if user.nick:
            name = f"{user.nick} ({name})"

        # Member information
        joined = time_since(user.joined_at, precision="days")

        # You're welcome, Volcyyyyyyyyyyyyyyyy
        roles = ", ".join(role.mention for role in user.roles
                          if role.name != "@everyone")

        # Infractions
        infractions = await self.bot.api_client.get('bot/infractions',
                                                    params={
                                                        'hidden': str(hidden),
                                                        'user__id':
                                                        str(user.id)
                                                    })

        infr_total = 0
        infr_active = 0

        # At least it's readable.
        for infr in infractions:
            if infr["active"]:
                infr_active += 1

            infr_total += 1

        # Let's build the embed now
        embed = Embed(title=name,
                      description=textwrap.dedent(f"""
                **User Information**
                Created: {created}
                Profile: {user.mention}
                ID: {user.id}

                **Member Information**
                Joined: {joined}
                Roles: {roles or None}

                **Infractions**
                Total: {infr_total}
                Active: {infr_active}
            """))

        embed.set_thumbnail(url=user.avatar_url_as(format="png"))
        embed.colour = user.top_role.colour if roles else Colour.blurple()

        await ctx.send(embed=embed)
Пример #11
0
def test_with_role_check_without_guild(context):
    context.guild = None

    assert not checks.with_role_check(context)
Пример #12
0
 def test_with_role_check_without_required_roles(self):
     """`with_role_check` returns `False` if `Context.author` lacks the required role."""
     self.ctx.author.roles = []
     self.assertFalse(checks.with_role_check(self.ctx))
Пример #13
0
 def test_with_role_check_without_guild(self):
     """`with_role_check` returns `False` if `Context.guild` is None."""
     self.ctx.guild = None
     self.assertFalse(checks.with_role_check(self.ctx))
Пример #14
0
    async def user_info(self,
                        ctx: Context,
                        user: Member = None,
                        hidden: bool = False):
        """
        Returns info about a user.
        """

        # Do a role check if this is being executed on
        # someone other than the caller
        if user and user != ctx.author:
            if not with_role_check(ctx, *MODERATION_ROLES):
                raise BadArgument(
                    "You do not have permission to use this command on users other than yourself."
                )

        # Non-moderators may only do this in #bot-commands
        if not with_role_check(ctx, *MODERATION_ROLES):
            if not ctx.channel.id == Channels.bot:
                raise MissingPermissions("You can't do that here!")

        # Validates hidden input
        hidden = str(hidden)

        if user is None:
            user = ctx.author

        # User information
        created = time_since(user.created_at, max_units=3)

        name = f"{user.name}#{user.discriminator}"
        if user.nick:
            name = f"{user.nick} ({name})"

        # Member information
        joined = time_since(user.joined_at, precision="days")

        # You're welcome, Volcyyyyyyyyyyyyyyyy
        roles = ", ".join(role.mention for role in user.roles
                          if role.name != "@everyone")

        # Infractions
        api_response = await self.bot.http_session.get(
            url=URLs.site_infractions_user.format(user_id=user.id),
            params={"hidden": hidden},
            headers=self.headers)

        infractions = await api_response.json()

        infr_total = 0
        infr_active = 0

        # At least it's readable.
        for infr in infractions:
            if infr["active"]:
                infr_active += 1

            infr_total += 1

        # Let's build the embed now
        embed = Embed(title=name,
                      description=textwrap.dedent(f"""
                **User Information**
                Created: {created}
                Profile: {user.mention}
                ID: {user.id}

                **Member Information**
                Joined: {joined}
                Roles: {roles or None}

                **Infractions**
                Total: {infr_total}
                Active: {infr_active}
            """))

        embed.set_thumbnail(url=user.avatar_url_as(format="png"))
        embed.colour = user.top_role.colour if roles else Colour.blurple()

        await ctx.send(embed=embed)
Пример #15
0
def test_with_role_check_with_guild_without_required_role(context):
    context.guild = True
    context.author.roles = []

    assert not checks.with_role_check(context)
Пример #16
0
 async def predicate(ctx: Context):
     return with_role_check(ctx, *role_ids)
Пример #17
0
 async def predicate(ctx: Context) -> bool:
     """With role checker predicate."""
     return with_role_check(ctx, *role_ids)
Пример #18
0
 def test_with_role_check_with_guild_and_required_role(self):
     """`with_role_check` returns `True` if `Context.author` has the required role."""
     self.ctx.author.roles.append(MockRole(id=10))
     self.assertTrue(checks.with_role_check(self.ctx, 10))