为什么我的聊天过滤器会阻止 wod 列表顶部的单词? (不和谐.py)

时间:2021-02-23 17:52:12

标签: python discord.py

我的聊天过滤器只过滤列表顶部的词,我不知道为什么。谁能解决这个问题?

with open("badwords.txt") as file:
    bad_words = file.read().splitlines()

@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=f":x:   Please don't say that here, {message.author.mention}.")
            t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
            await message.channel.send(embed=t, delete_after=5)
            await message.delete()
        else:
            await client.process_commands(message)
            return

1 个答案:

答案 0 :(得分:2)

当第一个单词不在带有 return 关键字的列表中时,您将退出,只需将其删除,一切都会正常进行。 process_commands 也应该在 else 语句之外

@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=f":x:   Please don't say that here, {message.author.mention}.")
            t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
            await message.channel.send(embed=t, delete_after=5)
            await message.delete()

    await client.process_commands(message)

如果您想要更短的方法,可以使用 any 函数

@client.event
async def on_message(message):    
    if any(bad_word in message.content.lower() for bad_word in bad_words):
        t = discord.Embed(color=0x039e00, title="Message Removed", description=f":x:   Please don't say that here, {message.author.mention}.")
        t.set_footer(text="DM TheSuperRobert2498#2498 for bot suggestions.")
        await message.channel.send(embed=t, delete_after=5)
        await message.delete()

    await client.process_commands(message)