【问题标题】:TypeError: in txt to list discord.pyTypeError: in txt to list discord.py
【发布时间】:2021-12-30 09:37:33
【问题描述】:

当您的消息与 bruh.txt 中的单词相同时,我想制作一个说 bruh 的机器人

我的代码:

bruh_file = open("bruh.txt", "r")
bruh_mo = [(line.strip()).split() for line in bruh_file]
bruh_file.close()

@client.event
async def on_message(message):
  msg = message.content
  if message.author == client.user:
    return
  if any(word in msg for word in bruh_mo):
    await message.channel.send("bruh")

但是当我尝试它时,它没有响应,我得到了这个:

忽略 on_message 中的异常 回溯(最近一次通话最后): _run_event 中的文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”,第 343 行 等待 coro(*args, **kwargs) on_message 中的文件“main.py”,第 53 行 如果有的话(味精中的单词对应bruh_mo中的单词): 文件“main.py”,第 53 行,在 如果有的话(味精中的单词对应bruh_mo中的单词): TypeError:'in'需要字符串作为左操作数,而不是列表 忽略 on_message 中的异常 回溯(最近一次通话最后): _run_event 中的文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”,第 343 行 等待 coro(*args, **kwargs) on_message 中的文件“main.py”,第 53 行 如果有的话(味精中的单词对应bruh_mo中的单词): 文件“main.py”,第 53 行,在 如果有的话(味精中的单词对应bruh_mo中的单词): TypeError:'in'需要字符串作为左操作数,而不是列表 172.18.0.1 - - [30/Dec/2021 09:27:25] “头/HTTP/1.1”200 -

谁能帮帮我

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    bruh_mo = [(line.strip()).split() for line in bruh_file] 行使bruh_mo 成为列表列表,因此当您执行any(word in msg for word in bruh_mo) 时,您基本上是在检查字符串(即msg)中是否存在列表(即word)。因此出现错误TypeError: 'in ' requires string as left operand, not list

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-27
      • 2016-11-04
      • 2021-02-21
      • 2022-12-04
      • 1970-01-01
      • 2023-01-12
      • 2022-12-26
      • 2018-04-03
      相关资源
      最近更新 更多