如何阻止 discord 机器人响应自身 - Return if 语句不起作用
How to stop a discord bot from responding to itself - Return if statement does not work
我是 Python 的新手,我正在使用 discord.py 重写 python 3.7 编写一个 discord 机器人。这就是问题所在:我的机器人不断响应自己,生成无限的重复消息流。我已经搜索了我的问题的解决方案,我遇到的问题似乎对其他人都有效,但对我却不是。这是我的代码:
@client.event
async def on_message(message):
await message.channel.send("hi")
if message.author == client.user:
return
await client.process_commands(message)
我试过更换
if message.author == client.user:
return
和
if message.author.bot == True:
return
因为它似乎是替代解决方案。但是,其中 none 有效。我不知道该怎么做。
问题已解决。对于那些想知道的人:我只需要移动
if message.author == client.user:
return
到函数的顶部。我的最终代码是这样的:
@client.event
async def on_message(message):
if message.author == client.user:
return
await message.channel.send("hi")
await client.process_commands(message)
我是 Python 的新手,我正在使用 discord.py 重写 python 3.7 编写一个 discord 机器人。这就是问题所在:我的机器人不断响应自己,生成无限的重复消息流。我已经搜索了我的问题的解决方案,我遇到的问题似乎对其他人都有效,但对我却不是。这是我的代码:
@client.event
async def on_message(message):
await message.channel.send("hi")
if message.author == client.user:
return
await client.process_commands(message)
我试过更换
if message.author == client.user:
return
和
if message.author.bot == True:
return
因为它似乎是替代解决方案。但是,其中 none 有效。我不知道该怎么做。
问题已解决。对于那些想知道的人:我只需要移动
if message.author == client.user:
return
到函数的顶部。我的最终代码是这样的:
@client.event
async def on_message(message):
if message.author == client.user:
return
await message.channel.send("hi")
await client.process_commands(message)