当用户键入特定短语时,Discord bot将发送无限消息

2024-04-27 16:55:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个机器人,当聊天中有人说某个特定的短语时,它会从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设置为bot的id并将有问题的if语句更改为:

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

Tags: thetextinsendidmessageifbot
1条回答
网友
1楼 · 发布于 2024-04-27 16:55:21

bot不会忽略自己的消息,因此如果bot编写自己的命令,它将执行它。这可以通过忽略它自己的消息来修复

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

    # the rest of your code goes here

相关问题 更多 >