简体   繁体   中英

TypeError: on_voice_state_update() takes 2 positional arguments but 3 were given

I'm trying to make a bot that gives members a role while being on a voice channel, but for some reason it doesn't work.

Here's the code:

@client.event
async def on_voice_state_update(before, after):
    role = discord.utils.get(after.server.roles, name="glosowy")
    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)

And here's the error I receive:

Ignoring exception in on_voice_state_update
Traceback (most recent call last):

  File "C:\Users\aaa\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)

TypeError: on_voice_state_update() takes 2 positional arguments but 3 were given

you forgot the parameter 'member'

from the documentation

async def on_voice_state_update(member, before, after)

before and after are VoiceState which stand for information on if the member (in parameter) is muted, broadcasting etc... see here

therefore you can't retrieve role from it you have to get it from the member

@client.event
async def on_voice_state_update(member, before, after):
    role = discord.utils.get(member.guild.roles, name="glosowy")
    if role: # verify their is a role with that name
        if not before.channel and after.channel: # check member just entered a channel
            await member.add_roles(role) #add role to it 
        elif before.channel and not after.channel: # check member just left a channel
            await member.remove_roles(role) # remove role to it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM