所以我正在制作一个阻止脏话的机器人,但可以通过使用下划线和其他符号来绕过它。我如何让它阻止它,但通常不删除符号? 这是我的代码:
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
if character not in allowed_characters:
return False
return True
@client.event
async def on_message(message):
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
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 :(得分:0)
如果我的消息是“嘿伙计们@#@@_badWord@@ 怎么了?” 那么,message.content.lower().split(" ")
是 ["hey", "guys", "@#@@_badWord@@", "what's", "up?"]
因此,您可以对正在检查的列表发挥创意。
也许可以
[re.sub(r'\W+', ' ', word) for word in content.message.content.lower().split(" ")]
这将创建一个新列表(使用列表理解),其中每个单词都删除了特殊字符(使用正则表达式替换)
见:How to remove special characters except space from a file in python?
我还建议将其变成一个函数,这样您就不必编写两次代码:
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.")
答案 1 :(得分:0)
您可以尝试使用 re.sub()
模块中的 re
函数从单词中删除所有特殊字符:
clearMessage = re.sub('[^A-Za-z0-9]+', '', message.content))
并删除空格:
clearMessage = clearMessage.replace(" ","")
如果你把它放在你的代码中,它看起来像这样:
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.
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))
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
您不一定需要将消息拆分为所有单词。 if something in something
语句还可以搜索原始字符串