python - 如何从列表中检测句子中的单词?
问题描述
所以我正在创建一个不和谐的机器人,我希望它在有人发送消息时删除消息,并且它包含一个特定的词,在这种情况下是一个坏词。
f = open("filter_words.txt", 'r')
list = f.read()
f.close()
msg = message.content
msg = msg.lower()
if msg in list:
await message.delete()
await message.channel.send(f"{message.author.mention}'s message was deleted")
filter_words.txt
包含一个坏词列表{'word', 'another'}
。此处代码仅在有人仅键入单词时才删除消息。如果单词在句子中的任何位置,我希望它删除消息。我希望我正确地解释了这一点。
解决方案
def check_bad_word(msg, list):
''' Detect bad word.'''
# Loop over all bad word
for word in list:
# bad word found ('in' can make wrong detection)
if word in msg:
return True
return False
if check_bad_word(msg, list)
await message.delete()
await message.channel.send(f"{message.author.mention}'s message was deleted")
推荐阅读
- go - 无法在 Go 应用程序的另一个包中使用函数
- firefox - Mozilla firefox 忽略缓存控制标头
- node.js - 如何处理 Selenium Webdriver Node Js 中的错误。NoSuchElementError
- sql - SQL Server:基于父子关系拆分列
- python - 有没有可以去除异常值的功能?
- c# - 防止用户拥有多个并发会话
- azure - 从 ARM 模板部署逻辑应用自定义连接器时设置身份验证
- java - Java FX 中的抗锯齿管理
- glsl - 在 KDevelop 中关闭 GLSL 的语义分析
- c# - 在.Net Windows Forms中循环时不阻塞UI