如何使我的Python Discord bot检查机器人本身是否发送了一条消息?

问题描述 投票:6回答:1

我正在使用Python(v.3.6.1)编写Discord bot,它检测通道中发送的所有消息并在同一通道中回复它们。但是,机器人自己回复消息,导致无限循环。

@bot.event async def on_message(message) await bot.send_message(message.channel, message.content)

我该如何解决这个问题?

python python-3.x discord.py
1个回答
6
投票

message类包含有关消息author的信息,您可以使用它来确定是否响应消息。 author是一个Member对象(或者如果通道是私有的,它的超类User),它具有id属性,但也支持用户之间的直接逻辑比较。

例如:

@bot.event
async def on_message(message):
    if message.author != bot.user:
        await bot.send_message(message.channel, message.content)

应该按照要求运作

© www.soinside.com 2019 - 2024. All rights reserved.