You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现discord.py帮助命令的循环反应分页功能?

实现Discord.py Help命令循环分页

要解决边界页点击箭头无响应的问题,只需修改分页逻辑的判断条件,移除对页码边界的限制,替换为循环跳转的计算方式:

核心修改点

将原代码中while True循环内的页码判断逻辑替换为以下内容:

if str(reaction.emoji) == "▶️":
    # 最后一页点击前进,跳转到第一页;否则页码+1
    cur_page = cur_page + 1 if cur_page != pages else 1
    await message.edit(embed=contents[cur_page-1])
    await message.remove_reaction(reaction, user)
elif str(reaction.emoji) == "◀️":
    # 第一页点击后退,跳转到最后一页;否则页码-1
    cur_page = cur_page - 1 if cur_page > 1 else pages
    await message.edit(embed=contents[cur_page-1])
    await message.remove_reaction(reaction, user)
else:
    await message.remove_reaction(reaction, user)

修改后的完整代码

import asyncio
import datetime
import discord
from discord.ext import commands

# 假设embedcolor已定义(可根据需求替换)
embedcolor = discord.Color.blue()

@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 bot 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])
        
    await message.add_reaction("◀️")
    await message.add_reaction("▶️")

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"]

    while True:
        try:
            reaction, user = await client.wait_for("reaction_add", timeout=60, check=check)

            if str(reaction.emoji) == "▶️":
                cur_page = cur_page + 1 if cur_page != pages else 1
                await message.edit(embed=contents[cur_page-1])
                await message.remove_reaction(reaction, user)
            elif str(reaction.emoji) == "◀️":
                cur_page = cur_page - 1 if cur_page > 1 else pages
                await message.edit(embed=contents[cur_page-1])
                await message.remove_reaction(reaction, user)
            else:
                await message.remove_reaction(reaction, user)
        except asyncio.TimeoutError:
            await message.delete()
            break

说明

  • 当在最后一页点击▶️时,cur_page会被重置为1,跳转到第一页
  • 当在第一页点击◀️时,cur_page会被设置为总页数pages,跳转到最后一页
  • 保留了原有的超时自动删除消息、仅命令发起者可操作的逻辑

内容的提问来源于stack exchange,提问作者TheRealKurumi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 15:00:45