TypeError:“bool”对象不是iterab

2024-05-19 03:02:28 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有一些问题的代码,我得到一个类型错误 哪个是TypeError: 'bool' object is not iterable我应该使用if状态而不是for语句?在

我要实现的是,如果一条消息被固定了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!")

我不知道我哪里出错了。在


Tags: 代码self消息类型messagefordateif
3条回答

问题是:

for message.content in message.channel.id == channelid:

==正在检查是否mess.age.频道.id和channelid相等,因此您的statemnts有效地

^{pr2}$

或者

for message.content in false:

for循环遍历列表或类似结构中的每个元素,因此不能在“in”后面使用布尔值

我猜你们是不是想把channelid分配给消息.channel.id,然后循环。e、 g

message.channel.id = channelid
for message.content in message.channel.id:

for message.content in message.channel.id == channelid:

也许你是想

if message.channel.id == channelid:
    for message.content in message.channel.id

在for循环中,message.channel.id == channelid的计算结果为True或{}的布尔值。所以你的for循环变成了

for message.content in True

或者

^{pr2}$

此处in的右侧必须是某个iterable。编译器会抱怨,因为它不是

为了提出这个问题的解决方案,我们需要更多关于您正在尝试做什么的信息。在

相关问题 更多 >

    热门问题