discord.py queue music commands

Question:

I’m trying to fix my queue issue so it is guild strict, basically so the queue doesn’t show the same in all guilds.

queuee = []
songs = []


@client.hybrid_command(
    name="play",
    with_app_command=True,
    description="Play a song in a voice channel with a url or keyword",
)
@commands.is_owner()
async def play(ctx, *, query):
    await ctx.send(f"Searching for {query.capitalize()}..")
    voice_channel = ctx.author.voice.channel
    vc = await voice_channel.connect()
    loop = asyncio.get_event_loop()
    data = await loop.run_in_executor(
        None, lambda: ytdl.extract_info(query, download=False)
    )
    if "entries" in data:
        video_format = data["entries"][0]["formats"][0]
    elif "formats" in data:
        video_format = data["formats"][0]
    song = video_format["url"]
    player = discord.FFmpegPCMAudio(song, **ffmpeg_options)
    vc.play(player, after=do_after)
    queuee.append(song)
    songs.append(query.capitalize())

The queuee list is for audio urls and the songs list is for the queue command to display the song names in the queue but the two lists are global so if I added two songs to a queue in one server, It would show in another server. How can I fix that?

I’m not really sure what to do besides making a db collection for it and I just want to know if there is another way besides that first before I do it.

Asked By: TheRealKurumi

||

Answers:

a basic solution is to use a defaultdict (so that a list is automatically constructed if it doesn’t exist already in the dictionary) with the guild id as the key so like

from collections import defaultdict

queuee = defaultdict(list)
songs = defaultdict(list)


@client.hybrid_command(
    name="play",
    with_app_command=True,
    description="Play a song in a voice channel with a url or keyword",
)
@commands.is_owner()
async def play(ctx, *, query):
    await ctx.send(f"Searching for {query.capitalize()}..")
    voice_channel = ctx.author.voice.channel
    vc = await voice_channel.connect()
    loop = asyncio.get_event_loop()
    data = await loop.run_in_executor(
        None, lambda: ytdl.extract_info(query, download=False)
    )
    if "entries" in data:
        video_format = data["entries"][0]["formats"][0]
    elif "formats" in data:
        video_format = data["formats"][0]
    song = video_format["url"]
    player = discord.FFmpegPCMAudio(song, **ffmpeg_options)
    vc.play(player, after=do_after)
    queuee[ctx.guild.id].append(song)
    songs[ctx.guild.id].append(query.capitalize())

Answered By: YousefZ01
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.