不和谐语音对象没有属性“播放”

时间:2020-11-08 13:27:00

标签: python discord.py-rewrite

嘿,我正在尝试在不和谐服务器的语音聊天中播放音频,但出现错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'play'

我的代码

import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from youtube_dl import YoutubeDL
import os

@bot.command('play')
async def jp(ctx, url):
    global voice
    chan = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

 ydl_op ={
        'format':'bestaudio/best',
        'postprocessors':[{
            'key':'FFmpegExtractAudio',
            'preferredcodec':'mp3',
            'preferredquality':'256'
        }]
    }
    with YoutubeDL(ydl_op) as ydl:
        ydl.download([url])

    for file in os.listdir('./'):
        if file.endswith('.mp3'):
            name = file
            os.rename(file,'audio.mp3')

    voice.play(FFmpegPCMAudio['audio.mp3'], after=lambda e: print(f"Done playing {name}"))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.07

    nname = name.rsplit('-',2)**

有一个相同的问题,但没有任何经过验证的答案

1 个答案:

答案 0 :(得分:0)

此代码必须以None的身份返回语音

尝试

voice = get(bot.voice_clients, guild=ctx.guild)
    if voice and voice.is_connected:
        await voice.move_to(channel)
    else:
        voice = await channel.connect()