TheBigBro122
TheBigBro122

Reputation: 99

on_message function repeating, Discord.py rewrite

I'm making a bot that, when it detects you are using a banned word, it will delete your message. Simple enough but, when I do that. The on_message function is repeating itself. I don't know why but I hope you can answer my question

@client.event
async def on_message(msg):
    contents = msg.content.split(" ")
    for word in contents:
        if word.lower() in chat_filter: #ChatFilter is a list of words that cannot be used
            try:
                await msg.delete()
                await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
            except discord.errors.NotFound:
                return

Upvotes: 1

Views: 676

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60944

You're looping over each word of the message and sending a response for each of those words that are also in chat_filter. Instead, send a single message if any of the words are in the banned list:

@client.event
async def on_message(msg):
    contents = msg.content.split(" ")
    if any(word in chat_filter for word in contents):
        try:
            await msg.delete()
            await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
        except discord.errors.NotFound:
            return

Upvotes: 1

Related Questions