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

如何用命令锁定Discord服务器所有频道?代码修复求助

问题修复方案

你的代码核心问题是循环时错误地重复修改了命令执行频道的权限,而非遍历到的每个频道。

修复后的代码如下:

@client.command()
@commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
    all_channels = ctx.guild.channels
    role = discord.utils.get(ctx.guild.roles, id=member_role_id)
    everyone = discord.utils.get(ctx.guild.roles, id=everyone_role_id)
    # 遍历服务器内所有频道,仅对文本频道设置权限
    for channel in all_channels:
        # 过滤掉语音频道、分类频道等不需要设置发消息权限的频道类型
        if isinstance(channel, discord.TextChannel):
            await channel.set_permissions(role, send_messages=False)
    channel = client.get_channel(bulletin_chan_id)
    await channel.send(f"{everyone.mention} {lockdown_message}")

关键修改说明:

  • 将循环内的ctx.channel替换为当前遍历的channel变量,确保每个频道都被设置权限
  • 增加isinstance(channel, discord.TextChannel)判断,避免对语音频道、分类频道等无效频道做冗余操作

内容的提问来源于stack exchange,提问作者Maria M.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 10:45:31