python - on_message 函数重复,Discord.py 重写
问题描述
我正在制作一个机器人,当它检测到您正在使用禁用词时,它会删除您的消息。很简单,但是,当我这样做时。on_message 函数正在重复。我不知道为什么,但我希望你能回答我的问题
@client.event
async def on_message(msg):
contents = msg.content.split(" ")
for word in contents:
if word.lower() in chat_filter: #ChatFilter is a list of words that cannot be used
try:
await msg.delete()
await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
except discord.errors.NotFound:
return
解决方案
您正在遍历消息中的每个单词,并为每个也在chat_filter
. 相反,如果任何单词在禁止列表中,请发送一条消息:
@client.event
async def on_message(msg):
contents = msg.content.split(" ")
if any(word in chat_filter for word in contents):
try:
await msg.delete()
await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
except discord.errors.NotFound:
return
推荐阅读
- docker - 我可以通过 docker-compose.yml 文件将服务部署到 docker swarm,该文件通过其 ID 引用图像吗?
- go - 自定义控制器在缓存同步之前对 addfunc 事件处理程序做出反应?这种行为正确吗?
- java - readInt 和 readLine 在 Java 中制作基本的加法机
- json - 使用 curl 使用 bashscript POST json
- mysql - MySQL如何将两个表(不同的字段)与两个语句组合起来
- c# - 使用单个源数据同时运行多个任务
- mysql - Mysql - 字符串A,B和B,A应该相同
- php - 如何更新 WooCommerce 订单商品数量
- javascript - 列出 Electron 中 ipcRenderer 的所有频道监听器
- python - 只有经过身份验证的用户才能访问媒体文件