为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 48 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信