如何自动删除不和谐的频道b

2024-06-26 13:41:33 发布

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

我在做一个Python机器人来解决不和。它根据玩家的指令创建一个删除通道。 我想创建一个垃圾收集器来测试服务器.channels删除过时的。在

我知道:

async def waitTimer():
    while True:
        await asyncio.sleep(10)

        regex = re.compile(r"[0-9]*_[a-z0-9]*-[0-9]*") #nom des channels de raid

        for cCurrent in client.get_all_channels():
            if regex.match(cCurrent.name):
                numRaid = int(cCurrent.name[0])
                cRaidCurrent = cRaids[numRaid]
                date = datetime.datetime.now()
                print (cRaidCurrent.raid.fin.timestamp())
                print (date.timestamp())
                if cRaidCurrent.raid.fin < date:
                    if cRaidCurrent.retirerRaid():
                        cId = cRaidCurrent.com.id
                        await removeFromListe(cRaidCurrent)
                        await client.delete_channel(client.get_channel(cId))
                        cCurrent = 0

有时它会过去,有时我会得到这样的错误:

^{pr2}$

如果我清楚地理解它,client.get_all_channels是一个字典,我不能在迭代过程中删除通道。。。所以问题是我还有什么其他的方法可以移除这些频道?在


Tags: nameclientgetdatetimedateifallawait
1条回答
网友
1楼 · 发布于 2024-06-26 13:41:33

感谢Patrick Haugh你的回答非常好。在

删除操作需要在最后完成2次。下面是代码,如果有人需要的话:

for cCurrent in client.get_all_channels():
    if regex.match(cCurrent.name):
        numRaid = getNumChannel(cCurrent.name)
        cRaidCurrent = cRaids[numRaid]
        now = datetime.datetime.now()
        if cRaidCurrent.raid.fin < now:
            toDelete.append(cRaidCurrent)

for cRaidCurrent in toDelete:
    cId = cRaidCurrent.id
    cRaidCurrent.retirerRaid()
    await removeCRaid(cRaidCurrent)
    del cRaids[cId]

相关问题 更多 >