Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - In discord.py, How do I make a profanity filter

This is what I have at the moment and when I test it by typing a bad word in the list nothing happens. Why is this and what can code I use to fix it?

infractions = {}
limit = 5
blacklist = ['bad words go here']
@bot.event
async def on_message(message):
    content = message.content.lower()
    if any(word in content for word in blacklist):
        id = message.author.id
        infractions[id] = infractions.get(id, 0) + 1
        if infractions[id] >= limit:
            await bot.kick(message.author)
        else:
            warning = f"{message.author.mention} this is your {infractions[id]} warning"
            await bot.send_message(message.channel, warning)
    await bot.process_commands(message)
question from:https://stackoverflow.com/questions/66047514/in-discord-py-how-do-i-make-a-profanity-filter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

your problem is that you are using an old version/tutorial of discord.py. Things like bot.send_message are out of date and things like bot.kick don't even exist. The ideal code would be?:

infractions = {}
limit = 5
blacklist = ['old', 'version', 'of', 'discord', '.', 'py']

@bot.event
async def on_message(message):
    content = message.content.replace(' ', '')

    if any(word in content.lower() for word in blacklist):
        id = message.author.id
        infractions[id] = infractions.get(id, 0) + 1

        if infractions[id] >= limit:
            await message.author.kick()
        else:
            warning = f"{message.author.mention} this is your {infractions[id]} warning"
            await message.channel.send(warning)
    await bot.process_commands(message)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...