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

Discord VC锁定权限异常求助:执行命令交互失败无报错

问题描述

执行lock命令锁定语音频道时显示交互失败,无任何错误提示。添加else: await interaction.response.send_message(content='You are not the author of this channel.')后,只会返回该提示;尝试移除interaction.user.voice中的channel字段,问题仍未解决。

相关代码:

@app_commands.command(name='lock', description='Locks the VC you are currently in')
async def lock(self, interaction:discord.Interaction):
    channel = interaction.user.voice.channel
    if interaction.user.voice is None:
        await interaction.response.send_message(content='You are not in a voice channel.')
    
    if channel.id in data and data[channel.id] == interaction.user.id:
        overwrite = channel.overwrites_for(interaction.guild.default_role)
        overwrite.connect = False
        await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
        await interaction.response.send_message(content='VC has been locked, only Moderators and Admins can join.')
问题分析与修复

核心问题1:未先检查用户状态就访问频道属性

代码先执行channel = interaction.user.voice.channel,如果用户不在任何语音频道,interaction.user.voice是None,直接访问.channel会触发属性错误,导致交互无声失败。必须先判断用户是否在语音频道,再获取频道对象。

核心问题2:缺少非频道创建者的响应分支

当用户在语音频道,但不是该频道创建者(channel.id不在data中,或data[channel.id]不等于用户ID)时,代码没有发送任何响应,Discord会因交互超时显示失败。

修正后的代码

@app_commands.command(name='lock', description='Locks the VC you are currently in')
async def lock(self, interaction:discord.Interaction):
    # 先检查用户是否在语音频道
    if interaction.user.voice is None:
        await interaction.response.send_message(content='You are not in a voice channel.')
        return
    
    # 确认用户在频道后再获取频道对象
    channel = interaction.user.voice.channel
    
    # 验证用户是否为频道创建者
    if channel.id in data and data[channel.id] == interaction.user.id:
        overwrite = channel.overwrites_for(interaction.guild.default_role)
        overwrite.connect = False
        await channel.set_permissions(interaction.guild.default_role, overwrite=overwrite)
        await interaction.response.send_message(content='VC has been locked, only Moderators and Admins can join.')
    else:
        # 处理非创建者的情况,返回明确提示
        await interaction.response.send_message(content='You are not the author of this channel.')

关键修改说明

  • 调整判断顺序:先检查用户是否在语音频道,避免属性错误。
  • 添加return:用户不在频道时发送消息后直接返回,终止后续代码执行。
  • 补充else分支:确保所有场景都有交互响应,解决超时失败问题。

内容的提问来源于stack exchange,提问作者French fry with a fade

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 06:01:12