python - 如何禁止某些单词然后删除消息
问题描述
我的目标是检查用户在不和谐频道中输入的内容是否包含我在列表中的某个单词或短语,如下所示:
illegal_words = ["apple", "pear", "banana"]
预期的结果是当我输入的内容包含以下一个或多个单词时,它会看到该单词,然后删除,然后发送一条消息,指出您不允许键入该特定消息。
这是我当前的代码,包括列表
client = commands.Bot(command_prefix="..")
illegal_words = ["apple", "pear", "banana"]
@client.event
async def on_message(ctx):
[word in message.content for word in illegal_words]
any([word in message.content for word in illegal_words])
await ctx.channel.purge(limit=1)
await ctx.send("""That Word Is Not Allowed To Be Used!
Continued Use Of Mentioned Word Would Lead To Punishment!""")
解决方案
@client.event
async def on_message(message):
if any(word in message.content for word in illegal_words):
await message.delete()
await message.channel.send("""That Word Is Not Allowed To Be Used! Continued Use Of Mentioned Word Would Lead To Punishment!""")
else:
await client.process_commands(message)
on_message
接收一个Message
对象,而不是一个Context
. 我们可以直接删除消息,并且应该通过向它所在的频道发送消息来响应它。
推荐阅读
- javascript - 查找外部站点的隐形 Recaptcha 回调函数
- node.js - 如何在 node.js 中更改 jsonwebtoken 的标头
- javascript - 在 React Native Picker 中仅渲染唯一标签
- javascript - 编写一个接受两个整数并显示较大的 JavaScript 程序。
- error-handling - 角度 5 中的错误处理,从前端的后端 api 捕获错误
- mysql - 错误:函数崩溃。详细信息:连接 ECONNREFUSED /cloudsql/
> - python - 转换成 python 类 IndexError
- mysql - 使用多线程(delphi)缓慢恢复mysql数据库
- c++ - Qt 中的调试显示无法访问
- ssis - (Azure) 数据工厂到数据仓库 - 动态命名登陆表和架构