【问题标题】:Join voice channel (discord.py)加入语音频道(discord.py)
【发布时间】:2018-04-04 05:50:54
【问题描述】:

当我尝试让我的机器人加入我的语音频道时,我收到此错误:

await client.join_voice_channel(voice_channel)(产生错误的行)

Traceback (most recent call last):
   File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 50, in wrapped
 ret = yield from coro(*args, **kwargs)
 File "bot.py", line 215, in sfx
vc = await client.join_voice_channel(voice_channel)
File "/usr/local/lib/python3.5/site-packages/discord/client.py", line 3176, in join_voice_channel
session_id_future = self.ws.wait_for('VOICE_STATE_UPDATE', session_id_found)
AttributeError: 'NoneType' object has no attribute 'wait_for'

上述异常是以下异常的直接原因:

Traceback (most recent call last):
 File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/bot.py", line 848, in process_commands
yield from command.invoke(ctx)
 File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 369, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
 File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'wait_for'

频道名称和频道 ID 出现此错误

功能:

description = "Bot"
bot_prefix = "!"

client = discord.Client()
bot = commands.Bot(description=description, command_prefix=bot_prefix)

@bot.command(pass_context=True)
async def join(ctx):
   author = ctx.message.author
   voice_channel = author.voice_channel
   vc = await client.join_voice_channel(voice_channel)

【问题讨论】:

  • self.wsNone。不知道如何解决它,你还没有给出一个可重复的例子。
  • 我发布了函数
  • 我们可以看到更多你的代码吗?
  • 张贴一切。 (onready() 事件除外)。我试图加载作品文件,但它没有改变任何东西@GiantsLoveDeathMetal

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


【解决方案1】:

这是我用来使它工作的代码。

#Bot.py
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.voice_client import VoiceClient
import asyncio

bot = commands.Bot(command_prefix="|")

async def on_ready():
    print ("Ready")

@bot.command(pass_context=True)
async def join(ctx):
    author = ctx.message.author
    channel = author.voice_channel
    await bot.join_voice_channel(channel)

bot.run("token")

【讨论】:

  • 虽然这可能会回答这个问题,但最好解释一下答案的基本部分,以及 OPs 代码可能存在什么问题。
  • @pirho 我知道解释它会好很多,但不幸的是我自己刚开始使用 python 并且几乎不理解它。我坚持认为,至少保留自他 1 个月前发布此代码以来对我有用的代码而没有任何回应会更好。
  • 对我来说,我的机器人没有名为 join_voice_channel 的函数,它会引发错误。只有我吗?
  • @cs1349459 我遇到了同样的问题,认为这可能是一种旧方法,但使用await channel.connect() 对我有用。我必须使用 pip install PyNaCl 安装 PyNaCl,然后只需将 import nacl 添加到文件中
【解决方案2】:

摆脱

从 discord.voice_client 导入 VoiceClient 行,应该没问题。

【讨论】:

    【解决方案3】:

    试试这个

    await bot.join_voice_channel(channel)
    

    【讨论】:

    • 能否详细说明以帮助我们理解这段代码?
    猜你喜欢
    • 2021-06-06
    • 2020-08-30
    • 2022-01-04
    • 1970-01-01
    • 2017-08-11
    • 2020-10-04
    • 2021-03-02
    • 2020-11-04
    • 2020-10-07
    相关资源
    最近更新 更多