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

聊天违规词删除脚本触发事件但功能未执行,如何修复?

修复Discord违规词过滤脚本

原脚本的核心问题是for循环内的else分支错误使用了return语句:只要第一个违规词不匹配,就直接退出整个函数,既不会检查后续的违规词,也不会执行await bot.process_commands(message)。

修复后的代码

words_to_filter = ["badword1", "badword2", "badword3"]

@bot.event
async def on_message(message):
    print(f"on_message event triggered")  # 事件触发日志
    if message.author == bot.user:
        return

    # 遍历所有违规词
    for word in words_to_filter:
        if word in message.content:
            print(f"Bad word found: {word}") # 检测到违规词日志
            await message.delete() # 删除违规消息
            await message.channel.send(f"{message.author.mention}, դուք օգտագործել եք արգելված բառ ձեր հաղրոդագրությունում") # 发送提示消息
            return  # 处理完违规词后退出函数

    # 所有词都检查完,未发现违规,处理命令
    print("No bad words found") # 调试日志
    await bot.process_commands(message)

关键修改点

  • 删除了原else分支中的return语句,确保循环能遍历完所有违规词
  • 将调试用的print('Falseee')替换为循环结束后的统一提示(可根据需求调整)
  • 确保只有在遍历完所有违规词且未发现违规时,才执行命令处理逻辑

终端输出示例:
terminal output

内容的提问来源于stack exchange,提问作者Hayk Kostanyan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 19:02:41