User123355
User123355

Reputation: 21

discord.py banned word automod

I recently made an automod feature which deletes any message that has bad words. I want to make it so that if you reach a certain amount of attempts, it mutes you, I tried something like this:

Here is my code:

filtered_words = ["bad", "words", "here"]

@client.event
async def on_message(message):
    for word in filtered_words:
        if word in message.content.lower()
            await message.delete()
            embed = discord.Embed(color=0xff0000)
            embed.set_author(name="Banned Word")
            embed.add_field(name="Username:", value=f"{message.author.mention}", inline=False)
            embed.add_field(name="Banned Word:", value=f"{word}", inline=False)
            embed.add_field(name="Message:", value=f"{message.content}", inline=False)
            embed.set_footer(text=f'User ID: {message.author.id}')
            embed.timestamp = datetime.datetime.utcnow()
            channel = client.get_channel(CHANNEL_ID_HERE)
            await channel.send(embed=embed)
            counter = 0
            counter+=1
            if counter < 3:
             role = discord.utils.get(message.guild.roles, name="Muted")
             await message.channel.send('User was muted')
             await message.author.add_roles(role)

The line if counter < 3: causes me an issue, when I use >, it doesn't mute the user, but if I use <, it mutes the user straight away. Any idea why?

Upvotes: 0

Views: 677

Answers (1)

Toni Sredanović
Toni Sredanović

Reputation: 2412

Try separating the counter from your function. Also, counter should be per user. Something like this:

filtered_words = ["bad", "words", "here"]
filtered_words_counter = {}

@client.event
async def on_message(message):
    for word in filtered_words:
        if word in message.content.lower()
            await message.delete()
            embed = discord.Embed(color=0xff0000)
            embed.set_author(name="Banned Word")
            embed.add_field(name="Username:", value=f"{message.author.mention}", inline=False)
            embed.add_field(name="Banned Word:", value=f"{word}", inline=False)
            embed.add_field(name="Message:", value=f"{message.content}", inline=False)
            embed.set_footer(text=f'User ID: {message.author.id}')
            embed.timestamp = datetime.datetime.utcnow()
            channel = client.get_channel(CHANNEL_ID_HERE)
            await channel.send(embed=embed)

            if not filtered_words_counter.get(message.author.id):
                filtered_words_counter[message.author.id] = 1
            else:
                filtered_words_counter[message.author.id] += 1

            if filtered_words_counter[message.author.id] >= 3:
             role = discord.utils.get(message.guild.roles, name="Muted")
             await message.channel.send('User was muted')
             await message.author.add_roles(role)
             filtered_words_counter[message.author.id] = 0

This solution will also reset the users counter once he gets muted and you must mind that this counter state isnt persistent (counter will be reset after bot restart).

Upvotes: 1

Related Questions