Почему не работает код для воспроизведения музыки?
Есть такой код:
import discord
import os
from discord.ext import commands
import youtube_dl
bot = commands.Bot(command_prefix='!')
server, server_id, name_channel = None, None, None
domains = [
'https://www.youtube.com', 'http://www.youtube.com', 'https://youtube.be/',
'http://youtube.be/'
]
async def check_domains(link):
for x in domains:
if link.startswitch(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_channel,
name=name_channel)
params = command.split(' ')
if len(params) == 1:
file_name = params[0]
server = ctx.guild
name_channel = author.voice.channel.name
voice_channel = discord.utils.get(server.voice_channel, name=name_channel)
print('param 1')
elif len(params) == 3:
server_id = params[0]
voice_id = params[1]
file_name = 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_channel, id=voice_id)
else:
await ctx.channel(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 file_name == None:
pass
elif file_name.startswitch('http'):
if not check_domains(file_name):
await ctx.channel.send(
f'{author.mention}, ссылка не является разрешённой')
return
song_there = os.path.isfile('song.mp3')
try:
if song_there:
os.remove('song.mp3')
except PermissionError:
await ctx.channel.send('Недостаточно прав для удаления файла!')
return
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [
{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}
],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([file_name])
for file in os.listdir('./'):
if file.endswith('.mp3'):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio('song.mp3'))
else:
voice.play(discord.FFmpegPCMAudio(f'{file_name}'))
bot.run(os.getenv('TOKEN'))
Но когда я прописываю команду !play <ссылка>, то выдаёт такую ошибку:
Ignoring exception in command play:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 38, in play
voice_channel = discord.utils.get(server.voice_channel, name=name_channel)
AttributeError: 'Guild' object has no attribute 'voice_channel'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/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: AttributeError: 'Guild' object has no attribute 'voice_channel'
Где у меня ошибка?