如何用命令锁定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.
相关产品推荐
相关产品推荐

