【问题标题】:Discord.py Music Bot doesn't play musicDiscord.py 音乐机器人不播放音乐
【发布时间】:2021-05-05 08:49:55
【问题描述】:

我正在尝试创建一个音乐机器人。它加入语音频道,但不在频道中播放音乐。我已经下载了包括ffmpeg在内的所有包。

我也检查了两次,但没有发现任何错误。谁能帮我改正我的代码?

@client.command(name='join', help='This command makes the bot join the voice channel')
    async def join(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()

@client.command(name='remove', help='This command removes an item from the list')
async def remove(ctx, number):
    global queue

    try:
        del (queue[int(number)])
        await ctx.send(f'Your queue is now `{queue}!`')

    except:
        await ctx.send('Your queue is either **empty** or the index is **out of range**')


@client.command(name='play', help='This command plays songs')
async def play(ctx):
    global queue

    server = ctx.message.guild
    voice_channel = server.voice_client

    async with ctx.typing():
        player = await YTDLSource.from_url(queue[0], loop=client.loop)
        voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

    await ctx.send(f'**Now playing:** {player.title} !')
    del (queue[0])


@client.command(name='pause', help='This command pauses the song')
async def pause(ctx):
    server = ctx.message.guild
    voice_channel = server.voice_client

    voice_channel.pause()


@client.command(name='resume', help='This command resumes the song!')
async def resume(ctx):
    server = ctx.message.guild
    voice_channel = server.voice_client

    voice_channel.resume()

@client.command(name='leave', help='This command stops makes the bot leave the voice channel')
async def leave(ctx):
    voice_client = ctx.message.guild.voice_client
    await voice_client.disconnect()


@client.command(name='stop', help='This command stops the song!')
async def stop(ctx):
    server = ctx.message.guild
    voice_channel = server.voice_client

    voice_channel.stop()

【问题讨论】:

  • 请说出一个不起作用的具体事情并发送不起作用的事情的代码。
  • @yotamrec 播放命令不起作用,我听不到任何歌曲

标签: python discord.py


【解决方案1】:

这对我有用:

@bot.command()
async def play(ctx, url):
    voice_client = bot.voice_clients[0]
    
    ytdl = youtube_dl.YoutubeDL(YTDL_OPTS)
    info = ytdl.extract_info(url, download=False)

    asrc = discord.FFmpegOpusAudio(info['formats'][0]['url'], before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5")

    voice_client.play(asrc)

确保您拥有最新版本的 Youtube-dl 和其他软件包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-06
    • 2022-01-04
    • 2021-05-28
    • 2021-10-25
    • 2021-04-29
    • 2021-05-24
    • 2020-08-31
    • 2021-04-09
    相关资源
    最近更新 更多