boredscripter
boredscripter

Reputation: 108

discord.py bot is spamming, on the on_message event

There's an issue with discord.py.

@client.event
async def on_message(message):
    if message.author != message.author:
        return
    else:
        if message == "Buck" or "buck":
           await message.channel.send("Yes, he's the Chairman of Dallas!")

It basically repeats to the infinity "yes he's the chairman of dallas" which can get me banned for api abuse and possibly make me fail a commission.

Upvotes: 1

Views: 646

Answers (2)

Shubham
Shubham

Reputation: 3

This should work:

@client.event
  async def on_message(message):
       if message.author == bot.user:
           return 
       if message.content.lower() == "buck":
           await message.channel.send("Yes, he's the Chairman of Dallas!") 

`bot` here refers to your `discord.Bot` instance.

Upvotes: 0

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

An expression like this:

if x == "foo" or "bar" or "baz":

Python is interpreting it like this:

if (x == "foo") or ("bar") or ("baz"):

If the first expression (x == "foo") isn't true, the second ("bar") will be, so this compound conditional always passes.

Try this instead:

if x == "foo" or x == "bar" or x == "baz":

Or, even better:

if x in ("foo", "bar", "baz"):

Also, you're comparing the whole discord.Message instance instead of just the content.

Fixing your code:

if message.content in ("Buck", "buck"):

Upvotes: 5

Related Questions