我是初学者,所以不要讨厌我。我试图给某人一个角色,当他们加入一个特定的语音通道。
下面是我的代码:
import discord
from discord import Member, Guild
client = discord.Client()
#########################################################################
@client.event
async def on_ready():
print('Hello!'.format(client.user.name))
@client.event
async def on_voice_state_update(before, after ):
role = discord.utils.get(after.server.roles, name="Talk-1")
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)
client.run('My Token')```
你的方向是对的。您只需要定义特定语音通道的ID,然后检查after.channel.id
是否等于该ID。还要确保使用member.add_roles
而不是client.add_roles
。所以像这样的代码应该可以工作:
@client.event
async def on_voice_state_update(member, before, after):
role = discord.utils.get(after.server.roles, name="Talk-1")
channel_id = 123456
if before.channel is None or before.channel.id != voice_channel:
if after.channel is not None and after.channel.id == voice_channel:
await member.add_roles(vc_role)
#reversed from above
if after.channel is None or after.channel.id != voice_channel:
if before.channel is not None and before.channel.id == voice_channel:
await member.remove_roles(vc_role)
https://discordpy.readthedocs.io/en/master/api.html?highlight=voice%20state%20update discord.on_voice_state_update