【问题标题】:How to make Discord.py detect uppercase and lowercase word format?如何让 Discord.py 检测大小写单词格式?
【发布时间】:2021-07-22 03:06:52
【问题描述】:

好的,所以我正在处理 Discord.py 中的 say 命令。我希望它检测用户是否在我的banned.txt 列表中使用了坏词,它不会说出来。问题是,如何让它检测大写和小写字母?例如,在我的列表中,它有“nigga”,但用户使用“NiGGa”。如何让 BOT 检测到它并且不会让 bot 这么说? 到目前为止,这是我的代码:

    @commands.command()
    async def say(self, message, *, content):
      fp  = open('banned.txt')
      bad_list = [word.strip() for line in fp.readlines() for word in line.split(',') if word.strip() if word.lower()]
      if any(word in content for word in bad_list):
          await message.reply("Don't you dare!")
      else:
          await message.channel.trigger_typing()
          await message.reply(f"```{content}```")    

我无法在此处上传禁止的.txt,因为它违反了 ToS。

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    检查小写单词。拿起绳子,把它放上去。

    尝试这样做:content=content.lower() 或者只是让 if 语句在检查时使用 content.lower()。它会将 StAckOverFlow 替换为 stackoverflow。

    因此,如果您要禁止使用“Stackoverflow”一词,则不必考虑所有可能的序列,只需考虑小写即可。

    【讨论】:

    • 非常感谢。现在这对我有用,但你能解释一下lower() 函数在这种情况下的作用吗?
    • 它需要一个字符串,使其所有字符都小写。例如,它将“DoGs”变为“dogs”,“catS”将变为“cats”。
    【解决方案2】:

    不要为这些词创建单独的文件,而是在代码中创建一个坏词列表。另一种方法是,在上面的代码中,在第 4 行中,只需添加 .decode('utf-8')。这可能有效。

    【讨论】:

    • 你的意思是这样的? bad_list = [word.strip() for line in fp.readlines() for word in line.split(',') if word.strip() if word.lower().decode('utf-8')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2022-01-07
    • 2011-09-15
    • 1970-01-01
    相关资源
    最近更新 更多