Dave Davidson
Dave Davidson

Reputation: 25

Selecting words from a discord message not working

Pretty new to coding. Want to make a discord bot with python. I have this code. All seems ok but when the keyword is typed the bot just spams the response. What have I done wrong?

https://i.sstatic.net/F5pxA.jpg

@client.event
async def on_message(message):
    channel = message.channel
    if 'donate' in message.content:
        await client.send_message(channel, 'To donate click this link http://www.test.com')

Upvotes: 1

Views: 52

Answers (1)

TrebledJ
TrebledJ

Reputation: 9007

Your matching/selection of words work fine.

You only have a logic error: the bot will also listen for messages sent by itself. Since the message emitted by the bot also contains the word donate (between To and click), your bot will recursively reply to itself. To fix this, add these lines to the start of your on_message function:

if message.author == client.user:
    return

This will filter out messages sent by the bot.

Upvotes: 2

Related Questions