【发布时间】: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