You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py不良词检测功能异常 仅触发首个条件忽略后续判断求助

Python Discord机器人不良词过滤逻辑问题修复

错误根因

你写的关键词判断语句存在逻辑错误:
if "hack" or "crack" in message.content.lower()
在Python中该语句会被解析为 if ("hack") or ("crack" in message.content.lower()),而非你预期的判断两个词是否存在。其中非空字符串"hack"的布尔值固定为True,所以只要消息命中words2列表里的内容,整个判断就会直接通过,完全忽略crack的校验。

修复方案

写法1:拆分完整判断

把错误的判断行替换为:

if "hack" in message.content.lower() or "crack" in message.content.lower():

写法2:统一用any语法(更易维护)

和你前面的列表匹配逻辑保持一致,后续新增关键词直接往列表里加即可:

if any(keyword in message.content.lower() for keyword in ["hack", "crack"]):

优化后完整代码

你也可以把两层嵌套的if合并为一个逻辑判断,结构更清晰:

@bot.event
async def on_message(message):
  ###BAD WORDS CHECK
  if message.author == bot.user:
    pass
  else:
        # 同时满足两个条件才触发拦截
        if any(x in message.content.lower() for x in words2) and any(key in message.content.lower() for key in ["hack", "crack"]):
            await message.reply("I think you may be asking for illegal services or illegal advice. Please refer to rule #5 in the welcome channel!")
            await message.channel.send("If you think this was a mistake please reply with $report!")
            global CAN_REPORT
            CAN_REPORT = "yes"
            return
  await bot.process_commands(message)

内容的提问来源于stack exchange,提问作者Important Ad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:57:03