函数可在discordbot中重复特定消息



所以,我正在为我的机器人程序编写一些新的东西,这时有人说字母"h〃;机器人会在聊天中重复相同的字母。然而,机器人似乎做到了,但说了5次信息,而且从未停止过一遍又一遍地说。

不仅如此,当有人说任何话时,机器人会一遍又一遍地说h 5次。

这是代码:

@client.event
async def on_message(message):
if message.content.find("h") != 1:
await message.channel.send("h")

on_message将对包括机器人程序本身在内的每条消息作出反应。忽略bot消息的最佳方法是这样的。

@client.event
async def on_message(message):
if message.author == bot.user:  # skip bot messages
return
# code here
if message.content.find("h") != 1:
for _ in range(5):
await message.channel.send("h")

这是因为您正在从1开始索引消息内容,而您应该从0:开始

if message.content.find("h") != 0:

顺便说一句,如果目的是忽略机器人程序消息,你可以明确地这样做:

if message.author != client.user:
await message.channel.send("h")

最新更新