如何在discord.py上防止服务器上的脏话



我正在尝试阻止包含脏话的消息。顺便说一句,文本文件中的脏话。所以,我被困住了,因为每当我写脏话时,bot都不回应我的信息。这是我的代码:


@client.event
async def on_message(message):   
global swearword_count
global badwords

if message.author == client.user:  
await client.process_commands(message)
else:
msg = message.content
for x in msg:
try:
if x in badwords:
if exception_counter[str(message.author.id)] == 0:
await message.channel.send("Please do not use this word ever")
swearword_count[str(message.author.id)] += 1
if swearword_count[str(message.author.id)] > 5 and exception_counter == 1:
await message.author.send("You've been banned due to bad words that you used.")
elif swearword_count[str(message.author.id)] > 5 and exception_counter == 0:
await message.author.send("You've been banned due to bad words that you used.")
await message.channel.send("Don't use this word")

else:
pass
except KeyError:
exception_handling[str(message.author.id)] = 1
swearword_count[str(message.author.id)] = 0
await message.author.send("If you use this word,you will get banned.")
continue
await client.process_commands(message)
@client.event
async def on_member_join(member): 
channel = discord.utils.get(member.guild.text_channels, name="welcome")
await channel.send(f"{member.name},welcome :)")
global swearword_count
swearword_count[str(member.id)] = 0
exception_handling[str(member.id)] = 0
dm_channell = await member.create_dm()
await dm_channell.send(f"{member},welcome mate :)")

我使用字典来存储用户的数据。那么我该如何解决这个问题呢?如果你有什么建议,我很乐意听听

PS:我的命令工作正常。

这是因为for x in msg:将您的消息分成单个字母。例如,你必须用空格来分割它:for x in msg.split(" "):,但是如果有人写了&;badword&;没有空格(例如">badwordIlikeapples),这将无法工作。所以试试这个:

for x in badwords:
try:
if x in msg:
...

这将捕获所有的"坏词",但如果你写了一个包含脏话的单词,这也会被捕获。

最新更新