You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py批量Reclone服务器文本频道报404错误,求修复方案

问题原因分析

报错NotFound: 404 Not Found (error code: 10003): Unknown Channel核心原因有两个:

  • 直接遍历的ctx.guild.text_channels是Discord动态维护的频道集合,删除频道后这个集合会实时更新,导致迭代过程中拿到已被删除/不存在的频道对象。
  • 使用同步的time.sleep()会阻塞Discord机器人的事件循环,导致bot心跳超时、响应异常,甚至中途停止运行。
  • 额外风险:删除原频道后再访问channel.position,此时原频道对象已失效,虽可能暂时拿到属性,但存在触发异常的隐患。
修复后的代码
import asyncio
from discord.ext import commands

@client.command()
async def reclone(ctx):
    # 转换为静态列表,避免迭代时集合动态变化
    text_channels = list(ctx.guild.text_channels)
    for channel in text_channels:
        # 异步sleep替代同步sleep,不阻塞事件循环
        await asyncio.sleep(0.2)
        print(f"Cloning {channel.name}")
        # 提前保存原频道位置,防止删除后对象失效
        original_position = channel.position
        # 克隆频道
        newchannel = await channel.clone(reason="Channel has been nuked")
        print(f"Cloned {channel.name}")
        # 删除原频道
        await channel.delete()
        print(f"Nuked {channel.name}")
        await asyncio.sleep(0.2)
        # 使用提前保存的位置调整新频道顺序
        await newchannel.edit(position=original_position)
        print(f"Repositioned {newchannel.name}")
        await newchannel.send("**[Soon]** :copyright:")
        print(f"Sent message to {newchannel.name}")
修复原理说明
  1. 静态列表遍历:把ctx.guild.text_channels转为list()后,得到的是当前时刻所有文本频道的固定快照,后续删除频道不会影响这个列表的内容,彻底避免迭代时遇到已不存在的频道。
  2. 异步sleep替代同步sleep:asyncio.sleep()是异步非阻塞的,只会暂停当前任务,不会干扰bot的事件循环(比如处理消息、维持Discord心跳等),防止bot因阻塞被判定为离线。
  3. 提前保存频道位置:在删除原频道前就把channel.position存入变量,避免删除后访问已失效的频道对象属性,消除潜在异常风险。

内容的提问来源于stack exchange,提问作者Lumiscnt1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:15:34