TypeError: on_voice_state_update() 需要 2 个位置参数

我正在尝试制作一个机器人,让成员在语音频道上扮演角色,但由于某种原因它不起作用。


这是代码:


@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)

这是我收到的错误:


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


杨魅力
浏览 130回答 1
1回答

米琪卡哇伊

你忘记了参数'member'从文档async def on_voice_state_update(member, before, after)之前和之后是 VoiceState,代表成员(在参数中)是否静音、广播等信息……因此你不能从中检索角色你必须从成员那里得到它@client.eventasync 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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python