简体   繁体   English

TypeError: on_voice_state_update() 需要 2 个位置 arguments 但给出了 3 个

[英]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之前和之后是 VoiceState 代表成员(在参数中)是否静音,广播等信息......请参阅 此处

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:update()接受1到2个位置参数,但给出了3个。 - TypeError : update() takes from 1 to 2 positional arguments but 3 were given. 类型错误:update() 需要 2 个位置参数,但给出了 3 个:Python - TypeError: update() takes 2 positional arguments but 3 were given : Python TypeError:create()接受2个位置参数,但给出了4个 - TypeError: create() takes 2 positional arguments but 4 were given 类型错误:randint() 需要 3 个位置参数,但给出了 4 个 - TypeError: randint() takes 3 positional arguments but 4 were given TypeError:deriv()接受2个位置参数,但给出了4个 - TypeError: deriv() takes 2 positional arguments but 4 were given TypeError:init()接受0个位置参数,但给出了5个 - TypeError: init() takes 0 positional arguments but 5 were given TypeError: _() 需要 2 个位置 arguments 但有 4 个被赋予 Databricks - TypeError: _() takes 2 positional arguments but 4 were given Databricks TypeError:open()接受0个位置参数,但给出了2个 - TypeError: open() takes 0 positional arguments but 2 were given TypeError:invoke()接受2个位置参数,但给出了3个 - TypeError: invoke() takes 2 positional arguments but 3 were given TypeError: imwrite() 接受 2 个位置参数,但给出了 3 个 - TypeError: imwrite() takes 2 positional arguments but 3 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM