【问题标题】:Discord bot sends infinite messages when a user types a specific phrase当用户键入特定短语时,Discord 机器人会发送无限消息
【发布时间】:2020-08-02 23:39:10
【问题描述】:

我创建了一个机器人,当聊天中有人说出特定短语时,它会发送来自 garfielf 视频的引用。例如,如果我说“烤宽面条”,它会回复“* lasaga”。但是,使用一个特定的短语,它会无限发送消息,直到我将其关闭。只有当有人在服务器上说“我讨厌”时,它才会这样做。我尝试创建一个等于 client.get_channel(channel_id) 的返回值的通道变量,并改用 channel.send(),但这有同样的问题。

@bot.event
async def on_message(message):
    await bot.wait_until_ready()
    lasaga = "lasagna"
    garfield = "garfielf"
    where_are_the = "where are the "
    wheres_the = "where\'s the "
    i_hate = "i hate "
    bad_bot = "bad bot"
    text = message.content.strip().lower()
    # sends response if keyword(s) are in message
    if lasaga in text:
        await message.channel.send('*lasaga')
    if garfield in text:
        await message.channel.send(f'I eat, {message.author.name}. It\'s what I do')
    if where_are_the in text or wheres_the in text:
        await message.channel.send('I eat those food')
    if i_hate in text:
        await message.channel.send('I hate alram clocks')
    if bad_bot in text:
        await message.channel.send('sowwy uwu')

我不知道为什么只有一个短语会发生这种情况。

编辑:这是视频:https://youtu.be/OGbhJjXl9Rk

编辑 2:事实证明机器人正在触发自己。我通过将 bot_id 设置为机器人的 id 并将有问题的 if 语句更改为以下方式来修复它:

if i_hate in text and message.author.id != bot_id:
        await message.channel.send('I hate alram clocks')

【问题讨论】:

  • "i hate " 出现在消息中时,您将消息写入也包含"i hate " 的频道。会不会是您的机器人正在响应自己的消息?

标签: python discord bots discord.py


【解决方案1】:

机器人不会忽略它自己的消息,所以如果机器人编写它自己的命令,它就会执行它。这可以通过忽略它自己的消息来解决

async def on_message(self, message):
    if message.author == bot.user:
        return

    # the rest of your code goes here

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 2020-01-22
    • 2018-04-28
    • 2021-08-18
    • 2021-08-19
    • 2021-03-23
    • 2021-01-08
    • 2020-12-30
    • 2018-07-27
    相关资源
    最近更新 更多