N
N
nymb322021-10-23 23:03:12
Python
nymb32, 2021-10-23 23:03:12

Discord bot won't start playing the track, what's wrong?

the music bot for discord does not start playing the track, but simply writes an error in the chat of the 61746b8573f90993402346.pngvideo from YouTube, it downloads 61746c03d20e5909501516.pngbut does not start playing
a piece of code:

@bot.command(name='go', help='Выбор видео с ютуба, звуковая дорожка которого будет воспроизводится.')
async def go(ctx, url):
    try:
        server = ctx.message.guild
        voice_channel = server.voice_client

        async with ctx.typing():
            filename = await YTDLSource.from_url(url, loop=bot.loop)
            voice_channel.play(discord.FFmpegPCMAudio(executable='ffmpeg.exe', source=filename))
        await ctx.send('СЕЙЧАС ИГРАЕТ: {}'.format(filename))
    except:
        await ctx.send("Не удалось выполнить команду, обратитесь к разработчику")


ytdlsource:
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)

class YTDLSource(discord.PCMVolumeTransformer):
    def __init__(self, source, *, data, volume=0.5):
        super().__init__(source, volume)
        self.data = data
        self.title = data.get('title')
        self.url = ""
    @classmethod
    async def from_url(cls, url, *, loop=None, stream=False):
        loop = loop or asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
        if 'entries' in data:
            data = data['entries'][0]
        filename = data['title'] if stream else ytdl.prepare_filename(data)
        return filename

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nymb32, 2021-10-31
@nymb32

Actually, the error was not in the bot itself, as I thought, but here:

async with ctx.typing():
            filename = await YTDLSource.from_url(url, loop=bot.loop)
            voice_channel.play(discord.FFmpegPCMAudio(executable='ffmpeg.exe', source=filename))
                                                                                                          ^^^^^^^^^

Specifically, I did not add ffmpeg to PATH, there are 2 solutions: 1- specify the path in PATH, 2- specify the full path to ffmpeg.exe in the code
(python-ffmpeg is just a library that allows you to work with ffmpeg without leaving the interpreter and not completely replaces ffmpeg, for the library to work in any case, you need to download ffmpeg on your PC)

V
Vindicar, 2021-10-24
@Vindicar

The telepaths are in quarantine, so you'll have to find the error message yourself.
For example, using the traceback module. You import it, and then in except you write something like

except:
        await ctx.send("Не удалось выполнить команду:\n" + traceback.format_exc())

In general, except, which does not even write an error message to the log or somewhere else, is worse than useless. No way to understand what happened and where it happened.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question