簡體   English   中英

discord.py 嵌入帶有反應頁面的幫助命令

[英]discord.py embed help command with reaction pages

我正在嘗試制作一個包含多個頁面的幫助命令,您可以使用反應來回 go。 它工作正常,但是當您再次轉到第 2 頁和 go 時,沒有任何反應。 當您 go 到第 1 頁並嘗試返回 go 時也是如此。 我怎樣才能做到這一點,當您嘗試 go 經過最后一頁時,它會返回到第一頁,而當您嘗試 go 在第一頁之前時,它會將您帶到最后一頁?

@client.hybrid_command(name = "help", with_app_command=True, description="Get a list of commands")
@commands.guild_only()
async def help(ctx):
    pages = 2
    cur_page = 1
    roleplayembed = discord.Embed(color=embedcolor, title="Roleplay Commands")
    roleplayembed.add_field(name=f"{client.command_prefix}Cuddle", value="Cuddle a user and add a message(Optional)",inline=False)
    roleplayembed.add_field(name=f"{client.command_prefix}Hug", value="Hug a user and add a message(Optional)",inline=False)
    roleplayembed.add_field(name=f"{client.command_prefix}Kiss", value="Kiss a user and add a message(Optional)",inline=False)
    roleplayembed.add_field(name=f"{client.command_prefix}Slap", value="Slap a user and add a message(Optional)",inline=False)
    roleplayembed.add_field(name=f"{client.command_prefix}Pat", value="Pat a user and add a message(Optional)",inline=False)
    roleplayembed.set_footer(text=f"Page {cur_page} of {pages}")
    roleplayembed.timestamp = datetime.datetime.utcnow()
    basicembed = discord.Embed(color=embedcolor, title="Basic Commands")
    basicembed.add_field(name=f"{client.command_prefix}Waifu", value="Posts a random AI Generated Image of a waifu",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}8ball", value="Works as an 8 ball",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}Ara", value="Gives you a random ara ara from Kurumi Tokisaki",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}Wikipedia", value="Search something up on the wiki",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}Userinfo", value="Look up info about a user",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}Ask", value="Ask the bot a question",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}Askwhy", value="Ask the boy a question beginning with 'why'",inline=False)
    basicembed.add_field(name=f"{client.command_prefix}Avatar", value="Get a user's avatar or your own avatar",inline=False)
    basicembed.set_footer(text=f"Page {cur_page+1} of {pages}")
    basicembed.timestamp = datetime.datetime.utcnow()
    contents = [roleplayembed, basicembed]
    message = await ctx.send(embed=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 client.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(embed=contents[cur_page-1])
                await message.remove_reaction(reaction, user)

            elif str(reaction.emoji) == "◀️" and cur_page > 1:
                cur_page -= 1
                await message.edit(embed=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

避免重復,在你的循環中:

while True:

    try:
        reaction, user = await client.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) == "▶️":
            cur_page += 1
            

        elif str(reaction.emoji) == "◀️": 
            cur_page -= 1

        if cur_page > pages: #check if forward on last page
          cur_page = 1 
        elif cur_page < 1: #check if back on first page
          cur_page = pages 
        await message.edit(embed=contents[cur_page-1])
        await message.remove_reaction(reaction, user)

        
    except asyncio.TimeoutError:
        await message.delete()
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM