TypeError: 'bool' 对象不可迭代

新手上路,请多包涵

嗨,我遇到了一些代码问题,我收到一个类型错误,即 TypeError: 'bool' object is not iterable 我应该使用 if 状态而不是 for 声明?

我想要实现的是,如果 on_message 一条消息已固定 7 天或更长时间,然后取消固定该消息。

这是我正在使用的:

 async def on_message(self, message):
    """Listen for a message then unpin any other messages older than 7 days"""
    server = message.server
    channelid = '490899209067823135'
    limit_date = datetime.now() - timedelta(days=7)
    if server:
        for message.content in message.channel.id == channelid:
            if limit_date:
                try:
                    await self.bot.unpin_message(message)

                except discord.Forbidden:
                    print("No permissions to do that!")

不知道我哪里错了。

原文由 Shan Jameson 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
1 个回答

在您的 for 循环中, message.channel.id == channelid 评估为布尔值 TrueFalse 。所以你的 for 循环变成

for message.content in True

或者

for message.content in False

in 的右侧必须是一些可迭代的。编译器抱怨,因为它不是。

要建议解决此问题的方法,我们需要有关您尝试执行的操作的更多信息。

原文由 Code-Apprentice 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题