如何使我的Python Discord机器人检查消息是否由该机器人本身发送?

问题描述:

我正在使用Python(第3.6.1版)编写Discord机器人,该机器人可以检测通道中发送的所有消息并在同一通道中对其进行回复。但是,该漫游器会自行回复消息,从而导致无限循环。

I am writing a Discord bot using Python (v. 3.6.1) which detects all messages sent in a channel and replies to them in that same channel. However, the bot replies to messages by itself, causing an infinite loop.

@ bot.event
异步def on_message(message)
等待bot.send_message(message.channel,message.content)

我该如何解决?

消息 类包含有关消息的 author ,您可以利用它来确定是否响应消息。 author 成员 对象(或其超类 用户 (如果频道是私有的),它具有 id 属性,支持用户之间的直接逻辑比较。

The message class contains information on the message's author, which you can utilize to determine whether or not to respond to the message. author is a Member object (or its superclass User if the channel is private), which has an id property but also supports direct logical comparisons between users.

例如:

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

应根据需要运行