如何在 discord.py 命令中允许多个可能的响应? [英] How do I allow for multiple possible responses in a discord.py command?

查看:15
本文介绍了如何在 discord.py 命令中允许多个可能的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 Discord 机器人,同时对 discord.py(实际上是 Python 3)相对较新.我想添加命令greet",它会提示用户对它说hello".但是,当我希望它同时响应hello"和Hello"时,它只会响应hello".

I'm attempting to set up a Discord bot while being relatively new to discord.py (and in reality, Python 3). I want to add the command "greet", which will prompt the user to say "hello" to it. It only responds to "hello", however, when I want it to respond to both "hello" and "Hello".

我唯一能想到的解决方法是将它们放在 or 语句中,理论上这应该让 Python 3 和机器人在两个响应之间进行选择(如下所示).

The only things I could think about fixing it was placing them in an or statement, which in theory should've made Python 3 and the bot choose between the two responses (as shown below).

@client.event
async def on_message(message):
    if message.content.startswith('~greet'):
        await client.send_message(message.channel, 'Say hello')
        msg = await client.wait_for_message(author=message.author, content=('hello' or 'Hello'))
        await client.send_message(message.channel, 'Hello.')

我的原始代码很简单,只允许 hello 的一个响应.

The original code I had for it was simple and only allowed for the one response of hello.

@client.event
async def on_message(message):
    if message.content.startswith('~greet'):
        await client.send_message(message.channel, 'Say hello')
        msg = await client.wait_for_message(author=message.author, content=('hello' or 'Hello'))
        await client.send_message(message.channel, 'Hello.')

由于某种原因,我无法理解,它仍然无法识别 'Hello',并且只允许我说 'hello' 作为响应.

For some reason I can't wrap my head around, it's still not recognizing 'Hello', and only ever allowing me to say 'hello' in response.

推荐答案

or 的行为与您想象的不同.'hello' 或 'Hello' 在传递给 wait_for_message 之前进行评估,并且等于 'hello'.

or doesn't behave how you think it does. 'hello' or 'Hello' is evaluated before being passed to wait_for_message, and is equal to 'hello'.

相反,您可以向 wait_for_message 提供一个 check 函数:

Instead, you can provide a check function to wait_for_message:

def is_hello(message):
    return message.content.lower() == 'hello'

msg = await client.wait_for_message(author=message.author, check=is_hello)

这篇关于如何在 discord.py 命令中允许多个可能的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆