How To Code a Discord Music Bot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so the first thing we need to do is go over to the Discord developer website and then we need to create a new application I'm going to call mine musicbot and then we need to go over to bots click add bot and then turn off public cuz we don't want other people to use it turn on presence check and server member intent go over to O off click Bots I'm going to give mine admin and then copy and then add this to your server we also want to go over and copy our token so that we can use this later all right so today we're going to use reflet again just because it's really easy to use man they come up with such weird names every time oh by the way did I mention I named the dog I named it Cactus because it's green and you can hug it yeah yeah okay as you can see this time we have dark theme because someone told me that there is a dark theme for repet and I am so sorry for the last video to all the people who hate light theme I am so sorry so the first thing we need to do is import Discord in the main.py so import Discord and from discord. EXT import commands and then we want to initiate our bot so client equals commands dobot command prefix equals question mark I always use question mark discord. intent. all client. run and in here you need to put the token I asked you to copy just now and then we need to add a new file I'm going to call mine music. py and in music. py we need to import Discord again so import Discord and from discord. ex import commands and finally we also need YouTube DL So What YouTube DL is it handles anything YouTube related so the YouTube video information and downloading the audio from the YouTube videos so now we need to create a class called music and put commands. Cog and then put a def in it and then self. client equals client and then we need to add this as a cog So Def setup client client. add Cog music client all right let's head over to the main.py and then import this as a cog so for music and then cogs equals and in this list you need to put any cogs you have and then for I in range length cogs cogs ey. setup and that's all we need to do in main.py let's head back over to music the first thing we need to do is create a join command so that the bot will join the voice channel so at commands. command async def join self CTX now if CTX do author. voice is none which means if the person is not in a voice Channel then await CTX do send well you're going to tell them something like you're not in a voice Channel and then we also want a variable declaring The Voice channel so voice Channel CTX do author. voice. Channel then if CTX dovo clients is none so if the bot is not in a voice Channel then await voice channel. connect else which means it is in a voice Channel and it needs to move over await CTX dovo client. move to voice Channel now since we've got to join we'll probably want a disconnect so just copy and paste the top part and and change it to disconnect and then we can put await CTX dovo clients. disconnect and that's it for disconnecting now on to the fun part the part that takes eons to write in most music bot tutorials well I'm going to try and do it in less than 10 lines let's put it at commands. command async def play self CTX and URL CTX dovo clients. stop so if it's playing a song before then it's going to stop the song and play this new song that you put on then we're going to need to put f fmeg options what ffmpeg does is it handles the streaming in Discord this written by the official Discord API and then we need to put these these preferences these are pretty standard for streaming we also need a YouTube DL options so ydl options equals this is just so that it's in the best audio format so format and then best audio and then we need to declare our voice chat so VC equals CTX dovo client and now we're ready to stream the audio with YouTube dl. youube DL ydl options as ydl info equals ydl do extract info URL and download should be false so download equals false and then url url 2 equals info format Z and URL and then Source equals await discord. ffmpeg Opus audio. from Probe url2 and Then star FFM Peg options and finally we need to play the audio so vc. playay source so what this part does is it it creates a stream to play the audio and then we're streaming it directly into the VC so the last few things we want is something to pause the music and resume it if you've paused it so we can copy this change it to pause and then change disconnect to pause and you'll probably want to send some kind of message so await CTX do send paused I think I want to put a sticker and then we want one for resume so copy and paste and change pause to resume same here that didn't come out very nice all right there we go that's that's all we needed for streaming audio all right let's hope it runs an error there's an error let's see oh whoops I put this before this needs to go on top oh in the setups we need to put client or runtime error P NL okay we need to go install pineapple pineapple it's done all right let's play Let's Play Let's play some music all right downloading the web page [Music] all right that's enough that's enough pause pause pause what can I say all right resume disconnect that's how you make a music bot thank you for watching I hope this helped you and obviously if you have any problems let me know in the comments down below if not I'll see you next time oh my
Info
Channel: Max Codez
Views: 227,553
Rating: undefined out of 5
Keywords: discord, python, rythm, music, bot
Id: jHZlvRr9KxM
Channel Id: undefined
Length: 6min 10sec (370 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.