Skip to main content

Button paginator using discord-py-interactions

Project description

SlashPaginator

discord-py-interactions

Button paginator using discord-py-interactions

Welcome!

It's a very simple paginator for discord-py-interactions!

This project is open source ⭐.

Install

pip install --upgrade SlashPaginator

Example

@slash.slash(name="example")
async def _example(ctx):
    embed1 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 1")
    embed2 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 2")
    embed3 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 3")
    paginator = SlashPaginator.AutoSlashEmbedPaginator(ctx)
    embeds = [embed1, embed2, embed3]
    await paginator.run(embeds)

The AutoSlashEmbedPaginator uses the lib's buttons to scroll. If given only one page, it just acts as a glorified ctx.send(embed=embeds) message.

The CustomAutoSlashPaginator is a subclass of AutoSlashEmbedPaginator that lets you:

  • Customise what buttons you want to use, instead of the default.
  • Customize what functions the buttons should use instead.

The caveat with the custom object is that it requires learning about how to use components in the lib. You may refer here to learn more.

Custom example (Reimplementing AutoSlashEmbedPaginator without the freeze page button):

class MyOwnPaginator(SlashPaginator.CustomAutoSlashPaginator):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        
    async def button_1_action(self, button_ctx):
        """Seeks to the first page."""
        self.current_page = 0
        if self.remove_reactions:
            try:
                await button_ctx.edit_origin(components=[])
            except:
                pass
        if self.auto_footer:
            self.embeds[0].set_footer(
                text=f"({self.current_page + 1}/{len(self.embeds)})"
            )
        await button_ctx.edit_origin(embed=self.embeds[0])
    async def button_2_action(self, button_ctx):
        """Seeks to the previous page."""
        self.current_page = self.current_page - 1
        self.current_page = (
            0 if self.current_page < 0 else self.current_page
        )
        if self.remove_reactions:
            try:
                await button_ctx.edit_origin(components=[])
            except:
                pass
        if self.auto_footer:
            self.embeds[self.current_page].set_footer(
                text=f"({self.current_page + 1}/{len(self.embeds)})"
            )
        await button_ctx.edit_origin(embed=self.embeds[self.current_page])
    async def button_3_action(self, button_ctx):
        """Seeks to the next page."""
        self.current_page = self.current_page + 1
        self.current_page = (
            len(self.embeds) - 1
            if self.current_page > len(self.embeds) - 1
            else self.current_page
        )
        if self.remove_reactions:
            try:
                await button_ctx.edit_origin(components=[])
            except:
                pass
        if self.auto_footer:
            self.embeds[self.current_page].set_footer(
                text=f"({self.current_page + 1}/{len(self.embeds)})"
            )
        await button_ctx.edit_origin(embed=self.embeds[self.current_page])
    async def button_4_action(self, button_ctx):
        """Seeks to the last page."""
        self.current_page = len(self.embeds) - 1
        if self.remove_reactions:
            try:
                await button_ctx.edit_origin(components=[])
            except:
                pass
        if self.auto_footer:
            self.embeds[len(self.embeds) - 1].set_footer(
                text=f"({self.current_page + 1}/{len(self.embeds)})"
            )
        await button_ctx.edit_origin(
            embed=self.embeds[len(self.embeds) - 1]
        )
        

@slash.slash(name="example")
async def _example(ctx):
    embed1 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 1")
    embed2 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 2")
    embed3 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 3")
    emojis = ["⏮️", "◀", "▶", "⏭️"]  # first page, prev page, next page, last page
    embeds = [embed1, embed2, embed3]
    paginator = MyOwnPaginator(ctx, control_emojis=emojis)
    
    await paginator.run(embeds)

Custom Example #2

(Redoing the look of the buttons, but keep the functionality intact.)

@slash.slash(name="example")
async def _example(ctx):
    embed1 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 1")
    embed2 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 2")
    embed3 = discord.Embed(color=ctx.author.color).add_field(name="Example", value="Page 3")
    emojis = ["⏮️", "◀", "⏹️", "▶", "⏭️"]  # first page, prev page, stop, next page, last page
    embeds = [embed1, embed2, embed3]
    paginator = SlashPaginator.CustomAutoSlashPaginator(ctx, control_emojis=emojis, default_run=True)
    
    await paginator.run(embeds)

License

This project is under the MIT License.

Contribute

Anyone can contribute to this by forking the repository, making a change, and create a pull request!

Make sure you run it under the black formatter first :)

Credits to:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SlashPaginator-0.0.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

SlashPaginator-0.0.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file SlashPaginator-0.0.3.tar.gz.

File metadata

  • Download URL: SlashPaginator-0.0.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for SlashPaginator-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5058c887b6b2225c02b14cdd24e009163854b1dd10a7a02e34cd92c45fd620dc
MD5 39752620a96f5b0379f450324260b577
BLAKE2b-256 ea943e4c38a23a58bd87ea1bd169b6cd07f31b62ddde1f5e34b08ccc82264fd3

See more details on using hashes here.

File details

Details for the file SlashPaginator-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: SlashPaginator-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for SlashPaginator-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 903ff7e233dcc387b85cf91e1fb4e454aa6b6c5de8eb360e7bec14cb1877103a
MD5 a0fe600b4b634cf04e773d34dbdf0da5
BLAKE2b-256 04cf73d49d125288af611d48d82e27d57dfd29c21c7ff79ce22f010eb6a93689

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page