繁体   English   中英

discord.py wait_for() 机器人响应自身

[英]discord.py wait_for() bot responds to itself

当我在 discord 机器人中使用 wait_for() 时,有时它会立即接收自己的消息作为响应

def check_instant_return(m):
    return m.content

@client.event
async def on_message(message):
  if message.author == client.user:
    return  

  if message.content.startswith('!example'):
    await message.channel.send("This is an example. Say 'y'")

    msg = await client.wait_for('message', check=check_instant_return)

    # the issue is that sometimes msg.content = "This is an example. Say 'y" instead of the real user input

    if msg.content == "y":
      await message.channel.send("Alright")
@client.event
async def on_message(message):
  if message.author == client.user:
    return  

  if message.content.startswith('!example'):
    await message.channel.send("This is an example. Say 'y'")

    msg = await client.wait_for('message', check=lambda mess: mess.author == message.author and mess.channel == message.channel)

    # the issue is that sometimes msg.content = "This is an example. Say 'y" instead of the real user input

    if msg.content == "y":
      await message.channel.send("Alright")

你的支票没用,这样做是为了检查你的邮件的作者。

不知道为什么会发生这种情况,可能是在 discord py 一侧执行命令时出现错误。 我可以建议,作为一个粗略的临时解决方案,您可以在等待消息之前尝试添加 time.sleep。

import time # top of the file

@client.event
async def on_message(message):
  ...
  if message.content.startswith('!example'):
    await message.channel.send("This is an example. Say 'y'")

    time.sleep(0.5)

    msg = await client.wait_for('message', check=check_instant_return)

    ...

告诉机器人不要回应自己(回应除了自己以外的任何人)

使用 wait_for 的检查 function,您可以指定仅在作者与机器人不同时返回( m.guild.me作为用户返回“me”(机器人),因此您不需要引用客户端)

def check_instant_return(m):
    return m.author != m.guild.me and m.content

资料来源: https://discordpy.readthedocs.io/en/latest/api.html?highlight=guild%20me#discord.Guild.me

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM