Example #1
0
    async def avatar(self,
                     ctx: commands.Context,
                     *,
                     user: discord.User = None):
        """Get user's profile picture"""
        if user is None:
            user = ctx.author

        assets = []
        member = ctx.guild.get_member(user.id)
        if member and member.guild_avatar:
            assets.append((member.guild_avatar, "Server avatar"))
        if user.avatar:
            assets.append((user.avatar, "Avatar"))
        else:
            assets.append((user.default_avatar, "Default avatar"))

        pages = []

        asset: discord.Asset
        for asset, description in assets:
            content = discord.Embed()
            content.set_author(name=f"{user} / {description}", url=asset.url)
            content.set_image(url=asset.url)
            stats = await util.image_info_from_url(self.bot.session, asset.url)
            color = await util.color_from_image_url(
                self.bot.session,
                asset.replace(size=64, format="png").url)
            content.colour = int(color, 16)
            if stats is not None:
                content.set_footer(
                    text=
                    f"{stats['filetype']} | {stats['filesize']} | {stats['dimensions']}"
                )
            pages.append(content)

        if len(pages) == 1:
            return await ctx.send(embed=pages[0])

        pages = util.TwoWayIterator(pages, loop=True)
        msg = await ctx.send(embed=pages.current())

        async def switch():
            content = pages.next()
            await msg.edit(embed=content)

        functions = {"↔️": switch}

        await util.reaction_buttons(ctx, msg, functions)
Example #2
0
    async def database_query(self, ctx, *, statement):
        """Execute something against the local MariaDB instance."""
        data = await self.bot.db.execute(statement)
        try:
            if data:
                content = "\n".join(str(r) for r in data)
                await ctx.send(f"```py\n{content}\n```")
            else:
                await ctx.send(":white_check_mark:")
        except discord.errors.HTTPException:
            # too long, page it
            pages = []
            this_page = "```py\n"
            for row in data:
                if len(this_page + str(row)) < 1993:
                    this_page += "\n" + str(row)
                else:
                    this_page += "\n```"
                    pages.append(this_page)
                    this_page = "```py\n"

            page_iterator = util.TwoWayIterator(pages)
            msg = await ctx.send(page_iterator.current())

            async def switch_page(new_page):
                await msg.edit(content=new_page)

            async def previous_page():
                content = page_iterator.previous()
                if content is not None:
                    await switch_page(content)

            async def next_page():
                content = page_iterator.next()
                if content is not None:
                    await switch_page(content)

            functions = {"⬅": previous_page, "➡": next_page}
            asyncio.ensure_future(util.reaction_buttons(ctx, msg, functions))