Is there a way to make a chat filter bot detect capital letters through code? (discord.py)

Viewed 363

I'm making a chat filter bot, but I don't want to have to put in EVERY possible bypass. Is there a way for it to look at messages and block it if it has spaces/symbols? Any help is appreciated.

1 Answers

This should work. I've tried to stay as close to your original code as possible.

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')

Related