Create a Discord Music Bot [Python]

2024 ж. 23 Мам.
6 223 Рет қаралды

Today we'll be making a Discord music bot that plays audio from KZhead videos! It's a long ride to get this going, so be sure to grab a coffee and strap in for this video.
FFmpeg download: www.gyan.dev/ffmpeg/builds/
GitHub files: github.com/MasterReach1/disco...
Timeline:
00:00 Building the bot on Discord
01:46 Setting up for the code
02:05 Downloading FFmpeg
02:30 Setting FFmpeg path as environment variable
03:39 Install required packages
04:50 Set up run_bot()
04:54 Load .env variables
05:12 Discussing .env
06:45 Setting up variables
08:44 Setting up on_ready()
09:00 Setting up on_message()
09:15 Setting up ?play command
13:12 First error
13:42 First successful test
14:15 Adding ?pause command and others
15:30 Second successful test
15:55 Outro

Пікірлер
  • If you are running into the issue: "ffmpeg process [x] successfully terminated with return code of 0", then watch this video for the fix: kzhead.info/sun/abOrcqeZjamgeq8/bejne.html

    @ethancox2025@ethancox20252 ай бұрын
    • Subscribed to you👍

      @Sleepmalice@SleepmaliceАй бұрын
  • Man, you really saved me with this tutorial! You explain everything very well. Apparently, the only error I had was that I needed to install "pip install PyNaCl". Other than that, everything worked perfectly. Thank you for the video! I'm Brazilian and I loved your video. I hope you have more success!

    @cauaribeiro2746@cauaribeiro27462 ай бұрын
    • I'm so glad you found it helpful! Thanks for letting me know about the extra streaming package requirement; anyone who faces the same issue will see this comment. Much love, dude

      @ethancox2025@ethancox20252 ай бұрын
  • great tutorial! thanks for giving me the foundation of my own bot. Also you are a pretty funny guy lol made the video even better with your personality. keep it up!

    @ryanm03@ryanm03Ай бұрын
    • Much appreciated friend

      @ethancox2025@ethancox202520 күн бұрын
  • Nice video, man. it would be nice if you make a video implementing a music queue.

    @alke5789@alke5789Ай бұрын
    • Yessir! I've had this request so much, I'm dropping the video on that this week. Be sure to check back or just turn notifications on!

      @ethancox2025@ethancox2025Ай бұрын
  • Nice Video 10/10

    @gilwicho3734@gilwicho37342 ай бұрын
    • Glad you found it useful!

      @ethancox2025@ethancox20252 ай бұрын
  • Thank you so much! I was having some trouble with some stuff and you helped a lot! Ah, I wanted to add some slash commands. But it was pretty hard to sync with discord. Do you perhaps have a video about it? Anyway, thanks a lot!

    @Phoenix72950@Phoenix72950Ай бұрын
    • I have a video on making a slash command bot out now, but this week I'm dropping a music bot tutorial that uses prefix commands and has a queue!

      @ethancox2025@ethancox2025Ай бұрын
    • @@ethancox2025 Aye! Okay.

      @Phoenix72950@Phoenix72950Ай бұрын
  • Excellent stuff. I ran into 2 errors, which I can see others hitting as well. 1) My path environment didn't want to update until I restarted my computer. I'm on Windows. 2) I needed to install the library pynacl, as my bot couldn't join the voice channel and ffmepg was stalling out.

    @aaroncraig-kg5wx@aaroncraig-kg5wx2 ай бұрын
    • Thank you for pointing these out and listing them here! I'm glad you found the video valuable

      @ethancox2025@ethancox20252 ай бұрын
  • you are great

    @xxmoodzz@xxmoodzz8 күн бұрын
    • Thank you friend ❤️

      @ethancox2025@ethancox20258 күн бұрын
  • This is one of the two bots I have been trying to research making. I saw you are going to make an updated version of this with a queue and slash command so I look forward to that, is there anyway you can make a video about a chatgpt bot? Every video I find in python is outdated and doesn't work. Thanks!

    @zacharyRL@zacharyRLАй бұрын
    • This is one of my earlier videos using ChatGPT. I also have one building a dialogue bot with ChatGPT and Discord! kzhead.info/sun/pcN9ns2KkGubl5E/bejne.htmlsi=JDtADbpDSPzoKA9t

      @ethancox2025@ethancox2025Ай бұрын
  • thank you so much bro! I was struggling to set one up. tell me something, can I join other functions like messages to the bot or not?

    @BEZERKER45@BEZERKER45Ай бұрын
    • Absolutely! You can add whatever functions you want, it's a standard Discord bot

      @ethancox2025@ethancox202520 күн бұрын
  • Im having a problem with yt_dlp, the import command isn't recognizing it. The error is: "reportMissingImports" And I already downloaded yt_dlp but it still doesn't work

    @jguivianna-4982@jguivianna-4982Ай бұрын
  • LoginFailure: Improper token has been passed.

    @Ksavzx@Ksavzx6 күн бұрын
  • Hi, thanks but I have this error : AttributeError: 'FFmpegOpusAudio' object has no attribute '_process'

    @SaumonArcEnCiel@SaumonArcEnCielАй бұрын
  • Thank you so much on the tutorial! I successfully created my bot. However, I want this bot to be able to add a song in the queue when I use ?play twice or more, and it will automatically play the next song. Can you advice me on how to do this? Even better if you make a tutorial about that.

    @izumi6502@izumi6502Ай бұрын
    • I'm glad you found this helpful! Also, yes I will be making a tutorial on a modern music bot using slash commands, and the queue will be included there.

      @ethancox2025@ethancox2025Ай бұрын
  • Thanks for this, was able to resurrect my long dead music bot after youtube_dl ceased being maintained. However I noticed when testing this the TLS session could be come dead "for some reason" according to error logs...is there a way to fully download the audio source before it plays to avoid this issue? I'm doing research on my own just not sure if you ran into this or not.

    @CarterCovers55@CarterCovers5520 күн бұрын
    • I'm actually looking to into caching the song as it plays + the queue as the network allows. Kind of like how a standard music streaming app works. It'll be a future video!

      @ethancox2025@ethancox202520 күн бұрын
    • @@ethancox2025 cool, can't wait!

      @CarterCovers55@CarterCovers5520 күн бұрын
  • How can i add a skip button? For example i have two songs in the queue and i want to skip the first one

    @nickname7966@nickname796621 күн бұрын
  • would you mind adding on a section where you can add songs to a queue and loop a specific song? if making a video is too difficult or time consuming, i would appreciate the sections of code alone in the comments and i can add them to my files myself, thanks!

    @s7ksu@s7ksuАй бұрын
    • No worries man! I've got a video on queues dropping this week. Just had a tight schedule lately and finally have time again for videos.

      @ethancox2025@ethancox2025Ай бұрын
    • @@ethancox2025 thats great to hear! thanks for the heads up, is there a date i should be looking forward to or just this week in general?

      @s7ksu@s7ksuАй бұрын
  • Hello, thank u so much for this tutorial but i have a problem : My bot doesn’t come in the voice channel, do you know why ? ( btw there is no error message in the terminal)

    @Stationnn@StationnnАй бұрын
    • Make sure you're in the voice channel that you want him in. Also make sure to give him Guild and Message intents in the Developer console on the web!

      @ethancox2025@ethancox2025Ай бұрын
  • Hey awsome video, I'm getting an issue where whenever I play I get the issue in the terminal "ffmpeg was not found"

    @WWickenden@WWickendenАй бұрын
    • Nvm I fixed it. I just had to put ffmpeg.exe into the bot folder. All the best :)

      @WWickenden@WWickendenАй бұрын
  • Good video. I have an issue that everytime i try to play a youtube video i get the error "ERROR: [youtube] hhfzhvurx7k: Video unavailable". Is there a fix for that?

    @felipedemarchi8927@felipedemarchi8927Ай бұрын
    • Some videos might have restrictions on using a third party to play the video. This video is simply for entertainment and educational purposes, so you'll need to just use discretion in selecting a video

      @ethancox2025@ethancox2025Ай бұрын
  • ffmpeg process 9732 should have terminated with a return code of 1. any fix ?

    @Veteran_47@Veteran_477 күн бұрын
    • Punned comment has a link to a video on fixing that

      @ethancox2025@ethancox20257 күн бұрын
  • Bro can u please create a tutorial on a music bot which searches music from the given text on spotify and other platforms. (Please create the text one so that people can use the music bot easily please bro)

    @Creative-Destruction@Creative-Destruction2 ай бұрын
    • Spotify isn't a free option, but I just dropped a video on searching music via keyword on KZhead. Check it out if you want!

      @ethancox2025@ethancox202520 күн бұрын
  • Hello friend, sorry, I'm new to programming. I have a problem: I can't get my bot online. What can I do?

    @jefreeyrendon9315@jefreeyrendon9315Ай бұрын
    • You need to run the bot from VS Code before it will come online

      @ethancox2025@ethancox2025Ай бұрын
    • @@ethancox2025 I followed the tutorial to the letter, but simply when I run the code in vscode the bot does not start, nor do I know where you put the bot token Excuse me for the trouble

      @jefreeyrendon9315@jefreeyrendon9315Ай бұрын
  • it says expected token is to be in str

    @Veteran_47@Veteran_477 күн бұрын
  • I'm having trouble with the code, especially with ffmpeg. Everything else is working fine, but when I try to play a song, it keeps saying "ffmpeg not found."

    @apotato2855@apotato28552 ай бұрын
    • Once ffmpeg has been installed and added to your system path variable, you might need to reboot your computer in order for the changes to take effect. Let me know if that helps!

      @ethancox2025@ethancox20252 ай бұрын
    • @@ethancox2025 Thanks, u have helped me a lot with your video!!

      @apotato2855@apotato28552 ай бұрын
    • @@ethancox2025 Do you know why the bot stops playing after a certain amount of time? it doesnt play the whole song.

      @apotato2855@apotato28552 ай бұрын
    • @@apotato2855 Does it say "ffmpeg process x successfully terminated with a return code of 0"? If so, do this: kzhead.info/sun/abOrcqeZjamgeq8/bejne.html

      @ethancox2025@ethancox20252 ай бұрын
  • Hi, thanks for the tutorial, but there is a question every time, regardless of the device, ffmpeg returns error code 1 to me, everything is installed and global variables are set, I don't know what to do, don't you know how to fix it? I can't find a solution to the problem on stackoverflow and github For example: 2024-05-08 09:50:20 INFO discord.player ffmpeg process 3544 has not terminated. Waiting to terminate... 2024-05-08 09:50:20 INFO discord.player ffmpeg process 3544 should have terminated with a return code of 1.

    @flashbang5401@flashbang540115 күн бұрын
  • Thank you so mush for this tutorial! I have issue that bot don't get messages from channel only if send direct message

    @Sleepmalice@SleepmaliceАй бұрын
    • Be sure you add the Message Intent in the Developer Web Console

      @ethancox2025@ethancox2025Ай бұрын
    • @@ethancox2025 thank you! but now i have another issue - when the message event triggered in the message object has no attribute autor but in debug mode this exists.So strange upd: sorry this my mistake autor instead author

      @Sleepmalice@SleepmaliceАй бұрын
    • @@ethancox2025 yes! it works.Also I imported nacl library

      @Sleepmalice@SleepmaliceАй бұрын
  • Hello guys. Thank you for a great tutorial, but I'm facing a problem with ffmpeg not terminating ffmpeg process 18188 has not terminated. Waiting to terminate... ffmpeg process 18188 should have terminated with a return code of 1. Tried different things, don't know what to do

    @smirm6034@smirm603424 күн бұрын
    • Have you found a solution?

      @fletline7326@fletline732622 күн бұрын
    • any solutions ?

      @Veteran_47@Veteran_477 күн бұрын
  • Im having this weird problem (not an error exactly). So whenever I play a music by bot loves to stops it in the middle and gives an error like: ffmpeg process 13268 successfully terminated with a return code of 0. Can anyone point out the reason?

    @its_fog@its_fog2 ай бұрын
    • Apologies for the delayed response, friend. Here's a short video on fixing this: kzhead.info/sun/abOrcqeZjamgeq8/bejne.html

      @ethancox2025@ethancox20252 ай бұрын
  • In this piece of code you can see that I don't have any errors, but my bot still joins the voice channel but I don't hear it, what could it be? @client.event async def on_message(msg): if msg.content.startswith("?play"): try: voice_client = await msg.author.voice.channel.connect() voice_clients[voice_clients.guild.id] = voice_client except Exception as e: print("error")

    @JhonDaid_JD@JhonDaid_JD22 күн бұрын
  • Im having this issue where the bot randomly stops playing. Do you know how i fix this? "discord.player ffmpeg process 3000 successfully terminated with return code of 0." (the number changes every time)

    @MaxOppendijk@MaxOppendijk2 ай бұрын
    • I have made a short video addressing this issue since it's pretty common for all who have completed this tutorial. Hope it helps! kzhead.info/sun/abOrcqeZjamgeq8/bejne.html

      @ethancox2025@ethancox20252 ай бұрын
KZhead