Discord语音频道配置按钮失效问题及VC所有者权限设置咨询
问题1:LFG系统按钮点击触发AttributeError错误
错误信息
2024-02-07 11:53:04 ERROR discord.ui.view Ignoring exception in view <VCView timeout=180.0 children=13> for item <Button style=<ButtonStyle.primary: 1> url=None disabled=False label='Lock' emoji=None row=None> Traceback (most recent call last): File "C:\Users\trace\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ui\view.py", line 427, in _scheduled_task await item.callback(interaction) File "c:\Users\trace\Desktop\MU LFG\lfg bot\lfg.py", line 423, in lock_vc voice_channel = interaction.user.voice.channel AttributeError: 'Button' object has no attribute 'user'
问题原因
按钮回调函数的参数顺序错误。discord.ui.button装饰的回调函数中,第二个参数应为interaction: discord.Interaction,第三个才是button: discord.ui.Button。你的代码把button放在了interaction前面,导致触发回调时,interaction变量实际接收的是Button对象,自然没有user属性。
修正后的代码
class VCView(ui.View): def __init__(self): super().__init__() @ui.button(label="Lock", style=discord.ButtonStyle.primary) async def lock_vc(self, interaction: discord.Interaction, button: discord.ui.Button): # 先判断用户是否在语音频道中 if not interaction.user.voice or not interaction.user.voice.channel: canned = discord.Embed(description="你不在任何语音频道中。", color=discord.Color.red()) return await interaction.response.send_message(embed=canned, ephemeral=True) 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) await interaction.response.send_message(embed=canned)
问题2:设置语音频道所有者的删除频道、移动成员等权限
可以通过VoiceChannel.set_permissions()方法,给频道所有者单独分配权限。需要提前记录频道的所有者(比如创建频道时绑定发起LFG的用户),然后针对性设置权限位:
代码示例
# 假设owner是频道所有者的Member对象 await voice_channel.set_permissions( owner, manage_channels=True, # 允许删除/管理频道 move_members=True, # 允许移动成员 manage_permissions=True, # 允许修改频道权限 priority_speaker=True, # 允许使用优先发言 # 可根据需求添加其他权限 )
常用权限对应说明:
manage_channels: 管理频道(包括删除、修改频道信息)move_members: 将成员移出频道manage_permissions: 修改频道权限mute_members: 静音成员deafen_members: 使成员失聪
内容的提问来源于stack exchange,提问作者Janicooking
相关产品推荐
相关产品推荐

