如何通过 discord.py 重写从提到的用户那里获取输入?
How to get input from mentioned user with discord.py rewrite?
我正在尝试为我最近开始编程的 discord.py 机器人添加 marry 命令。我希望提到的用户能够回复机器人。
截至目前的代码,它只回复我。
@client.command()
async def marry(ctx, member: discord.Member):
await ctx.send(f"{ctx.author.mention} **proposes to** {member.mention} **Do you accept??** "
f"\nRespond with [y(es)/n(o)]")
def check(m):
return m.author == ctx.author
try:
msg = await client.wait_for('message', check=check, timeout=10)
if msg.content.lower() in ['y', 'yes']:
await ctx.send(f"Congratulations! {ctx.author.mention} and {member.mention} are now married to each other!")
elif msg.content.lower() in ['n', 'no']:
await ctx.send(f"Unlucky, maybe another time! {ctx.author.mention}")
else:
await ctx.send("I did not understand that, aborting!")
except asyncio.TimeoutError as e:
print(e)
await ctx.send("Looks like you waited too long.")
有谁知道如何让机器人识别出来自提到的用户(成员:discord.Member)的下一个回复,而不仅仅是我?
在您的检查中,您可以简单地检查消息的作者是否与传递到命令参数中的成员相同:
def check(m):
return m.author == member
我还建议在执行命令的同一频道中添加另一个检查。
这将阻止机器人从提到的用户正在进行的可能不相关的其他对话中获取回复:
def check(m):
return ... and m.channel == ctx.channel
参考文献:
我正在尝试为我最近开始编程的 discord.py 机器人添加 marry 命令。我希望提到的用户能够回复机器人。
截至目前的代码,它只回复我。
@client.command()
async def marry(ctx, member: discord.Member):
await ctx.send(f"{ctx.author.mention} **proposes to** {member.mention} **Do you accept??** "
f"\nRespond with [y(es)/n(o)]")
def check(m):
return m.author == ctx.author
try:
msg = await client.wait_for('message', check=check, timeout=10)
if msg.content.lower() in ['y', 'yes']:
await ctx.send(f"Congratulations! {ctx.author.mention} and {member.mention} are now married to each other!")
elif msg.content.lower() in ['n', 'no']:
await ctx.send(f"Unlucky, maybe another time! {ctx.author.mention}")
else:
await ctx.send("I did not understand that, aborting!")
except asyncio.TimeoutError as e:
print(e)
await ctx.send("Looks like you waited too long.")
有谁知道如何让机器人识别出来自提到的用户(成员:discord.Member)的下一个回复,而不仅仅是我?
在您的检查中,您可以简单地检查消息的作者是否与传递到命令参数中的成员相同:
def check(m):
return m.author == member
我还建议在执行命令的同一频道中添加另一个检查。
这将阻止机器人从提到的用户正在进行的可能不相关的其他对话中获取回复:
def check(m):
return ... and m.channel == ctx.channel
参考文献: