【问题标题】:'VoiceState' object has no attribute 'disconnect' (discord.py)'VoiceState' 对象没有属性'disconnect' (discord.py)
【发布时间】:2020-10-30 22:28:27
【问题描述】:

问题

你好,我正在学习discord.py。在我的编码过程中,我尝试创建一个可以在我的不和谐服务器中加入和离开语音频道的机器人。当我尝试运行代码时出现问题。机器人完美加入频道,但当我在不和谐聊天中输入 ()leave 时,我在控制台中收到此错误。

错误

“VoiceState”对象没有“断开连接”属性

这是我的机器人的代码。

代码

@client.command(pass_context = True) # Join Voice Channel.
async def join(ctx):
    channel = ctx.message.author.voice.channel
    print(f"Joining into: {channel}.")
    await channel.connect()

@client.command(pass_context = True) # Leave Voice Channel.
async def leave(ctx):
    channel = ctx.message.author.voice
    print(f"Leaving: {channel}.")
    await channel.disconnect()

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    您在VoiceState 上调用disconnect,正如您的错误所示,您不能这样做,因为它没有具有该名称的函数。快速查看API docs 建议您应该在VoiceClient 实例上调用disconnect,而不是VoiceState

    您可以从ctx 中获取VoiceClient

    @client.command() # Leave Voice Channel
    async def leave(ctx):
        channel = ctx.voice_client.channel
        print(f"Leaving: {channel}.")
        await ctx.voice_client.disconnect()
    

    附言。从 Discord Rewrite 开始,Discord 现在自动传递 Context,因此 pass_context=True kwarg 已完全过时。

    【讨论】:

    • 非常感谢,它成功了。现在我可以很好地使用 ()join 和 ()leave。谢谢!
    • @Giuseppe 如果它有效,那么您应该将我的答案标记为正确
    猜你喜欢
    • 2020-12-17
    • 2021-07-27
    • 2021-01-05
    • 2021-03-06
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2021-02-04
    相关资源
    最近更新 更多