python - discord.py 在 message.content 中使用列表?
问题描述
@bot.event
async def on_message(message):
wordfilter = ['badword', 'anotherone', 'and the last one']
if wordfilter in message.content:
await message.delete()
错误:
Traceback (most recent call last):
File "C:path/client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:path/combot.py", line 34, in on_message
if wordfilter in message.content:
TypeError: 'in <string>' requires string as left operand, not list
我想要一个 wordfilter,里面有很多单词,所以我想有一个列表,我可以在其中添加我所有的单词(以后甚至使用 Discord 的命令)。但我真的不知道如何让它工作。
解决方案
您无法检查列表是否在字符串中,您做错了。你想要做的是if message.content in wordfilter
,但这也行不通。您需要获取消息中的每个单词,然后检查其中一个是否在其中,wordfilter
并且您还需要wordfilter
从事件中创建列表,这样它就不会每次都创建一个新列表,并且它使您的代码更加优化。因此,您可以简单地在一行中完成:
wordfilter = ['badword', 'anotherone', 'and the last one']
@bot.event
async def on_message(message):
[await message.delete() for word in message.content.split(' ') if word in wordfilter]
因此,它会将您的消息内容从空格中拆分出来,并检查其中一个单词是否在 wordfilter 中。如果是,它将删除该消息。
推荐阅读
- pdf - 呈现为 pdf 的 Visualforce 页面可以使用 Docusign 发送给 Esign
- c# - 有没有办法以链接格式从根目录一次获取所有文件以使用图形 api 进行查看?
- postgresql - 如何安全地向数据库添加新货币操作?
- c# - 注册数据未显示在 ASP.NET 项目的数据库中
- javascript - Nuxt:如何在应用初始化时加载数据以存储
- c - 使用 Parson 库创建一个 json 数组?
- django - 如何在 django 模型中订购一个choicefiled
- testcontainers - 可以在没有互联网访问的情况下使用测试容器吗?
- php - 转到个人资料页面时尝试更改我的 URL 的一部分
- javascript - 如何使用 vue 从本地文件夹下载文件(pdf)