import requests
from config import settings
import discord
from discord.utils import get
from youtube_dl import YoutubeDL
import os
from discord.ext import commands
bot = commands.Bot(command_prefix=settings['prefix'])
@bot.event
async def on_ready():
print("Start")
@bot.command()
async def load(ctx, extension):
extension = extension.lower()
bot.load_extension(f'cogs.{extension}')
await ctx.send(f'{extension} загружен')
@bot.command()
async def unload(ctx, extension):
extension = extension.lower()
bot.unload_extension(f'cogs.{extension}')
await ctx.send(f'{extension} загружен')
for filename in os.listdir("./cogs"):
if filename.endswith(".py") and not filename.startswith('_'):
bot.load_extension(f"cogs.{filename[:-3]}")
async def join(ctx, voice):
channel = ctx.author.voice.channel
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
def search(arg):
with YoutubeDL({'format': 'bestaudio', 'noplaylist':'True', 'ffmpeg_location': 'C:\\ffmpeg\\bin'}) as ydl:
try: requests.get(arg)
except: info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
else: info = ydl.extract_info(arg, download=False)
return (info, info['formats'][0]['url'])
@bot.command(pass_context=True)
async def play(ctx, *, query):
FFMPEG_OPTS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
video, source = search(query)
voice = get(bot.voice_clients, guild=ctx.guild)
channel = ctx.author.voice.channel
if voice and voice.is_connected:
await voice.move_to(channel)
else:
voice = await channel.connect()
await join(ctx, voice)
await ctx.send(f'Now playing {video["title"]}.')
voice.play(discord.FFmpegPCMAudio(source, **FFMPEG_OPTS), after=lambda e: print('done', e))
voice.is_playing()
bot.run(settings['token'])
Он скачивает само видео печатает название а потом выдает ошибку:
Ignoring exception in command play:
Traceback (most recent call last):
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/vasil/PycharmProjects/MusicBot/main.py", line 68, in play
voice.play(discord.FFmpegPCMAudio(source, **FFMPEG_OPTS), after=lambda e: print('done', e))
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\player.py", line 225, in __init__
super().__init__(source, executable=executable, args=args, **subprocess_kwargs)
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\player.py", line 138, in __init__
self._process = self._spawn_process(args, **kwargs)
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\player.py", line 147, in _spawn_process
raise ClientException(executable + ' was not found.') from None
discord.errors.ClientException: ffmpeg was not found.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\vasil\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: ffmpeg was not found.