【问题标题】:How to use a discord.py bot that gives a user role when they join VC and remove it when they leave如何使用 discord.py 机器人,在他们加入 VC 时赋予用户角色并在他们离开时将其删除
【发布时间】:2019-06-01 19:57:32
【问题描述】:
我正在尝试制作一个不和谐的机器人,当他们加入语音频道时赋予用户角色并在他们离开时删除角色。我知道 on_voice_state_update 以及如何给用户一个角色,但我不知道如何让加入频道的用户给他们角色。
现在我的代码是来自答案的略微修改版本
How to use discord.py event handler on_voice_state_update to run only when a user joins a voice channel。
@client.event
async def on_voice_state_update(之前,之后):
如果 before.voice.voice_channel 是 None 并且 after.voice.voice_channel 不是 None:
对于 before.server.channels 中的频道:
如果 channel.name == '一般':
await client.send_message(channel, "用户加入")
elif before.voice.voice_channel is not None and after.voice.voice_channel is None:
for channel in before.server.channels:
if channel.name == 'general':
await client.send_message(channel, "User left");
【问题讨论】:
标签:
python
python-3.x
discord
discord.py
【解决方案1】:
晚了三个月,但对于以后偶然发现的人来说,这里是 rewite (1.0) 分支版本。
# VC PROCESSING
@client.event
async def on_voice_state_update(member, before, after):
if not before.channel and after.channel:
role = discord.utils.get(member.guild.roles, name="role name")
await member.add_roles(role)
elif before.channel and not after.channel:
role = discord.utils.get(member.guild.roles, name="role name")
await member.remove_roles(role)
【解决方案2】:
after 是 Member 对象,因为它“现在”存在,在语音状态发生变化之后。那将是您传递给 add_roles
的成员对象
@client.event
async def on_voice_state_update(before, after ):
role = discord.utils.get(after.server.roles, name="YOUR ROLE NAME")
if not before.voice.voice_channel and after.voice.voice_channel:
await client.add_roles(after, role)
elif before.voice.voice_channel and not after.voice.voice_channel:
await client.remove_roles(after, role)