【问题标题】:how to automatically delete channels with a discord bot如何使用不和谐机器人自动删除频道
【发布时间】:2018-03-08 00:19:20
【问题描述】:

我正在为不和谐做一个 python 机器人。它根据播放器指令创建一个删除频道。 我想创建一个垃圾收集器来测试所有 server.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

有时它会通过,有时我会收到此错误:

    for cCurrent in client.get_all_channels():
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/client.py", line 581,
 in get_all_channels
    for channel in server.channels:
RuntimeError: dictionary changed size during iteration

如果我清楚地理解 client.get_all_channels 是一本字典,我无法在迭代过程中删除通道......所以问题是我还必须删除这些通道还有哪些其他可能性?

【问题讨论】:

  • 遍历 get_all_channels 并构建要删除的频道列表,然后在单独的循环中删除这些频道

标签: python python-3.x dictionary runtime-error discord.py


【解决方案1】:

感谢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]

【讨论】:

    猜你喜欢
    • 2020-09-06
    • 2020-08-25
    • 2020-12-28
    • 2021-07-25
    • 2021-01-20
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多