@Aleksey_Vishnyakov

Почему бот не заходит в голосовой канал (discord.py)?

Не могу понять почему бот не заходит в голосовой канал.
import discord
from discord.ext import commands

from confing import token

import youtube_dl
import os

bot = commands.Bot(command_prefix='+')


# обработка событий
@bot.event
async def on_ready():
    print('Бот готов к работе')


server, server_id, name_channel = None, None, None

domains = ['https://www.youtube.com/', 'http://www.youtube.com/', 'https://you.be/', 'http://you.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_channels, name=name_channel)
    params = command.split(' ')
    if len(params) == 1:
        source = 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]
        source = 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 source is None:
        pass
    elif source.startswitch('http'):
        if not check_domains(source):
            await ctx.channel.send(f'{author.mention}, ссылка является не разрешенной')
            return

        song_there = os.path.isfile('music/song.mp3')
        try:
            if song_there:
                os.remove('song.mp3')
        except PermissionError:
            await ctx.channel.send('Нет прав для удаления!')
            return
        ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessons': [
                {
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',
                }

            ],
        }
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([source])
        for file in os.listdir('music/'):
            if file.endswith('mp3'):
                os.rename(file, 'song.mp3')
        voice.play(discord.FFmpegPCMAudio('song.mp3'))
    else:
        voice.play(discord.FFmpegPCMAudio(f'music/{source}'))


bot.run(token)
  • Вопрос задан
  • 100 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы