基本上,我的 Discord 机器人中有一个反脏话系统,其中一个列入黑名单的词是“锄头”。但是,如果您说的单词包含脏话,例如“whoever”,则机器人会将其检测为脏话,因为它会在其中找到“hoe”一词。
如何在不从黑名单中删除“锄头”一词的情况下防止这种情况发生?
这是我的代码:
@client.event
async def on_message(message):
if message.author.bot:
return
for badword in file:
if badword in message.content.lower():
await message.delete()
warnMessage = f"Hey {message.author.mention}! Don't say that!"
await message.channel.send(warnMessage, delete_after=5.0)
print(f"{message.author.name} tried saying: {badword}")
channel = client.get_channel(836232733126426666)
embed = discord.Embed(title=f"Someone tried to swear!", colour=0x2D2D2D)
embed.add_field(name="Person who tried to swear:", value=f"{message.author.name}", inline=False)
embed.add_field(name="What they tried to say:", value=f"{badword}", inline=False)
embed.add_field(name="Channel they tried to swear in:", value=f"<#{message.channel.id}>", inline=False)
await channel.send(embed=embed)
return
await client.process_commands(message)
if message.content.startswith('Jason derulo'):
await message.channel.send('Wiggle wiggle wiggle')
if message.content.startswith('fast'):
await message.channel.send('She a runner she a track star')
await client.process_commands(message)
@client.event
async def on_message_edit(before, after):
for badword in file:
if badword in after.content.lower():
await after.delete()
warnMessage = f"Hey {after.author.mention}! Don't say that!\n*You said ||{badword}||*"
await after.channel.send(warnMessage, delete_after=5.0)
print(f"{message.author.name} tried saying: {badword}")
channel = client.get_channel(836232733126426666)
embed = discord.Embed(title=f"Someone tried to swear!", colour=0x2D2D2D)
embed.add_field(name="Person who tried to swear:", value=f"{message.author.name}", inline=False)
embed.add_field(name="What they tried to say:", value=f"{badword}", inline=False)
embed.add_field(name="Channel they tried to swear in:", value=f"<#{message.channel.id}>", inline=False)
return await channel.send(embed=embed)
答案 0 :(得分:1)
如果您不关心人们试图混淆单词以通过您的系统,例如eeHOEee
,您可以使用 .split(' ')
创建内容中所有单词的列表。然后您可以检查列表以查看是否找到了“坏词”。
message_content_list = (message.content.lower()).split(' ')
for badword in file:
if badword in message_content_list:
刚刚意识到,但是由于您要迭代许多不同的单词,因此可以稍微优化一下代码:
message_content_set = set((message.content.lower()).split(' '))
for badword in file:
if badword in message_content_set:
这应该会稍微加快这个过程,因为检查一个项目是否是 in
一个集合比检查一个列表要快。
注意:运行时的差异非常小,通常你不会这样做,除非有大量的“坏词”需要迭代。