【发布时间】:2021-04-03 09:44:17
【问题描述】:
我正在学习如何使用 discord.py,我想制作机器人功能,当我们加入特定语音频道时赋予角色,并在用户离开频道时移除相同角色
@client.event
async def on_voice_state_update():
【问题讨论】:
-
嗨,有没有授予状态的命令
标签: python discord.py
我正在学习如何使用 discord.py,我想制作机器人功能,当我们加入特定语音频道时赋予角色,并在用户离开频道时移除相同角色
@client.event
async def on_voice_state_update():
【问题讨论】:
标签: python discord.py
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)
删除一个角色也是一样的,只不过@987654332@
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
参考:
【讨论】: