亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Discord.py 在 message.content 中使用列表?

Discord.py 在 message.content 中使用列表?

嗶嗶one 2023-12-09 15:58:26
@bot.eventasync 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 的命令)。但我真的不知道如何讓它發揮作用。
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

你無法檢查列表是否在字符串中,你做錯了。你想做的是,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]

因此,它會將您的消息內容從空格中分離出來,并檢查其中一個單詞是否在單詞過濾器中。如果是,它將刪除該消息。


查看完整回答
反對 回復 2023-12-09
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號