@bot.eventasync def on_message(message): wordfilter = ['badword', 'anotherone', 'and the last one'] if wordfilter in message.content: await message.delete()錯(cuò)誤: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我想要一個(gè)單詞過濾器,里面有大量單詞,所以我喜歡有一個(gè)列表,我可以在其中添加所有單詞(稍后甚至可以使用 Discord 的命令)。但我真的不知道如何讓它發(fā)揮作用。
1 回答

萬千封印
TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
你無法檢查列表是否在字符串中,你做錯(cuò)了。你想做的是,if message.content in wordfilter但這也是行不通的。您需要獲取消息中的每個(gè)單詞,然后檢查其中一個(gè)單詞是否在其中wordfilter,并且您還需要wordfilter在事件之外創(chuàng)建列表,這樣就不會(huì)每次都創(chuàng)建新列表,并且可以使您的代碼更加優(yōu)化。所以你可以簡單地用一行來完成:
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]
因此,它會(huì)將您的消息內(nèi)容從空格中分離出來,并檢查其中一個(gè)單詞是否在單詞過濾器中。如果是,它將刪除該消息。
添加回答
舉報(bào)
0/150
提交
取消