我正在学习如何使用 discord.py 并且我想制作机器人功能,当我们加入特定的语音频道时赋予角色并在用户离开频道时删除相同的角色
@client.event
async def on_voice_state_update():
答案 0 :(得分:3)
on_voice_state_update
为您提供 after
和 before
参数,以下是检查成员何时加入和离开语音频道的方法
async def on_voice_state_update(member, before, after):
if before.channel is None and after.channel is not None:
# member joined a voice channel, add the roles here
elif before.channel is not None and after.channel is None:
# member left a voice channel, remove the roles here
要添加角色,首先需要获取一个discord.Role
对象,然后才能执行member.add_roles(role)
role = member.guild.get_role(role_id)
await member.add_roles(role)
删除角色是一样的,但是.remove_roles
await member.remove_roles(role)
编辑:
async def on_voice_state_update(member, before, after):
channel = before.channel or after.channel
if channel.id == some_id:
if before.channel is None and after.channel is not None:
# member joined a voice channel, add the roles here
elif before.channel is not None and after.channel is None:
# member left a voice channel, remove the roles here
参考: