【问题标题】:How can make discord.py bot to determine keyword in sentence?如何使 discord.py bot 确定句子中的关键字?
【发布时间】:2021-07-30 09:00:06
【问题描述】:

我正在制作一个 discord.py 机器人,我希望它在捕获句子或段落中的关键字时发送消息。 我知道如何制作关键字命令,但我不知道如何制作来确定单词中的关键字,有人可以帮我解决这个问题吗? 这是我的命令:

@bot.event
async def on_message(msg):
    if msg.content == 'Good morning' and msg.author != bot.user:
        await msg.channel.send('Good morning boys:)')

【问题讨论】:

  • 你能澄清一下这个问题吗?你的意思是一条消息可以包含一个句子,比如说“你好,大家早上好”,并且你希望能够检测到一个子字符串,比如说“早上好”?您对检测或实际发送消息有疑问吗?
  • 不,我希望该命令可以检测消息中的一个单词,例如它可以在“大家好,早上好”中找到关键字“早安”,然后发送消息给回复发件人。跨度>

标签: python


【解决方案1】:

如果我从您的 cmets 中理解正确,这就是问题:

您有一个 Discord Bot,并且您想检测消息中何时包含某些单词,例如说“Hello World”。 如果消息中包含“Hello World”,则机器人会发送回复。

这是一个小的概念验证:

@bot.event 
async def on_message(msg):
  # For more details on how to detect when a string contains a substring in Python, 
  # refer to this question: https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method
  if "Hello World" in msg.content and msg.author != bot.user: 
    await msg.channel.send('Good morning boys:)')

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2022-11-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多