Hello I'm trying to create a music bot but I want it so I can play music and add additional music to a queue with the same command. I've tried to do this but I can't get it to work. Here's the code with play and queue being two separate commands:
@bot.command(pass_context=True)
async def join(ctx):
await bot.join_voice_channel(bot.get_channel('487315853482786820'))
@bot.command(pass_context=True)
async def leave(ctx):
voice_client = bot.voice_client_in(ctx.message.server)
await voice_client.disconnect()
players = {}
queues = {}
def check_queue(id):
if queues[id] != []:
player = queues[id].pop(0)
players[id] = player
player.start()
@bot.command(pass_context=True)
async def play(ctx, url):
server = ctx.message.server
voice_client = bot.voice_client_in(server)
player = await voice_client.create_ytdl_player(url, after=lambda: check_queue(server.id))
players[server.id] = player
player.start()
@bot.command(pass_context=True)
async def queue(ctx, url):
server = ctx.message.server
voice_client = bot.voice_client_in(server)
player = await voice_client.create_ytdl_player(url, after=lambda: check_queue(server.id))
if server.id in queues:
queues[server.id].append(player)
else:
queues[server.id] = [player]
await bot.say('Video queued.')
@bot.command(pass_context=True)
async def pause(ctx):
id = ctx.message.server.id
players[id].pause()
@bot.command(pass_context=True)
async def stop(ctx):
id = ctx.message.server.id
players[id].stop()
@bot.command(pass_context=True)
async def resume(ctx):
id = ctx.message.server.id
players[id].resume()
queue[len(self. queue)] = song. Then if someone uses the play 'song' command again the song is added to the queue and played once the first song is finished.
To play music on Discord, you must use a bot or play through connected platforms. If using a bot, connect your music bot, create a playlist on other platforms like Deezer and Soundcloud, then invoke the command to play a playlist using the link to your playlist. Discord will then play the queued songs on your playlist.
Launch Discord and join a voice channel. Type “!search” and enter the song or artist. The bot will list the results. Enter the number of the song and add it to your playlist.
How do you make a Discord bot wait for a response in Python? python discord bot wait for response # Use Client. wait_for to wait for on_message event.
You need some sort of queue system where song requests are stored in the queue and a loop that checks if any item is in the queue, grabbing the first item in the queue if it's available. If no songs are in the queue, then the loop waits until a song is added. You can use asyncio.Queue()
and asyncio.Event()
to do this.
import asyncio
from discord.ext import commands
client = commands.Bot(command_prefix='!')
songs = asyncio.Queue()
play_next_song = asyncio.Event()
@client.event
async def on_ready():
print('client ready')
async def audio_player_task():
while True:
play_next_song.clear()
current = await songs.get()
current.start()
await play_next_song.wait()
def toggle_next():
client.loop.call_soon_threadsafe(play_next_song.set)
@client.command(pass_context=True)
async def play(ctx, url):
if not client.is_voice_connected(ctx.message.server):
voice = await client.join_voice_channel(ctx.message.author.voice_channel)
else:
voice = client.voice_client_in(ctx.message.server)
player = await voice.create_ytdl_player(url, after=toggle_next)
await songs.put(player)
client.loop.create_task(audio_player_task())
client.run('token')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With