Discord.py message.content.find多个单词



简单的问题,可能不是那么简单的答案。我能让机器人回答更多的单词吗;嗨"?例如,message.content.find将有多个字来执行事件。

这是代码:

@client.event
async def on_message(message):
if message.content.find("Hi") >= 0:
channel = message.channel
await message.channel.send("Hello!")
def check(m):
return m.content != "x" and m.channel == channel
msg = await client.wait_for("message", check=check)
await message.channel.send("How are you?".format(msg))

您也可以使用语法来检查另一个中是否有特定的字符串

代替使用:

if message.content.find("Hi There") >= 0:

您可以使用:

if "Hi There" in message.content:

让你惊讶的是,答案其实很简单。

if "hi" in message.content:
# your code goes here

现在,我们遇到了一个问题。如果用户说";嗨,我是komko;它将触发事件,但是";嗨,我是komko;不会。为什么?好吧,我们正在检查";嗨"在message.content中;嗨"或";hI";,等等。

我的方法是始终使用lower()消息.content转换为小写

现在,它看起来是:

# we can also make a string declared before, like:
stringValue = "HI"
if stringValue.lower() in message.content.lower():
# your code goes here

这也可以替换为:

if "Hi".lower() in message.content.lower():
# your code goes here

最新更新