How to make my Python Discord bot I check if the message was sent by the bot itself?

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

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

How can i fix this?

+4
source share
1 answer

The class messagecontains information about the message author, which you can use to determine whether to reply to the message. authoris an Memberobject (or its superclass Userif the channel is private) that has a property idbut also supports direct logical comparisons between users.

For instance:

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

Must function as desired

+3
source

Source: https://habr.com/ru/post/1691115/


All Articles