Cliv M
Cliv M

Reputation: 3

Discord.py message spamming

my message is spamming, I only want to send it 1 time. how to fix?

@client.event async def on_message(message): if 'happy birthday' in message.content.lower(): await message.channel.send('Happy Birthday! ๐ŸŽˆ๐ŸŽ‰')

Upvotes: 0

Views: 347

Answers (2)

Ethan Armstrong
Ethan Armstrong

Reputation: 310

You are creating an infinite loop, when the bot sends โ€Happy Birthday!โ€ โ€happy birthdayโ€ is in the lowercase version of the message. So the bot reads happy birthday and sends a message, Happy Birthday!. Which the bot detects as a new message, the new message (when converted to lowercase) contains happy birthday so the bot sends a message.

There are two ways to fix this, you can change the content of the message to not contain happy birthday

Or you could check if the message is sent from anyone but the bot using message.author message docs page

Upvotes: 0

Nurqm
Nurqm

Reputation: 4743

Because it's an on_message event and it detects also the bot's messages and bot is sending a message including happy birthday.

If you don't want it spamming, you can check if author is a bot account with message.author.bot.

@client.event
async def on_message(message):
    if message.author.bot:
        return
    if 'happy birthday' in message.content.lower():
        await message.channel.send('Happy Birthday! ๐ŸŽˆ๐ŸŽ‰')

Upvotes: 1

Related Questions