@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
我想要一个单词过滤器,里面有很多单词,所以我想有一个列表,在那里我可以添加我所有的单词(以后甚至可以使用Discord的命令)。 但我真的不知道如何让它工作
您无法检查列表是否在字符串中,因为您做错了。您试图做的是
if message.content in wordfilter
,但这也行不通。您需要获取消息中的每个单词,然后检查其中是否有一个在wordfilter
中,还需要从事件中创建wordfilter
列表,这样它就不会每次都创建一个新列表,从而使代码更加优化。因此,您只需在一行中完成:因此,它将从空格中分割消息内容,并检查其中一个单词是否在wordfilter中。如果是,它将删除该消息
相关问题 更多 >
编程相关推荐