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

preview_player
Показать описание
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 YouTube guidelines on streaming and downloading videos. Bots made using this content are not to be distributed, both non-commercially and commercially.

Code for this video:

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
Рекомендации по теме
Комментарии
Автор

Hi, thank you for these tutorials!
I'm having an issue with the queue, the bot plays the first song without a problem, but once anyone adds another song when one is already playing, I get this error:

Already playing audio.
discord.player ffmpeg process 7860 has not terminated. Waiting to terminate...
discord.player ffmpeg process 7860 should have terminated with a return code of 1.

MEGAFIL
Автор

Thank for vid. What about video how upload bot on hosting service? It will help a lot

fun
Автор

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 <module>
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\runners.py", line 44, in run
return
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?

fletline
Автор

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.

its_fog
Автор

It doesn't play for me. I get this error: ffmpeg was not found...

[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?

abaleaskg
Автор

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.

itsyeetz
Автор

very useful, thanks for the perfect tutorial.

龔柏叡
Автор

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?

Sleepmalice
Автор

Can someone explain to me why when I add a track to the queue, the playing music starts to freeze?

dehavo
Автор

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] 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.

programmer
Автор

#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