Discord 音乐机器人的音频崩溃

时间:2021-02-24 20:15:21

标签: discord discord.py python-3.7

我正在尝试制作一个不和谐的音乐机器人,但由于某种原因,音频崩溃了。

代码如下:

# IMPORTING

import discord
from discord.ext import commands
from discord.utils import get
import asyncio
import youtube_dl
from youtube_dl import YoutubeDL
import urllib.request
import random
import re
import pafy


# VARIABLES
TOKEN = ""
BOT_PREFIX = "!"
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix=BOT_PREFIX, intents=intents)


@bot.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, *, args):

    voice = get(bot.voice_clients, guild=ctx.guild)
    if not voice or not voice.is_connected():
        await ctx.send("Don't think I am in a voice channel")
        return
    keywords = args.replace(" ", "+")
    html = urllib.request.urlopen("https://www.youtube.com/results?search_query="+keywords)
    video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
    url = ("https://www.youtube.com/watch?v=" + video_ids[0])

    ydl_opts = {'format': 'bestaudio'}
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)
        URL = info['formats'][0]['url']
    voice = get(bot.voice_clients, guild=ctx.guild)
    voice.play(discord.FFmpegPCMAudio(URL))

    video = pafy.new(url)
    if video.length >= 360:
        await ctx.send("The video you attempted to play was too long.")
    else:
        await ctx.send(f"Now playing {video.title} on Youtube.")


# RUNNING THE BOT
bot.run(TOKEN)

过了一会儿,它只是发送然后错误: [tls @ 0x7f9fc1521a40] 拉函数出错。 [matroska,webm @ 0x7f9fc1808200] 读取错误 [tls @ 0x7f9fc1521a40] 由于某种原因,指定的会话已失效。 最后一条消息重复了 1 次

如何在不下载歌曲的情况下解决此问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

C:\Users\Pop>pip install --user colorama
Traceback (most recent call last):
  File "c:\users\pop\appdata\local\programs\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\pop\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Pop\AppData\Local\Programs\Python\Python37\Scripts\pip.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'pip._internal.cli.main'