Discord.py : AttributeError: 'NoneType' object has no attribute 'create_ytdl_player'

Discord.py : AttributeError: 'NoneType' object has no attribute 'create_ytdl_player'

我有一个音乐机器人(Discord 服务器)的代码: 这是API:https://discordpy.readthedocs.io/en/latest/api.html

@client.event
async def on_message(message):
import os
import random
msgnach = str(message.content)
suche = str(msgnach[6:len(msgnach)])
if(msgnach.startswith("-play")):
    channel = message.author.voice.voice_channel
    server = message
    voice = client.voice_client_in(server)
    if(client.is_voice_connected(server)):
        print("Schon connectet")
        await voice.disconnect()
        sleep(1)
        await client.join_voice_channel(channel)

    else:
        await client.join_voice_channel(channel)
    voice = client.voice_client_in(server)

    if("https" not in suche):
        player = await voice.create_ytdl_player("ytsearch:"+suche)


    else:
        player = await voice.create_ytdl_player(suche, ytdl_options="--skip-download")

    player_dict[server.id] = player

    player.start()

机器人加入了我的频道,但不播放音乐。 在控制台中我有这个错误:

Ignoring exception in on_message
Traceback (most recent call last):
 File "D:\Programmieren\Python\PassiBot\venv\lib\site-packages\discord\
     client.py", line 307, in _run_event
 yield from getattr(self, event)(*args, **kwargs)
 File "D:/Programmieren/Python/PassiBot/main.py", line 128, in on_message
player = await voice.create_ytdl_player("ytsearch:"+suche)
AttributeError: 'NoneType' object has no attribute 'create_ytdl_player'

我试了很多,还是没有解决。

join_voice_channel returns 它创建的 VoiceClient。你应该能够取消

voice = client.voice_client_in(server) 

行,将其上方的代码更改为

if(client.is_voice_connected(server)):
    print("Schon connectet")
    await voice.disconnect()
    await asyncio.sleep(1)
    voice = await client.join_voice_channel(channel)

else:
    voice = await client.join_voice_channel(channel)