如何让我的聊天过滤器机器人检测脏话中的空格,但不阻止包含坏词的单词,例如“class”?

时间:2021-01-17 22:47:15

标签: python discord discord.py

我正在制作一个聊天过滤机器人来阻止 Discord 服务器中的脏话,而且效果很好,但人们可以通过在其中放置空格来轻松绕过它。我终于解决了这个问题,但它阻止了包含坏词的单词,例如“class”。有谁知道如何解决这个问题?

这是我的代码:

import random
import string
import re
from discord.ext import commands

client = commands.Bot(command_prefix = 'as-')

with open("badwords2.txt") as file:
    bad_words = file.read().splitlines()

def check_if_allowed(text):

    allowed_characters = string.ascii_uppercase + string.ascii_lowercase #creates a string containing all upper- and lower- case letters.

    allowed_characters += "1234567890" # adds digits to the string

    if character not in allowed_characters: #checks if the character is in the set of allowed characters, and returns false if not
        return False

    return True #returns true if every character in the input is allowed
    
@client.event
async def on_message(message):    
    clearMessage = re.sub('[^A-Za-z0-9]+', '', message.content).lower()
    clearMessage = clearMessage.replace(" ","")

    for bad_word in bad_words:
        if bad_word in clearMessage:
            t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:   Please do not swear. Swearing can result in a mute or ban.")
            t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
            await message.channel.send(embed=t)
            await message.delete()
            return

    if not check_if_allowed(message.content):
        t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:  Please do not swear. Swearing can result in a mute or ban.")
        t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
        await message.channel.send(embed=t)
        await message.delete()
        return


client.run('TOKEN')

感谢所有帮助,并提前致谢!

0 个答案:

没有答案