pythondiscordclientdiscord.py

python - discord.py sends to many messages


I am building a discord bot and I set it to respond to messages that include '_hello', but as a response it sends from 1 to 3 messages which are always the same.

@client.event
async def on_message(message):
    if '_hello' in message.content.lower():
        print('message received')
        words = message.content.split(' ')
        if len(words) < 3:
            await message.channel.send('incorect syntax')
        else:
            name = words[1]
            userMessage = ""
            for word in words[2:]:
                userMessage = userMessage + word + ' '

            await message.channel.send('hello: ' + name + "\nwith message: " + userMessage)


Solution

  • The cause to this issue can be due to accidently running the bot multiple times. This is a glitch that can occur on some IDE's like PyCharm if you close the application while your bot is running. This happens to me sometimes and the only fix I really found that works is changing your bot's token in the Discord Developer Portal and putting the new token in your run line. All though this is annoying this is due to the bot being run multiple times, and if you close the application while it happened, you can not end the code after you closed the application.