Discord VC配置按钮异常及Kick功能替代方案咨询
问题1:Lock按钮点击后其他按钮失效的解决方法
你的问题根源是点击Lock按钮后,通过send_message发送了新消息,但原嵌入消息的View没有被更新,导致View实例的交互会话结束,后续点击其他按钮时触发"interaction failed"。
修复方案
修改Lock和Unlock按钮的逻辑,点击后更新原消息的View状态(禁用/启用其他按钮),并通过edit_message修改原消息,而不是发送新消息:
class VCView(ui.View): def __init__(self): # 设置timeout=None让View永久有效(需开启组件持久化,按需调整) super().__init__(timeout=None) @ui.button(label="Lock", style=discord.ButtonStyle.primary) async def lock_vc(self, interaction:discord.Interaction, button: discord.ui.Button): voice_channel = interaction.user.voice.channel canned = discord.Embed(description="Voice channel locked.",color=discord.Color.random()) await voice_channel.set_permissions(interaction.guild.default_role, connect=False) # 禁用除Lock外的所有按钮 for btn in self.children: if btn != button: btn.disabled = True # 更新原消息的嵌入和View await interaction.response.edit_message(embed=canned, view=self) @ui.button(label="Unlock", style=discord.ButtonStyle.primary) async def unlock_vc(self, interaction:discord.Interaction, button: discord.ui.Button): voice_channel = interaction.user.voice.channel chan = discord.Embed(description="Voice channel unlocked.",color=discord.Color.random()) await voice_channel.set_permissions(interaction.guild.default_role, connect=True) # 重新启用所有按钮 for btn in self.children: btn.disabled = False # 更新原消息的嵌入和View await interaction.response.edit_message(embed=chan, view=self) @ui.button(label="Rename", style=discord.ButtonStyle.primary) async def rename_vc(self, interaction:discord.Interaction, button: discord.ui.Button): await interaction.response.send_modal(RenameModal())
问题2:Kick功能的替代实现方式
对于需要member参数的Kick功能,推荐以下两种更高效的实现:
1. 斜杠命令(Slash Command)
直接创建带成员参数的斜杠命令,用户可直接指定目标成员,体验更直观:
@bot.tree.command(name="kick", description="从当前语音频道踢出成员") async def kick_member(interaction: discord.Interaction, member: discord.Member, reason: str = "无理由"): # 检查用户是否在语音频道且有权限 if not interaction.user.voice: await interaction.response.send_message("你不在任何语音频道中", ephemeral=True) return vc = interaction.user.voice.channel if not vc.permissions_for(interaction.user).kick_members: await interaction.response.send_message("你没有权限在此频道踢人", ephemeral=True) return await vc.kick(member, reason=reason) embed = discord.Embed(description=f"已将 {member.mention} 踢出语音频道,理由:{reason}", color=discord.Color.random()) await interaction.response.send_message(embed=embed)
2. 上下文菜单(Context Menu)
给用户添加右键菜单选项,点击后直接触发踢人操作,无需手动输入成员:
@bot.tree.context_menu(name="从我的VC踢出") async def kick_from_vc(interaction: discord.Interaction, member: discord.Member): if not interaction.user.voice: await interaction.response.send_message("你不在任何语音频道中", ephemeral=True) return vc = interaction.user.voice.channel if not vc.permissions_for(interaction.user).kick_members: await interaction.response.send_message("你没有权限在此频道踢人", ephemeral=True) return await vc.kick(member) await interaction.response.send_message(f"已将 {member.mention} 踢出你的语音频道", ephemeral=True)
内容的提问来源于stack exchange,提问作者Janicooking
相关产品推荐
相关产品推荐

