问题
I am learning how to use discord.py and I want to make bot feature that give role when we join particular voice channel and removes the same role when user leaves the channel
@client.event
async def on_voice_state_update():
回答1:
on_voice_state_update
gives you after
and before
arguments, here's how to check when a member joined and left a voice channel
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
To add roles, first you need to get a discord.Role
object, then you can do member.add_roles(role)
role = member.guild.get_role(role_id)
await member.add_roles(role)
To remove a role is the same, but .remove_roles
await member.remove_roles(role)
EDIT:
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
Reference:
- on_voice_state_update
- Guild.get_role
- Member.add_roles
- Member.remove_roles
来源:https://stackoverflow.com/questions/65446794/how-to-make-a-bot-that-gives-role-when-we-join-a-particular-voice-channel-and-re