Answer the question
In order to leave comments, you need to log in
How to solve error when writing discord music bot on Discord.py?
import os
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import youtube_dl
bot = commands.Bot(command_prefix='!')
bot = discord.Client()
bot = Bot("!")
@bot.event
async def on_ready():
print(bot.user)
print(bot.user.id)
print([c.name for c in bot.get_all_channels()])
server, server_id, name_channel = None, None, None
domains = ['https://www.youtube.com/', 'http://www.youtube.com/', 'https://youtu.be/', 'http://youtu.be/']
async def check_domains(link):
for x in domains:
if link.startswith(x):
return True
return False
@bot.command()
async def play(ctx, *, command = None):
"""воспроизводит музикууу"""
global server, server_id, name_channel
author = ctx.author
if command == None:
server = ctx.guild
name_channel = author.voice.channel.name
voice_channel = discord.utils.get(server.voice_channels, name = name_channel)
params = command.split(' ')
if len(params) == 1:
sourse = params[0]
server = ctx.guild
name_channel = author.voice.channel.name
voice_channel = discord.utils.get(server.voice_channels, name = name_channel)
print("param 1")
elif len(params) == 3:
server_id = params[0]
voice_id = params[1]
sourse = params[2]
try:
server_id - int(server_id)
voice_id = int(voice_id)
except:
await ctx.channel.send(f'{author.mention}, id сервера или канала войс чата должно быть числом')
return
print('param 3')
server = bot.get_guild(server_id)
voice_channel = discord.utils.get(server.voice_channels, id=voice_id)
else:
await ctx.channel.send (f'{author.mention} Ты еблан?Сам прочитал что написал?')
return
voice = discord.utils.get(bot.voice_clients, guild = server)
if voice is None:
await voice_channel.connect()
voice = discord.utils.get(bot.voice_clients, guild=server)
if sourse == None:
pass
elif sourse.startswith('http'):
if not await check_domains(sourse):
await ctx.channel.send(f'{author.mention} Ссылка запрещенна.')
return
song_there = os.path.isfile('music/song.webm')
try:
if song_there:
os.remove('music/song.webm')
except PermissionError:
await ctx.channel.send (f'Системная ошибка (Недостаточно прав)')
return
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [
{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'webm',
'preferredquality': '192',
}
],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([sourse])
for file in os.listdir('music/'):
if file.endswith('.webm'):
os.rename(file, 'song.webm')
voice.play(discord.FFmpegPCMAudio('music/song.webm'))
else:
voice.play(discord.FFmpegPCMAudio('music/{sourse}'))
Answer the question
In order to leave comments, you need to log in
Did you set ffmpeg, fprobe to be available from PATH?
Have you read the README.md ?
Have you tried setting the ffmpeg_location parameter when initializing YoutubeDL?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question