我正在尝试为我的discord.py创建一个帮助菜单(我使用的是discord.py V2.0,但是在这方面并没有太大的区别),然而,我的部分代码有一个很大的错误。它都正常运行,输出/响应正确的内容,但是,它将响应任何活动的帮助消息(任何未超时的消息)。
我所附的照片显示了这两条帮助消息(使用不同的命令创建),问题是,即使它们是单独创建的,它也会对这两条消息(以及存在的任何其他消息)的反应做出响应。

这是我的代码:
@commands.command()
async def help(self, ctx, command=None):
p = cluster["Atomic_Developer_Bot"]["prefix"].find_one({"guild_id": ctx.guild.id})["prefix"]
messageid = ctx.message.id
# ------------------ IF COMMAND == NONE --------------------------- #
page_one = discord.Embed(title="\u200b", color=0x008000)
page_one: Embed = discord.Embed(
colour=discord.Color.teal()
)
page_one.add_field(name="**Atomic Help**", value=f"Welcome to Atomic!\nFor help with individual commands please do **{p}help <command>**\n \nFor more support with the bot do **+support**\n \nUse the reactions below to navigate the help message!")
page_one.timestamp = datetime.datetime.utcnow()
page_one.set_footer(text='Page 1/5 \u200b')
page_two = discord.Embed(title="\u200b", color=0x008000)
page_two: Embed = discord.Embed(
description=("**Setup Commands**"),
colour=discord.Color.teal()
)
page_two.add_field(name=f'**{p}setup** - Detailed description of the below commands', value=f"Enable logging commands:\n```{p}enablemod,{p}enablemessage,{p}enablemember,{p}enablevoice,{p}enablechannel,{p}enablejoin,{p}enableleave,{p}enablewelcome,{p}enablegoodbye,{p}enableall```\n \nDisable logs commands:\n```{p}disablemod,{p}disablemessage,{p}disablemember,{p}disablevoice,{p}disablechannel,{p}disablejoin,{p}disableleave,{p}disablewelcome,{p}disablegoodbye,{p}disableall```")
page_two.timestamp = datetime.datetime.utcnow()
page_two.set_footer(text='Page 2/5 \u200b')
page_three = discord.Embed(title="\u200b", color=0x008000)
page_three: Embed = discord.Embed(
description=('**Fun Commands**'),
colour=discord.Color.teal()
)
page_three.add_field(name=f'**{p}funhelp** - Detailed description of the below commands', value=f"Fun commands:\n ```{p}8ball,{p}topic,{p}coin,{p}insta,{p}tiktok,{p}avatar,{p}meme,{p}claptext```\n \nAnimal Commands:\n```{p}cat,{p}fox,{p}panda,{p}redpanda,{p}koala,{p}dog,{p}bird```")
page_three.timestamp = datetime.datetime.utcnow()
page_three.set_footer(text='Page 3/5 \u200b')
page_four = discord.Embed(title="\u200b", color=0x008000)
page_four: Embed = discord.Embed(
description=('**Utility Commands**'),
colour=discord.Color.teal()
)
page_four.add_field(name=f'**{p}utilityhelp** - Detailed description of the below commands', value=f'Utility commands:\n```{p}serverinfo,{p}userinfo,{p}support,{p}invite,{p}suggest,{p}error,{p}changeprefix,{p}partner,{p}credits,{p}status```')
page_four.timestamp = datetime.datetime.utcnow()
page_four.set_footer(text='Page 4/5 \u200b')
page_five = discord.Embed(title="\u200b", color=0x008000)
page_five: Embed = discord.Embed(
description=('**Moderation Commands**'),
colour=discord.Color.teal()
)
page_five.add_field(name=f'**{p}modhelp** - Detailed description of the below commands', value=f'Moderation commands:\n```{p}warn,{p}ban,{p}kick,{p}removewarn,{p}clearwarns,{p}unban,{p}mute,{p}unmute,{p}tempmute,{p}note,{p}removenote,{p}purge```')
page_five.timestamp = datetime.datetime.utcnow()
page_five.set_footer(text='Page 5/5 \u200b')
if command == None:
contents = [page_one, page_two, page_three, page_four, page_five]
pages = 5
cur_page = 1
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("▶️")
if messageid == ctx.message.id:
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 self.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(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:
pass
# ending the loop if user doesn't react after x seconds发生这种情况时不存在错误。
抱歉,这句话措辞不太好。任何帮助都是非常感谢的。
发布于 2021-10-20 16:33:39
在您的检查中,您可以指定该反应属于哪个消息。您之前已经将您的消息定义为message,因此您只需检查它是否正确。
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"] and reaction.message == message我现在不能亲自测试这个,很抱歉,但它应该能用。希望这有点有用。
https://stackoverflow.com/questions/69649767
复制相似问题