我的目标是让机器人对其自己的消息做出反应,然后如果发送原始命令的用户也做出反应,则在该频道中发送回复。现在,如果命令在服务器通道中发送,则该功能可以工作,但如果作为直接消息发送给机器人,它不会等待您的反应。通过打印的值user.id
and ctx.author.id
在检查过程中,我注意到机器人有时会检查它自己的反应(user.id
返回机器人的 ID)。我不确定这是否相关,因为无论如何它都会等待服务器通道中正确用户的反应。
API 文档wait_for() https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.wait_for and on_reaction_add https://discordpy.readthedocs.io/en/stable/api.html#discord.on_reaction_add
我希望此功能在服务器中工作并将消息直接发送给机器人,我是否遗漏了什么?谢谢!
@bot.command()
async def hello(ctx):
msg = await ctx.send(f'Hi {ctx.author.mention}')
await msg.add_reaction('✅')
def check(reaction, user):
print(user.id, ctx.author.id)
return reaction.message.id == msg.id and user.id == ctx.author.id and str(reaction.emoji) == '✅'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=30.0, check=check)
except asyncio.TimeoutError:
pass
else:
await ctx.send('success')
毕竟这是一个意图问题,与this https://stackoverflow.com/questions/66628327/discord-py-bot-reactions-in-dms邮政。
后intents = discord.Intents.default()
您还需要为您的应用程序启用服务器成员特权意图here https://discord.com/developers/applications并设置intents.members = True
在您的代码中为机器人提供在 DM 中工作所需的权限
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)