'TypeError: argument of type 'Message' is not iterable. Discord Python Bot

I get this error everytime a message is sent ive tried looking for solutions but havent found any here is the traceback:

Traceback (most recent call last):
  File "C:\Users\super\AppData\Roaming\Python\Python310\site-packages\discord\client.py", line 382, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\super\Documents\Discord Bots\First Bot\bot.py", line 27, in on_message
    if bad_word in message:
TypeError: argument of type 'Message' is not iterable

Here is the code that isnt working:

#Events
@bot.event
async def on_message(message):
    for bad_word in bad_words:
        if bad_word in message:
            await message.send(message.channel, f"{message.author.mention} your message has been censored.")
            await message.delete(message)

I do have a list name "bad_words" and there is 1 word in it.



Solution 1:[1]

For the Discord module, to get the content of a message, you need to use message.content.

Example code:

#Events
@bot.event
async def on_message(message):
    contents = message.content
    for bad_word in bad_words:
        if bad_word in contents:
            await message.send(message.channel, f"{message.author.mention} your message has been censored.")
            await message.delete(message)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alan Shiah