1
votes

Here's my current Play command, I know there is an issue with it checking if its in the right voice channel, but I can probably figure that out eventually, what I'm worried about is that I'm pretty sure this code won't work if people are trying to play music at the same time in different servers.

@bot.command(name='play', help='This command plays songs')
async def play(ctx, *args):
    searchterm = " ".join(args)
    print(searchterm)
    global queue

    if not ctx.message.author.voice:
        await ctx.send("You are not connected to a voice channel")
        return
    else:
        channel = ctx.message.author.voice.channel
        botchannel = ctx.bot.voice_clients
        print(channel)
        print(botchannel)
        await channel.connect()


    if 'youtube.com' in searchterm:

        queue.append(searchterm)

        server = ctx.message.guild
        voice_channel = server.voice_client

        async with ctx.typing():
            player = await YTDLSource.from_url(queue[0], loop=bot.loop)
            voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

        await ctx.send('**Now playing:** {}'.format(player.title))
        del(queue[0])

    elif 'youtube.com' not in searchterm:

        results = SearchVideos(searchterm, offset=1, mode = "dict", max_results=1).result()   
        resultlist = results['search_result']
        resultdict = resultlist[0]
        url = resultdict['link']
        queue.append(url)

        server = ctx.message.guild
        voice_channel = server.voice_client

        async with ctx.typing():
            player = await YTDLSource.from_url(queue[0], loop=bot.loop)
            voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

        await ctx.send('**Now playing:** {}'.format(player.title))
        del(queue[0])
1
Well have you tried playing in multiple servers at once to see if it works? - unex

1 Answers

2
votes

Your code will work on multiple servers but you'll only a unique queue (since queue is a list). You can create a dict with server ids as keys and queue as value:

queue = {}
@bot.command(name='play', help='This command plays songs')
async def play(ctx, *, searchterm):
    global queue
    (...)
    if 'youtube.com' in searchterm:
        try:
            queue[ctx.guild.id].append(searchterm)
        except:
            queue[ctx.guild.id] = [searchterm]

    elif 'youtube.com' not in searchterm:
        results = SearchVideos(searchterm, offset=1, mode = "dict", max_results=1).result()   
        url = results['search_result'][0]['link']
        try:
            queue[ctx.guild.id].append(url)
        except:
            queue[ctx.guild.id] = [url]

    server = ctx.message.guild
    voice_channel = server.voice_client

    async with ctx.typing():
        player = await YTDLSource.from_url(queue[ctx.guild.id][0], loop=bot.loop)
        voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

    await ctx.send(f'**Now playing:** {player.title}')
    del(queue[ctx.guild.id][0])