Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python discord next page

@bot.command()
async def pages(ctx):
    contents = ["This is page 1!", "This is page 2!", "This is page 3!", "This is page 4!"]
    pages = 4
    cur_page = 1
    message = await ctx.send(f"Page {cur_page}/{pages}:
{contents[cur_page-1]}")
    # getting the message object for editing and reacting

    await message.add_reaction("◀️")
    await message.add_reaction("▶️")

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]
        # This makes sure nobody except the command sender can interact with the "menu"

    while True:
        try:
            reaction, user = await bot.wait_for("reaction_add", timeout=60, check=check)
            # waiting for a reaction to be added - times out after x seconds, 60 in this
            # example

            if str(reaction.emoji) == "▶️" and cur_page != pages:
                cur_page += 1
                await message.edit(content=f"Page {cur_page}/{pages}:
{contents[cur_page-1]}")
                await message.remove_reaction(reaction, user)

            elif str(reaction.emoji) == "◀️" and cur_page > 1:
                cur_page -= 1
                await message.edit(content=f"Page {cur_page}/{pages}:
{contents[cur_page-1]}")
                await message.remove_reaction(reaction, user)

            else:
                await message.remove_reaction(reaction, user)
                # removes reactions if the user tries to go forward on the last page or
                # backwards on the first page
        except asyncio.TimeoutError:
            await message.delete()
            break
            # ending the loop if user doesn't react after x seconds
Comment

PREVIOUS NEXT
Code Example
Python :: get the first principle component of pca 
Python :: flask-sqlalchemy inserting a dictionary to a database 
Python :: converter json em form-data-encoded python 
Python :: ring Delete Item From List 
Python :: ring Date and Time Clock 
Python :: dictionary, accepting similar words solution 
Python :: while loop using increment 
Python :: ring Load Syntax Files 
Python :: pandas rolling list 
Python :: ring Desktop, WebAssembly and Mobile Using QTreeView and QFileSystemModel 
Python :: install open3d jetson nano aarch64 
Python :: Error: Directory not empty @ dir_s_rmdir - /usr/local/Cellar/python/3.7.3 
Python :: webdriver antibot 
Python :: modules django 
Python :: python strip txt 
Python :: colorgram.py 1.2.0 
Python :: gun in python turtle 
Python :: bar plot with patterns colors 
Python :: rolingmean python 
Python :: python how to compress pytorch model 
Python :: code help 
Python :: multiplication table in python 
Python :: identifying strings python 
Python :: correct code to read csv file in python 
Python :: demploy django in vps 
Python :: /var/www/html/flag 
Python :: python loop increment by 2 
Python :: permutation test python 
Python :: adding multiple items to a list python 
Python :: RRRR INSTEAD YYYY 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =