Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python discord音乐机器人youtube\u dl_Python_Discord.py - Fatal编程技术网

python discord音乐机器人youtube\u dl

python discord音乐机器人youtube\u dl,python,discord.py,Python,Discord.py,有没有办法修复这个错误 import discord import youtube_dl from discord.ext import commands ----------------------------------------------- @cat.command(pass_context=True) async def play(ctx): if not ctx.message.author.voice: await ctx.send('you are

有没有办法修复这个错误

import discord

import youtube_dl

from discord.ext import commands
-----------------------------------------------
@cat.command(pass_context=True)
async def play(ctx):
    if not ctx.message.author.voice:
        await ctx.send('you are not connected to a voice channel')
        return

    else:
        channel = ctx.message.author.voice.channel

    await channel.connect()

    server = ctx.message.guild
    voice_channel = server.voice.client

    async with ctx.typing():
        player = await YTDLSource.from_url(url, loop = client.loop)
        voice_channel.play(player)
        
    await ctx.send(f'**Music:**{player.title}')

如果不是ctx.message.author.voice:请尝试将
替换为
如果“voice”不在ctx.message.author:

youtube\u dl和FFmpeg中有一些更改,因此这应该可以工作

AttributeError: 'Guild' object has no attribute 'voice'
可选的、有用的功能 如果需要,您可以在歌曲/音乐停止播放后使您的机器人离开语音频道。将此添加到代码末尾

import discord

import youtube_dl

from discord.ext import commands

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}   

def endSong(guild, path):
    os.remove(path)                                   

@cat.command(pass_context=True)
async def play(ctx, url):
    if not ctx.message.author.voice:
        await ctx.send('you are not connected to a voice channel')
        return

    else:
        channel = ctx.message.author.voice.channel

    voice_client = await channel.connect()

    guild = ctx.message.guild

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        file = ydl.extract_info(url, download=True)
        path = str(file['title']) + "-" + str(file['id'] + ".mp3")

    voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
    voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)

    await ctx.send(f'**Music: **{url}')
如果有什么事情不起作用
如果您在理解代码方面有任何问题,或者有些东西不起作用,请在我的答案下发表评论,我将尽力帮助您。顺便说一句,如果你想在Heroku上托管你的机器人,我可以帮你,因为你需要做一些事情才能让你的音乐机器人在那里运行。

错误:voice\u channel=server.voice.client AttributeError:“Guild”对象没有属性“voice”@return2749对
voice\u channel=server.voice.client
执行类似的检查,查看它是否有
voice
键:
如果“voice”不在服务器:
。。。
while voice_client.is_playing():
        await asyncio.sleep(1)
    else:
        await voice_client.disconnect()
        print("Disconnected")