如何让我的不和谐机器人删除一个 nsfw 词而不是另一个包含 nsfw 词的 sfw 词? (不和谐.py)

时间:2021-01-31 10:26:54

标签: python discord bots discord.py discord.py-rewrite

我让我的不和谐机器人在遇到 nsfw 单词时删除它们。但是,我遇到了一个问题。 我的清单中有“粪”这个词。但是每当有人说“星期六”时,它也被删除了。

我如何才能让这个词单独被删除,而不是另一个 sfw 词中的一个实例?任何帮助将不胜感激!

这是我的代码:

    def __init__(self, client):
        self.client = client
        self.client.attempts = {}

    @commands.Cog.listener()
    async def on_message(self, msg):
        for word in file:
            if word in msg.content.lower():
                await msg.delete()
                attempts = 0
                try:
                    self.client.attempts[msg.author.id] += 1
                except KeyError:
                    self.client.attempts[msg.author.id] = 1

                #the person gets muted if they say the same nsfw word 3 times in a row.

                if self.client.attempts[msg.author.id] == 3:
                    muted_role = msg.author.guild.get_role(783622936837226529)
                    await msg.author.add_roles(muted_role)
                    embed = discord.Embed(
                        title='',
                        description='',
                        colour=discord.Colour.red()
                    )
                    embed.add_field(name=f"✅ {msg.author.display_name} has been muted.", value='Reason: Toxicity')
                    await msg.channel.send(embed=embed)
                    await asyncio.sleep(1800)
                    await msg.author.remove_roles(muted_role)
                    break

                else:
                    pass

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码。然而,这是一种低效的方法

for word in file:
    if word in msg.content.lower():
        if msg.content.lower() in ["saturday"]: return

你可以使用正则表达式

import re
def string_found(word, content):
    if re.search(r"\b" + re.escape(word) + r"\b", content):
        return True
    return False

for word in file:
    if string_found(word, message.content.lower()) or string_found(string + "s", message.content.lower()):
相关问题