AttributeError: 'NoneType' 对象没有属性 'play'

时间:2021-04-10 14:23:10

标签: python discord.py

这是不和谐音乐机器人的代码

client = commands.Bot(command_prefix='$')


@client.event
async def on_ready():
    print('ready')


def search(query):
    with YoutubeDL({'format': 'bestaudio', 'noplaylist':'True'}) as ydl:
        try: requests.get(arg)
        except: info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
        else: info = ydl.extract_info(arg, download=False)
    return (info, info['formats'][0]['url'])


async def join(ctx, voice):
    channel = ctx.author.voice.channel

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()


@client.command(pass_context=True)
async def play(ctx, *, query):
    FFMPEG_OPTS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

    video, source = search(query)
    voice = get(client.voice_clients, guild=ctx.guild)

    await join(ctx, voice)
    await ctx.send('Music is playing')

    voice.play(FFmpegPCMAudio(source, **FFMPEG_OPTS), after=lambda e: print('done', e))
    voice.is_playing()


client.run(token)

这里是错误

在命令播放中忽略异常: 回溯(最近一次调用最后一次): 文件“D:\Aboba\venv\lib\site-packages\discord\ext\commands\core.py”,第 85 行,包裹 ret = await coro(*args, **kwargs) 文件“D:/Aboba/bommmm.py”,第 50 行,正在播放 voice.play(FFmpegPCMAudio(source, **FFMPEG_OPTS), after=lambda e: print('done', e)) AttributeError: 'NoneType' 对象没有属性 'play'

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

0 个答案:

没有答案