【问题标题】:Discord.py (Not the rewrite). How to accept multiple inputs for on_messageDiscord.py(不是重写)。如何接受 on_message 的多个输入
【发布时间】:2020-11-06 23:54:36
【问题描述】:

我正在为我的 discord 机器人使用 Discord.py 库(不是重写)。我有一个命令可以在用户发送消息时纠正用户语法。

如:

elif message.content.find("your"):
    await message.channel.send("*you're")

此方法有效,因为即使它在句子中也可以检测到单词。例如,如果用户说“你很酷”,机器人会拿起它并发送“你是”。我希望它能够检测多个输入,因为我不想要一个 300 行长的 if-elif-else 阶梯。

如:

elif message.content.find("your", "ur", "yor"):
    await message.channel.send("*you're")

错误:

TypeError: slice indices must be integers or None or have an __index__ method

所以我想知道是否有办法做到这一点,因为我从文档中看到的不多。

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    您可以创建一个类似['ur', 'your', 'yor'] 的列表,然后您可以遍历此列表并检查其中一个是否在message.content 中。所以你可以简单地使用这个:

    [await message.channel.send("*you're") for m in ['ur', 'your', 'yor'] if m in message.content]
    

    或者你可以使用

    word_list = ['ur', 'your', 'yor']
    for m in word_list:
        if m in message.content:
            await message.channel.send("*you're")
            return
    

    【讨论】:

    • 我将把它放在我的代码中的什么位置?我有 def on_message: 然后是一长串 if-elif。会是:“word_list 中的 elif message.content:”吗?或者它会是一个client.command? @Nurqm
    • 您应该将它添加到on_message 事件中,只需将其添加到那里。
    • 由于某种原因,当我在 discord 中输入“your”时,它会以“you're”响应 2 次。它不会对“yor”和“ur”执行此操作。证据:media.discordapp.net/attachments/769198899680247828/…(我没有其他响应“你的”的命令)
    猜你喜欢
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2020-10-14
    • 1970-01-01
    • 2021-11-24
    • 2016-01-01
    • 2021-01-30
    相关资源
    最近更新 更多