【问题标题】:Multiple Scenario User Input Discord.py多场景用户输入 Discord.py
【发布时间】:2021-05-14 01:01:31
【问题描述】:

根据给定的提示(消息 1),用户应该能够向左、向右或保持不动做出响应,并且不和谐机器人将提供自定义消息(消息 2-4 之一)。我试图创建多个检查来检测不同的用户响应,但无济于事。我在网上找了其他解决方案,其中一些提到了使用asyncio,但我对它不是太熟悉,即使使用它也无法解决问题。任何帮助表示赞赏,我对python相当陌生。谢谢。

代码如下:

if message.content.startswith('gametest'):
        channel = message.channel
        await channel.send('Would you like to play?')

        def check(m):
            return m.content == 'yes' and m.channel == channel

        msg = await client.wait_for('message', check=check)
        await channel.send('Message1')

        def check2(m):
            return m.content == 'left' and m.channel == channel
        def check3(m):
            return m.content == 'right' and m.channel == channel
        def check4(m):
            return m.content == 'stay still' and m.channel == channel

        msg2 = await client.wait_for('message', timeout=30.0, check=check2)
        await channel.send('Message2')

        msg3 = await client.wait_for('message', timeout=30.0, check=check3)
        await channel.send('Message3')

        msg4 = await client.wait_for('message', timeout=30.0, check=check4)
        await channel.send('Message4')

【问题讨论】:

    标签: python python-3.x async-await discord discord.py


    【解决方案1】:

    可以比较消息内容是否在列表中

    >>> content = 'right'
    >>> valid_responses = ['right', 'left', 'stay still']
    >>> content in valid_responses
    True
    

    你可以在check函数中使用同样的原理

    def check(m):
        return m.content in ['right', 'left', 'stay still'] and m.channel == channel
    

    如果您希望它不区分大小写,可以使用str.lower() 方法

    def check(m):
        return m.content.lower() in ['right', 'left', 'stay still'] and m.channel == channel
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多