【问题标题】:discord.py: Trying to wait for author response and answer itdiscord.py:试图等待作者回复并回答
【发布时间】:2021-02-26 06:15:33
【问题描述】:

我目前正在尝试使用 discord.py 制作一个机器人,但我有一个小问题:我不知道如何等待作者的回复以及机器人在其后发布另一条消息。 这是我目前的代码:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):

    if message.author == client.user:
        return

    if message.content.startswith('.pdfcheckup'):
        await message.channel.send("Quelle matière scolaire cherchez-vous ?\nListe des matières disponibles:\n**```\n1- Histoire\n2- Géographie\n3- EMC\n4- Français\n6- Mathématiques\n7- Physique-Chimie\n8- Enseignement Scientifique\n9- NSI\n10- Anglais\n11- Espagnol\n12- Allemand\n```**")

client.run('[my token]')

所以,总结一下我的机器人需要做什么,作者写“.pdfcheckup”,机器人用长消息响应,机器人等待作者说出 1 到 12 之间的数字,具体取决于数字作者选择了,它以特殊的信息回答。但是我很笨。

谢谢。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你可以使用Client.wait_for:

    def author_check(author):
        return lambda message: message.author == author
    
    # later in your code
    await message.channel.send("long message")
    msg = await client.wait_for("message", check=author_check(message.author), timeout=30.0)
    print(msg.content) # will be whatever the original author sent
    

    timeout 参数只是等待事件发生的最长时间(以秒为单位)。

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2020-06-27
      • 2021-10-20
      • 1970-01-01
      • 2019-07-10
      相关资源
      最近更新 更多