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



我正在制作一个聊天过滤机器人,但我不想设置任何可能的旁路。如果它有空格/符号,有没有办法查看消息并阻止它?感谢您的帮助。

这应该可以工作。我已经尽可能地接近您的原始代码。

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

最新更新