我如何检测对我的帮助命令discord.py的反应



这是我的主要帮助命令:

@client.command(pass_context=True)
async def help(ctx):
embed = discord.Embed(colour=discord.Colour.blurple())
embed=discord.Embed(title="", description="This is a list of everything Otay! can do! If you need additional help with Otay! join the support server at `-support`", color=0x7289da)
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/776181059757932615/784819540496351233/otay2.png")
embed.add_field(name='***:tada: Fun*** `-helpfun`', value='`level`, `8ball`, `coinflip`, `dice`', inline=False)
embed.add_field(name='***:hammer_pick: Moderation*** `-helpmoderation`', value='`kick`, `ban`, `unban`, `nick`, `purge`, `slowmode`', inline=False)
embed.add_field(name='***:warning: Setup*** `-setup`', value='`setup`, `changeprefix`, `welcome`', inline=False)
embed.add_field(name='***🎁 Giveaway*** `-helpgiveaway`', value='`gstart [minutes] [prize]`')
embed.add_field(name='***:camera: Images*** `-helpimages`', value='`cat`, `dog`, `meme`', inline=False)
embed.add_field(name='***:regional_indicator_i: Utilities*** `-helputilities`', value='`avatar`, `ping`, `poll`, `serverinfo`, `bitcoin`', inline=False)
embed.add_field(name='***:link: Links***', value='[Invite](https://discord.com/oauth2/authorize?client_id=774687442895765535&scope=bot&permissions=-9) | [Support](https://discord.gg/452j64SdQt) | [Vote](https://top.gg/bot/774687442895765535/vote) | [Patreon](https://www.patreon.com/otay?fan_landing=true)')
embed.set_footer(icon_url=f"https://cdn.discordapp.com/attachments/776181059757932615/784819540496351233/otay2.png", text=ctx.author)
embed.timestamp = datetime.datetime.now()
await ctx.send(embed=embed)

我希望它能添加相应的表情符号,如果你按下表情符号,它会发送附加信息嵌入。我知道如何添加反应,但我不知道如何"检测";他们我在文档上找到了这个,但我仍然不知道如何才能完成

@client.event
async def on_message(message):
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('👍')

wait_for函数本质上是在等待某个事件被调用。

第一个参数是要等待的事件,它可以是on_message事件、on_reaction_add甚至on_member_join,您在不使用on_的情况下传递它

check参数是返回TrueFalse的函数,只有当检查函数返回True时,wait_for才会完成

超时就是这样,它会等待一段时间,当它结束时,它会抛出asyncio.TimeoutError

这是解释的代码

@client.command()
async def foo(ctx):
# Here I'm assigning a message I'll send to later check if the reaction was added in it
message = await ctx.send('Give me that 👍 reaction, mate')
def check(reaction, user):
""" Note: the arguments passed here are the same as in the event, 
in `on_message` it's going to be `message`, 
in `on_member_join` is going to be `member`
This returns `True` if the message where the reaction was added was the one sent before
if the reaction added was `👍`
and if the user that reacted is the one that invoked the command
Another way to write would be like so:
if reaction.message == message:
if str(reaction) == '👍':
if user == ctx.author:
return True
return False
"""
return reaction.message == message and str(reaction) == '👍' and user == ctx.author
try:
# Waiting for a reaction
reaction, user = await client.wait_for('reaction_add', check=check, timeout=60.0)
await ctx.channel.send('👍')
except asyncio.TimeoutError:
# Will send `👎` if the timeout is over
await ctx.send('👎')

编辑

reaction, user = await client.wait_for('reaction_add', check=check, timeout=60.0)
if str(reaction) == '👍':
# send some embed
elif str(reaction) == '👎':
# send some other embed

最新更新