0

I am developing a Discord bot with python that can play music. When I do the tests on my computer all work find but when I take all the files to put there in my server it stop working. So I tried to run it on an other computer and no, it still does not work.

First I was thinking it was a power problems but the other computer is almost the same. I tried to install all the packages that I have on my computer to the second but the music part still not work. It's not a Discord API problem because my others commands work find.

import discord
from discord.ext import commands
from youtube_dl import YoutubeDL


class music_cog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

        # all the music related stuff
        self.is_playing = False

        # 2d array containing [song, channel]
        self.music_queue = []
        self.YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}
        self.FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
                               'options': '-vn'}

        self.vc = ""

    # searching the item on youtube
    def search_yt(self, item):
        with YoutubeDL(self.YDL_OPTIONS) as ydl:
            try:
                info = ydl.extract_info("ytsearch:%s" % item, download=False)['entries'][0]
            except Exception:
                return False

        return {'source': info['formats'][0]['url'], 'title': info['title']}

    def play_next(self):
        if len(self.music_queue) > 0:
            self.is_playing = True

            # get the first url
            m_url = self.music_queue[0][0]['source']

            # remove the first element as you are currently playing it
            self.music_queue.pop(0)

            self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next())
        else:
            self.is_playing = False

    # infinite loop checking
    async def play_music(self):
        if len(self.music_queue) > 0:
            self.is_playing = True

            m_url = self.music_queue[0][0]['source']

            # try to connect to voice channel if you are not already connected

            if self.vc == "" or not self.vc.is_connected() or self.vc == None:
                self.vc = await self.music_queue[0][1].connect()
            else:
                await self.vc.move_to(self.music_queue[0][1])

            print(self.music_queue)
            # remove the first element as you are currently playing it
            self.music_queue.pop(0)

            self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next())
        else:
            self.is_playing = False

    @commands.command(name="play", help="Plays a selected song from youtube")
    async def p(self, ctx, *args):
        query = " ".join(args)

        voice_channel = discord.utils.get(ctx.guild.voice_channels, name='General')
        if voice_channel is None:
            # you need to be connected so that the bot knows where to go
            await ctx.send("Connect to a voice channel!")
        else:
            song = self.search_yt(query)
            if type(song) == type(True):
                await ctx.send(
                    "Could not download the song. Incorrect format try another keyword. This could be due to playlist or a livestream format.")
            else:
                await ctx.send("Song added to the queue")
                self.music_queue.append([song, voice_channel])

                if self.is_playing == False:
                    await self.play_music()

    @commands.command(name="queue", help="Displays the current songs in queue")
    async def q(self, ctx):
        retval = ""
        for i in range(0, len(self.music_queue)):
            retval += self.music_queue[i][0]['title'] + "\n"

        print(retval)
        if retval != "":
            await ctx.send(retval)
        else:
            await ctx.send("No music in queue")

    @commands.command(name="skip", help="Skips the current song being played")
    async def skip(self, ctx):
        if self.vc != "" and self.vc:
            self.vc.stop()
            # try to play next in the queue if it exists
            await self.play_music()

    @commands.command(name="disconnect", help="Disconnecting bot from VC")
    async def dc(self, ctx):
        await self.vc.disconnect()

    @commands.command(name="pause")
    async def pause(self, ctx):
        voice = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)
        if voice.is_playing():
            voice.pause()
        else:
            await ctx.send("Currently no audio is playing.")

    @commands.command(name="resume")
    async def resume(self, ctx):
        voice = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)
        if voice.is_paused():
            voice.resume()
        else:
            await ctx.send("The audio is not paused.")

There are not any error messages, the bot just joins the voice chat and does nothing.

If someone can tell me we it work just on my computer I will be very grateful.

My files: my files

Random Davis
  • 6,662
  • 4
  • 14
  • 24
Alex
  • 51
  • 4
  • 1
    I think you need to clarify a few things. Are there any error messages on the server? How do you know all the required libraries are installed on the server, into the same environment that the scripts are running in? Do you know that all the required libraries are supported by the server's operating system? – Random Davis May 11 '22 at 15:55
  • With server I mean an other little computer that use Windows. I just copy the files on that and do pip freeze > requirements.txt on my computer and after that I do pip install -r requirements.txt on the second. I don't get any errors messages, it just do nothing – Alex May 11 '22 at 16:24
  • My first computer also work with windows – Alex May 11 '22 at 16:25
  • You say "it does nothing"...how are you running the script? What happens when you run it on the server via a command prompt? Does it exit right away? Does it hang? Does anything get printed if you put a print statement at the beginning of the program? You're still leaving out a lot of critical details. – Random Davis May 11 '22 at 18:30
  • I run the script by doing python main.py all the script work I set print statement everywhere. So it recognises the command, the bot download the song, go the the voice channel and stop. It just jump over the part that play the song – Alex May 11 '22 at 19:10
  • And the script don't stop working, it's like the playing part just don't exist – Alex May 11 '22 at 19:12
  • self.vc.play(discord.FFmpegPCMAudio(m_url, **self.FFMPEG_OPTIONS), after=lambda e: self.play_next()) – Alex May 11 '22 at 19:14
  • It was this part that plays the song – Alex May 11 '22 at 19:15
  • According to [the official documentation](https://discordpy.readthedocs.io/en/stable/api.html#ffmpegpcmaudio): "You must have the `ffmpeg` or `avconv` executable in your path environment variable in order for this to work." – Random Davis May 11 '22 at 19:18
  • It's already done, ffmpeg.exe is in the environment variable and in the file with the scripts – Alex May 11 '22 at 19:44
  • All I can suggest, then, is adding a bunch of print statements so you know exactly what's going on; either that or run a debugger on the server and step through the code line-by-line like you'd normally do when debugging. – Random Davis May 11 '22 at 19:47
  • You can test if `ffmpeg` is added correctly to path environment variable by opening CMD and trying to run `ffmpeg`. – Benjin May 11 '22 at 20:08
  • What is the command and what it will do – Alex May 11 '22 at 20:09
  • I don't need to run my script line by line I already now the line that was not working and I already send it in comments – Alex May 11 '22 at 20:11

1 Answers1

0

I finally found the problem, in environment variable I set ffmpeg in general not in path. When I put ffmpeg in cmd It did nothing so I reinstalled ffmpeg in the good place and it is done all work, thk for all your help

Alex
  • 51
  • 4