Phishy1
Phishy1

Reputation: 46

Variables are not being redefined in Python (discord.py)

I have been making a Discord bot for one of my servers, and one of it's functions is to prevent curse words. I want it to toggle the censor mode when you type in a command. However, instead of switching the variable to 1 and back again, it just stays on 0, even though I get the "Bad words will now not work" output.

@client.event
async def on_message(message):
    swareCensor: int = 0
    if message.content == 'f! censor':
        if swareCensor == 0:
            await message.channel.send('Bad words will now not work')
            swareCensor += 1
        else:
            await message.channel.send('The bad word filter is now off')
            swareCensor *= 0

    if swareCensor == 1:
        if 'fuck' in message.content:
            await message.delete()
            await message.channel.send('Sware has been detected')

It wouldn't be ideal for swears to be censored all the time, especially if I want it on multiple servers

Upvotes: 0

Views: 88

Answers (1)

Magnus Enebakk
Magnus Enebakk

Reputation: 161

I would first of all strongly recommend using the actual command method instead of saying if message == "!command" in an onMessage event.

swearFilter = False

@client.command()
async def sensor(ctx):
    swearFilter = not swearFilter
    await ctx.send(f"Swear filter is now set to {swearFilter}")

@client.event()
async def on_message(message):
    if swearFilter:
        if "fuck" in message.content:
            await message.delete()
            await message.channel.send("Swear detected!")

Upvotes: 1

Related Questions