Ejemplo n.º 1
0
    async def _list_guilds(self, ctx: commands.Context, cog: str):
        cogs = await self.config.cogs()
        if cog.lower() not in cogs:
            await ctx.send(warning(_("That cog doesn't require a whitelist to use")))
            return

        proper_name = cog_name(self.bot, cog) or cog

        def converter(page, page_id, total_pages):
            embed = discord.Embed(
                colour=ctx.me.colour,
                title=_("Whitelisted Guilds"),
                description=_("The following guilds are allowed to use {}").format(proper_name),
            )

            for guild_id in page:
                guild = self.bot.get_guild(guild_id)
                try:
                    name = escape(guild.name, formatting=True)
                except AttributeError:
                    name = inline(_("Unknown guild"))

                embed.add_field(
                    name=_("Guild #{}").format(((page_id * 10) + page.index(guild_id)) + 1),
                    value=_("**Name:** {name}\n**ID:** `{id}`").format(name=name, id=guild_id),
                    inline=False,
                )

            embed.set_footer(text=_("Page {}/{}").format(page_id + 1, total_pages))
            return embed

        await PaginateMenu(ctx, pages=chunks(cogs[cog.lower()], 6), converter=converter, actions={})
Ejemplo n.º 2
0
    async def rolemention_list(self, ctx: commands.Context):
        """List all roles setup to allow role mentions for"""
        roles = [
            x for x in [
                discord.utils.get(ctx.guild.roles, id=y)
                for y in await self.config.guild(ctx.guild).roles()
            ] if x
        ]

        if await ctx.embed_requested():
            await PaginatedMenu(
                ctx=ctx,
                pages=list(chunks(roles, 15)),
                converter=lambda page: (discord.Embed(
                    colour=ctx.me.colour,
                    description="\n".join([
                        _("{}: {} members").format(y.mention, len(y.members))
                        for y in page.data
                    ]),
                ).set_author(name=_("Mentionable Roles"),
                             icon_url=ctx.guild.icon_url).set_footer(text=_(
                                 "Page {} out of {}").format(
                                     page.current, page.total))),
            )

        else:
            await ctx.send_interactive(
                pagify(
                    tabulate(
                        [[x.name, len(x.members)] for x in roles],
                        headers=[_("Name"), _("# of Members")],
                        tablefmt="psql",
                    )),
                box_lang="",
            )
Ejemplo n.º 3
0
    async def timedrole_list(self, ctx: commands.Context):
        """List all active timed roles"""
        roles = list(chunks(await TempRole.all_roles(ctx.guild), 4))
        if not roles:
            await ctx.send(
                warning(_("This guild has no currently active timed roles")))
            return

        def convert(roles_: Sequence[TempRole], page_id: int):
            for role in roles_:
                rid = ((page_id * 4) + roles_.index(role)) + 1

                yield _(
                    "**❯** Timed role **#{id}**\n"
                    "**Member** \N{EM DASH} {member}\n"
                    "**Role** \N{EM DASH} {role}\n"
                    "**Given by** \N{EM DASH} {given_by} \N{EM DASH} {given_at}\n"
                    "**Reason** \N{EM DASH} {reason}\n"
                    "**Expires** \N{EM DASH} {expires}\n"
                    "**Total duration** \N{EM DASH} {duration}").format(
                        id=rid,
                        member=role.member.mention,
                        role=role.role.mention,
                        reason=role.reason or _("No reason specified"),
                        given_by=getattr(role.added_by, "mention",
                                         _("An unknown member")),
                        given_at=td_format(role.added_at - datetime.utcnow()),
                        expires=td_format(role.expires_at - datetime.utcnow(),
                                          append_str=True),
                        duration=td_format(role.expires_at - role.added_at),
                    )

        def page_convert(x, page, pages):
            return (discord.Embed(
                description="\n\n".join(list(convert(x, page))),
                colour=ctx.me.colour).set_footer(
                    text=_("Page {}/{}").format(page + 1, pages)).set_author(
                        name=_("Timed Roles"), icon_url=ctx.guild.icon_url))

        async with PaginateMenu(ctx,
                                pages=roles,
                                converter=page_convert,
                                actions={}):
            pass
Ejemplo n.º 4
0
    async def _list_cogs(self, ctx: commands.Context):
        cogs = await self.config.cogs()
        if not cogs:
            await ctx.send(warning(_("I have no cogs that require a whitelist to use")))
            return

        def converter(page, page_id, total_pages):
            embed = discord.Embed(colour=ctx.me.colour, title=_("Whitelisted Cogs"))

            for cog, guild_ids in page.items():
                cog = cog_name(self.bot, cog) or cog
                status = (
                    _("This guild is currently allowed to use this cog")
                    if getattr(ctx.guild, "id", None) in guild_ids
                    else _("This guild is **not** currently allowed to use this cog")
                )

                if cog not in self.bot.cogs:
                    status = _("**This cog is not currently loaded.**")

                value = _(
                    "Current whitelisted guild(s): {guilds}\n"
                    "Use `{prefix}cogwhitelist list {cog}` to the whitelisted guild(s).\n"
                    "\n"
                    "{status}"
                ).format(guilds=len(guild_ids), status=status, prefix=ctx.prefix, cog=cog)

                embed.add_field(name=cog, value=value)

            embed.set_footer(text=_("Page {}/{}").format(page_id + 1, total_pages))
            return embed

        await PaginateMenu(
            ctx,
            pages=[dict(x) for x in chunks(list(cogs.items()), 6)],
            converter=converter,
            actions={},
        )
Ejemplo n.º 5
0
    async def permissionbreakdown(self,
                                  ctx: commands.Context,
                                  member: discord.Member = None,
                                  channel=None):
        """Break down the permissions for a given member

        If more than three roles grant a single permission, only the member's top three
        roles are shown for each permission.
        """
        member = member or ctx.author
        if channel is not None:
            channel = await resolve_any(
                ctx,
                channel,
                commands.TextChannelConverter,
                commands.VoiceChannelConverter,
                commands.CategoryChannelConverter,
            )
        else:
            channel = ctx.channel
        perms: Dict[str, List[discord.Role]] = {
            x: [
                r for r in reversed(member.roles)
                if getattr(r.permissions, str(x), False) is True
            ]
            for x, y in discord.Permissions()
        }

        def converter(pg: Page):
            embed = discord.Embed(
                colour=ctx.me.colour,
                title=_("Permission Breakdown"),
                description=_("Displaying permissions for member {}").format(
                    member.mention),
            )

            # noinspection PyShadowingNames
            for perm, roles in pg.data:
                roles = [mention(x) for x in roles[:3]]
                if not roles:
                    value = [
                        _("This permission is not granted by any of {}'s roles"
                          ).format(member.mention)
                    ]
                else:
                    value = [
                        " \N{EM DASH} ".join([
                            (_("Granted by {} roles") if len(roles) != 1 else
                             _("Granted by {} role")).format(len(roles)),
                            ", ".join(roles),
                        ])
                    ]

                overwrites: Dict[Union[discord.Member, discord.Role],
                                 Optional[bool]] = {
                                     x: getattr(y, perm, None)
                                     for x, y in channel.overwrites
                                     if x == member or x in member.roles
                                     and getattr(y, perm, None) is not None
                                 }
                if overwrites:
                    granted = [x for x, y in overwrites.items() if y is True]
                    denied = [x for x, y in overwrites.items() if y is False]
                    if granted:
                        value.append(" ".join([
                            (_("Granted by {} overwrites for \N{EM DASH}")
                             if len(granted) != 1 else
                             _("Granted by {} overwrite for \N{EM DASH}")
                             ).format(len(granted)),
                            ", ".join(mention(x) for x in granted[:3]),
                        ]))
                    if denied:
                        value.append(" ".join([
                            (_("Denied by {} overwrites for \N{EM DASH}")
                             if len(denied) != 1 else
                             _("Denied by {} overwrite for \N{EM DASH}")
                             ).format(len(denied)),
                            ", ".join(mention(x) for x in denied[:3]),
                        ]))

                embed.add_field(name=format_permission(perm),
                                value="\n".join(value),
                                inline=False)

            return embed.set_footer(text=_("Page {current} out of {total}").
                                    format(current=pg.current, total=pg.total))

        await PaginatedMenu(
            ctx=ctx,
            pages=list(chunks(list(perms.items()), round(len(perms) / 4))),
            converter=converter,
            wrap_around=True,
        )