yt-dlp 或discord.ext 出错

问题描述 投票:0回答:1

这是我的代码


from discord.ext import commands
from discord.utils import get
from yt_dlp import YoutubeDL

bot = commands.Bot(command_prefix='!', intents= discord.Intents.all())
YDL_OPTIONS = {'format': 'worstaudio/best', 'noplaylist': 'False', 'simulate': 'True', 'key': 'FFmpegExtractAudio'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}

@bot.event
async def on_ready():
  print('connected')

@bot.command()
async def join(ctx):
  channel = ctx.message.author.voice.channel
  voice = get(bot.voice_clients, guild = ctx.guild)

  if voice and voice.is_connected():
    await voice.move_to(channel)
  else:
    voice = await channel.connect()
    await ctx.send(f'Бот присоеденился к каналу : {channel}')
@bot.command()
async def leave(ctx):
  voice = get(bot.voice_clients, guild=ctx.guild)

  if voice and voice.is_connected():
    await voice.disconnect()
    await ctx.send('Бот покинул канал.')
  else:
    await ctx.send('Бот не находится в голосовом канале.')

@bot.command()
async def play(ctx, url):
  with YoutubeDL(YDL_OPTIONS) as ydl:
    if 'https://' in url:
      info = ydl.extract_info(url, download=False)
    else:
      info = ydl.extract_info(f"ytsearch:{url}", download=False)['entries'][0]
  link = info['formats'][0]['url']

  vc.play(discord.FFmpegPCMAudio(executable="ffmpeg\\ffmpeg.exe", source=link, **FFMPEG_OPTIONS))


bot.run('TOKEN')

这是我的日志

[2024-02-08 07:21:02] [INFO    ] discord.client: logging in using static token
[2024-02-08 07:21:04] [INFO    ] discord.gateway: Shard ID None has connected to Gateway (Session ID: eeec958466d29561bb9437d3fd9873bd).
connected
[2024-02-08 07:21:07] [INFO    ] discord.voice_client: Connecting to voice...
[2024-02-08 07:21:07] [INFO    ] discord.voice_client: Starting voice handshake... (connection attempt 1)
[2024-02-08 07:21:08] [INFO    ] discord.voice_client: Voice handshake complete. Endpoint found frankfurt829.discord.media
[youtube:search] Extracting URL: ytsearch:sigma
[download] Downloading playlist: sigma
[youtube:search] query "sigma": Downloading web client config
[youtube:search] query "sigma" page 1: Downloading API JSON
[youtube:search] Playlist sigma: Downloading 1 items of 1
[download] Downloading item 1 of 1
[youtube] Extracting URL: https://www.youtube.com/shorts/-UAGvQhV-7A
[youtube] -UAGvQhV-7A: Downloading webpage
[youtube] -UAGvQhV-7A: Downloading ios player API JSON
[youtube] -UAGvQhV-7A: Downloading android player API JSON
[youtube] -UAGvQhV-7A: Downloading m3u8 information
[info] Testing format 233
[download] Finished downloading playlist: sigma
[2024-02-08 07:21:16] [ERROR   ] discord.ext.commands.bot: Ignoring exception in command play
Traceback (most recent call last):
  File "C:\Users\dog94\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\core.py", line 235, in wrapped
    ret = await coro(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Discord_Bots\Beta_Discord_Bot\main.py", line 44, in play
    vc.play(discord.FFmpegPCMAudio(executable="ffmpeg\\ffmpeg.exe", source=link, **FFMPEG_OPTIONS))
    ^^

我需要机器人从 youtube 视频中提取音频并在语音通道中播放它,机器人已连接到语音通道,我不明白为什么会出现该错误,而且我不明白为什么代码不理解“vc”,但它搜索了视频,我认为已下载,但它不读取“vc”,这是我的问题。

python discord.py
1个回答
0
投票

您的机器人需要知道它位于哪个语音通道才能播放音频,因为您的

!play
命令中未定义 vc。

vc = get(bot.voice_clients, guild = ctx.guild)

只需在 !play 命令中添加 !join 命令中的代码,看看它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.