Skip to content
Advertisement

How do I listen for new chat messages in text channels using a discord bot in python?

I’ve designed a system to loop through all the channels and check to see if the last few messages are the same as they were at the previous check, but that’s slow and unnecessary. If there’s any way to do it through a function, or a simpler, faster way, let me know. Thanks!

Advertisement

Answer

You can use the event on_message, which is called every time someone writes anything in a channel your bot can read:

@bot.event #could be client.event for you
async def on_message(message):
    #do stuff here you want to do every time a new message is sent 

References:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement