【问题标题】:Bot cannot connect voice channelBot无法连接语音通道
【发布时间】:2020-02-19 16:00:51
【问题描述】:

Bot 不会连接到频道并播放音乐,因为它给出了错误。: AttributeError: 'Bot' object has no attribute 'is_voice_connected' 我查看了一些 discord.py 教程并编写了以下代码:

import asyncio
from discord.ext import commands
client = commands.Bot(command_prefix='!')
songs = asyncio.Queue()
play_next_song = asyncio.Event()
@client.event
async def on_ready():
    print('client ready')
async def audio_player_task():
    while True:
        play_next_song.clear()
        current = await songs.get()
        current.start()
        await play_next_song.wait()
def toggle_next():
    client.loop.call_soon_threadsafe(play_next_song.set)
@client.command(pass_context=True)
async def play(ctx, url):
    if not client.is_voice_connected(ctx.message.server):
        voice = await client.join_voice_channel(ctx.message.author.voice_channel)
    else:
        voice = client.voice_client_in(ctx.message.server)
    player = await voice.create_ytdl_player(url, after=toggle_next)
    await songs.put(player)
client.loop.create_task(audio_player_task())
client.run('TOKEN')

【问题讨论】:

    标签: python discord python-asyncio


    【解决方案1】:

    我不知道您使用的是 discord.py 重写还是任何以前的版本,但我认为这不起作用的唯一方法是您使用了错误版本的 discord.py。 Here 是您可以找到代码的正确文档的地方。

    此外,关于检查您的机器人是否已连接的错误问题,客户端在 discord.py 的重写中找不到您的语音连接。你所需要的就是

    from discord.utils import get
    
    @client.command(pass_context=True)
    async def play(ctx, url):
        voice_client = get(ctx.bot.voice_clients, guild=ctx.guild)
        if not voice_client.is_connected():
            #Do this
        else:
            #Do that
    

    如果您需要解决任何其他问题,从原始 discord.py 到重写的大部分更改都在我在此答案中提供的文档中。

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2020-12-06
      • 2022-01-05
      • 2017-07-31
      • 2021-04-10
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多