【问题标题】:Can't get Discord.Py bot to play more then one song无法让 Discord.Py 机器人播放超过一首歌曲
【发布时间】:2021-09-18 12:00:18
【问题描述】:

到目前为止,我让我的机器人播放一首歌曲,但只有当它第一次连接到语音通道时。当我在执行停止命令后尝试放入另一首歌曲时,我得到了这个

discord.ext.commands.errors.CommandInvokeError:命令引发异常:ClientException:已连接到语音通道。

我的意思是,我知道它在语音频道中,但为什么它不能播放超过一首歌曲?

    import discord
from discord.ext import commands
import youtube_dl
import os
import asyncio

client = commands.Bot(command_prefix="^")
songs = asyncio.Queue()
play_next_song = asyncio.Event()

@client.command()
async def play(ctx, url: str):
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except PermissionError:
        await ctx.send("Wait for the current playing music to end or use the 'stop' command")
        return

    voiceChannel = ctx.message.author.voice.channel
    await voiceChannel.connect()
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
    voice.play(discord.FFmpegPCMAudio("song.mp3"))

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
    voice.play(discord.FFmpegPCMAudio("song.mp3"))

    while voice.is_playing():  # Makes other commands no longer work
        print("playing...")
        time.sleep(5)

    else:
        song_queue.remove(song)
        song_there = os.path.isfile("song.mp3")
        if song_there:
            os.remove("song.mp3")



@client.command()
async def leave(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice.is_connected():
        await voice.disconnect()
    else:
        await ctx.send("The bot is not connected to a voice channel.")


@client.command()
async def pause(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice.is_playing():
        voice.pause()
    else:
        await ctx.send("Currently no audio is playing.")


@client.command()
async def resume(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if voice.is_paused():
        voice.resume()
    else:
        await ctx.send("The audio is not paused.")


@client.command()
async def stop(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    voice.stop()





client.run('')

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    获取语音客户端后,检查是否连接。

    voiceChannel = ctx.message.author.voice.channel
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    # If bot is currently connected to voice channel
    if voice is not None:
        # If bot is connected to different voice channel than author
        if voice.channel != voiceChannel:
            await voice.move_to(voiceChannel)
         # If move_to doesn't work try below:
            #await voice.disconnect()
            #await voiceChannel.connect()
    else:
        await voiceChannel.connect()
    

    您可能需要检查 ctx.author.voice 是否为 None。当作者未连接到任何语音通道时,当前代码会出现异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多