首页 > 解决方案 > 多场景用户输入 Discord.py

问题描述

根据给定的提示(消息 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')

标签: pythonpython-3.xasync-awaitdiscorddiscord.py

解决方案


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

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

您可以在检查功能中使用相同的原理

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

推荐阅读