有没有办法让聊天过滤器机器人通过代码检测大写字母? (不和谐.py)

时间:2021-01-11 00:42:37

标签: discord discord.py

我正在制作一个聊天过滤器机器人,但我不想设置所有可能的绕过方式。如果它有空格/符号,有没有办法查看消息并阻止它?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

这应该有效。我已尽量与您的原始代码保持一致。

import discord
import random
import string
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

    for character in text: #iterates through every character in the input
        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):
    if not check_if_allowed(message.content):
        t = discord.Embed(color=0x039e00, title="Message Removed", description=":x:  Please only use alpha-numeric characters and spaces.")
        t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
        await message.channel.send(embed=t)
        await message.delete()
        return
    
    for bad_word in bad_words:
        if bad_word in message.content.lower().split(" "):
            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')