No video

Create a Discord Music Bot with a Queue in Python [2024]

  Рет қаралды 4,776

Ethan | The Code Syndicate

Ethan | The Code Syndicate

Күн бұрын

Today we'll be making a Discord music player bot with a queue in Python using prefix commands!
*DISCLAIMER*
This video is for educational purposes ONLY. Anything produced by this video should respect KZfaq guidelines on streaming and downloading videos. Bots made using this content are not to be distributed, both non-commercially and commercially.
Old bot code: github.com/Mas...
Code for this video:
github.com/Mas...
Timeline:
00:00 Intro
00:18 Web portal settings
02:50 Get base code
03:24 Setting up prefix (slash) commands
05:54 Make queue command
07:54 Add play next song function to play()
09:43 After function of play()
10:08 Lambda function explained
11:24 Testing the queue
12:38 Add memory safe feature to stop()
13:08 Clear queue function
15:00 Test clear queue function
15:51 Outro

Пікірлер: 46
@_AI_GEN_
@_AI_GEN_ 4 ай бұрын
#skip voice_clients[msg.guild.id].stop() await play_next(ctx)
@ethancox2025
@ethancox2025 4 ай бұрын
Awesome addition! Thanks, dude
@Sleepmalice
@Sleepmalice 4 ай бұрын
i did it too :)
@yox9899
@yox9899 3 ай бұрын
@client.command(name="skip") async def skip(ctx): try: voice_clients[ctx.guild.id].stop() await play_next(ctx) await ctx.send("Skipped current track!") except Exception as e: print(e) Added a bit of code
@martincarranza114
@martincarranza114 3 ай бұрын
@@yox9899 This will cause a small bug that I ran into, doing this will prematurely trigger the 'after' parameter in the 'play' function making it so that 2 URL will be popped from the queues dictionary instead of just one. Try it yourself, if you add 3 URL to the queues it will only allow you to use the skip method one, if you add 4 you can only use it twice and so on. In order to fix this we will need to adjust the 'play_next" function by adding a flag to make sure it only triggers once, and not twice like it will do now. Code below! :) Print statement were added when I was debugging and how I saw it was being called twice so they can be deleted. async def play_next(ctx): if queues[ctx.guild.id] != [] and not getattr(play_next, 'running', False): play_next.running = True print(f"Queue length before skipping track: {len(queues[ctx.guild.id])}") link = queues[ctx.guild.id].pop(0) print(f"Queue length after skipping track: {len(queues[ctx.guild.id])}") await play(ctx, link=link) play_next.running = False
@DraukooYT
@DraukooYT 2 ай бұрын
Thanks my dude!
@user-wk4wm1ib4o
@user-wk4wm1ib4o 2 ай бұрын
very useful,thanks for the perfect tutorial.
@its_fog
@its_fog 3 ай бұрын
Too late to reply.. But greatt tutorial everything worked smoothly as per the need.. Future potential tutorial idea: searching for URL if name of the video is provided.
@allegrojacc
@allegrojacc 3 ай бұрын
ty 🖤
@1993abaleaskg
@1993abaleaskg Ай бұрын
It doesn't play for me. I get this error: ffmpeg was not found... [youtube] Extracting URL: kzfaq.info/get/bejne/quCWiceQ3decYJ8.html [youtube] tzfVd_xq80k: Downloading webpage [youtube] tzfVd_xq80k: Downloading ios player API JSON [youtube] tzfVd_xq80k: Downloading m3u8 information ffmpeg was not found. What could be wrong?
@m0str33t
@m0str33t 4 ай бұрын
Gonna try an make this tomorrow
@ethancox2025
@ethancox2025 4 ай бұрын
Sweet! How'd it go?
@m0str33t
@m0str33t 4 ай бұрын
@@ethancox2025 I have been tryna use replit to host the bot 24/7 in the cloud, I got it connect and log into the discord server but when i .play a yt video I keep getting ffmpeg not found and I’ve triple checked that I have ffmpeg.exe’s path correctly in env variables idrk other than it’s something with replit maybe
@m0str33t
@m0str33t 4 ай бұрын
@@ethancox2025 couldn’t get it to work the bike connects to my discord server, but when I try to play a song, it says FFMPeg not found I’ve copied the correct path in env varibles like 4 times. I’m using replit to try and host in cloud 24/7 but can’t figure it out.
@ethancox2025
@ethancox2025 4 ай бұрын
@@m0str33t does it work locally? You'll need to set the environment variables in Replit if you're hosting there
@Sleepmalice
@Sleepmalice 3 ай бұрын
#repeat @client.command(name = "repeat") async def repeat(ctx): global repeat_songs global current_song repeat_songs = abs(repeat_songs-1) if repeat_songs == 1: await queue(ctx,current_song) await ctx.send("repeated now") else: await ctx.send("don't repeated now") idk how to avoid in python keyword "global" for global vars in this context, but it works.var current_song is the current played song.In the play_next popping url adding to queue again and so on Also repeat_songs must be boolean type this my mistake/
@Sleepmalice
@Sleepmalice 4 ай бұрын
Thank you! queue for this case is really not so obvious as i thinked.If we want to repeat our queue for playing maybe realise linkedlist?
@ethancox2025
@ethancox2025 4 ай бұрын
Glad you liked it!
@fletline7326
@fletline7326 3 ай бұрын
Hi, there's a problem. I did everything as shown in the video, but at startup it gives the following error Traceback (most recent call last): File "c:\ds_bot\main.py", line 4, in bot.run_bot() File "c:\ds_bot\bot.py", line 107, in run_bot client.run(TOKEN) File "C:\Users\OLEG\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 860, in run asyncio.run(runner()) File "C:\Users\OLEG\AppData\Local\Programs\Python\Python39\lib\asyncio unners.py", line 44, in run return loop.run_until_complete(main) File "C:\Users\OLEG\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete return future.result() File "C:\Users\OLEG\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 849, in runner await self.start(token, reconnect=reconnect) File "C:\Users\OLEG\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 777, in start await self.login(token) File "C:\Users\OLEG\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 609, in login raise TypeError(f'expected token to be a str, received {token.__class__.__name__} instead') TypeError: expected token to be a str, received NoneType instead Could you help me figure this out?
@ethancox2025
@ethancox2025 3 ай бұрын
Looks like the token was never set. Did you set it in the .env file and specifically reference it as your TOKEN var with load_env?
@fletline7326
@fletline7326 3 ай бұрын
@@ethancox2025 well, I just installed all the libraries, put my bot's token in .env and ran the code. Do I need to change something else?
@fletline7326
@fletline7326 3 ай бұрын
@@ethancox2025 Hooray, I figured it out. I had problems with VS and support for .env files. I tried to run it on PyCharm and everything worked. Many thanks to you for the guide))))
@ethancox2025
@ethancox2025 3 ай бұрын
So glad you figured it out! VS Code can be finicky with .env lol
@itsyeetz304
@itsyeetz304 4 ай бұрын
Hi there! Quick question here, I'm trying to have my bot play webradio but to save on bandwith I disconnect the stream. is it normat that voiceclient.is_playing still returns true after running voice_client.stop() ? I tried to check this to prevent the 'is already playing audio' error on reconnecting when someone joins the channel.
@ethancox2025
@ethancox2025 4 ай бұрын
I think this is a known bug for FFmpeg. I'm not sure if it's a memory thread issue or the stream is holding on until the length of the audio given completes.
@fun3999
@fun3999 4 ай бұрын
Thank for vid. What about video how upload bot on hosting service? It will help a lot
@ethancox2025
@ethancox2025 4 ай бұрын
Future video 👀
@dehavo
@dehavo 2 ай бұрын
Can someone explain to me why when I add a track to the queue, the playing music starts to freeze?
@ethancox2025
@ethancox2025 2 ай бұрын
This could be low network connectivity
@dehavo
@dehavo 2 ай бұрын
@@ethancox2025 This happens exactly at the moment a new track is added to the queue. I tried everything, but nothing changes...😥
@jonathanjacob512
@jonathanjacob512 4 ай бұрын
does this support playing playlists?
@jonathanjacob512
@jonathanjacob512 4 ай бұрын
great video btw
@ethancox2025
@ethancox2025 4 ай бұрын
I actually have never tried that. Did it work for you?
@jonathanjacob512
@jonathanjacob512 3 ай бұрын
​@@ethancox2025 no it didnt, it seems to connect to voice channel and download all the songs in the list but doesnt play anything
@IN3ANEEDITZ
@IN3ANEEDITZ 11 күн бұрын
Hello 👋🏿 i need help
@ethancox2025
@ethancox2025 11 күн бұрын
@@IN3ANEEDITZ hello!
@eastwesser
@eastwesser 3 ай бұрын
git pull?
@ethancox2025
@ethancox2025 3 ай бұрын
Check description
@eastwesser
@eastwesser 3 ай бұрын
@@ethancox2025 thanks :D
@programmer2625
@programmer2625 4 ай бұрын
Hy there Ethan thanks for this informing video i'm having trouble on this and i try adding it as a cog rather than a async function it still runs but it's giving me the error of ffmpeg not found even i did add it to my enviroments variables : Already connected to a voice channel. [youtube] Extracting URL: kzfaq.info/get/bejne/pJ1ooJd_z87Vd5s.html&ab_channel=Ethan%7CTheCodeSyndicate [youtube] tS8nzAQg_8k: Downloading webpage [youtube] tS8nzAQg_8k: Downloading ios player API JSON [youtube] tS8nzAQg_8k: Downloading android player API JSON [youtube] tS8nzAQg_8k: Downloading m3u8 information ffmpeg was not found.
@ethancox2025
@ethancox2025 4 ай бұрын
You might need to restart your computer after making it an environment variable. I know that fixed this for me
@programmer2625
@programmer2625 4 ай бұрын
@@ethancox2025 it didn't work even tho it gave me the same error
@SpectrePVZ72
@SpectrePVZ72 12 күн бұрын
​@@programmer2625Try putting the address where the ffmpeg.exe is located, for example, in the PATH, delete the previous one and put like this: C:\ffmpeg\bin remember that this will depend on where you have extracted the ffmpeg folder
@hestbehist
@hestbehist 3 ай бұрын
ty Ethan
The Next Generation of Discord Music Bots
11:54
No Text To Speech
Рет қаралды 383 М.
20 Programming Projects That Will Make You A God At Coding
14:27
The Coding Sloth
Рет қаралды 880 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 43 МЛН
Kind Waiter's Gesture to Homeless Boy #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 7 МЛН
CHOCKY MILK.. 🤣 #shorts
00:20
Savage Vlogs
Рет қаралды 28 МЛН
When a CIA Hacker Goes Rogue
23:09
TyFrom99
Рет қаралды 1,9 МЛН
How Hackers Bypass Kernel Anti Cheat
19:38
Ryscu
Рет қаралды 654 М.
Create a Discord Music Bot [Python]
16:26
Ethan | The Code Syndicate
Рет қаралды 10 М.
I forced EVERYONE to use Linux
22:59
NetworkChuck
Рет қаралды 368 М.
I tried Discord Bot Makers...
14:19
FaceDev
Рет қаралды 117 М.
Making a Game in Python with No Experience
5:57
Goodgis
Рет қаралды 1,6 МЛН
I Tried To Beat Minecraft Backwards
18:53
Contraption8or
Рет қаралды 1,2 МЛН
I made my own Web
15:43
FaceDev
Рет қаралды 499 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 43 МЛН