0

I'm trying to implement a command with discord.py that allows users to enter a queue. Once the queue hits 6 total users, it closes. Right now I'm just testing how to have the queue increment. Currently the queue returns back to the default count = 0, if count != 6. However, I'm not sure how to run the queue command again without it running through the entire function. Basically, once a user starts the queue command I need it to save their spot in the queue while also allowing more users to enter. All while avoiding returning to the beginning of the function.

I know it seems simple but I can't wrap my head around how to do it. I tried converting the members who queued into an integer to compare the total value of members queued with the 6 user limit, but can't parse "message.author" to integer.

@client.command()
async def queue(ctx):
  count = 0
  while count <= 6:
    await ctx.send('Added to the queue!' f'{ctx.author.mention}')
    count += 1
    #member = ctx.author.mention
    while count != 6:
      return
    else:
      await ctx.send('Queue full')

Thanks for the help.

2 Answers2

0

You can simply have a variable outside of your function and increment it inside the function as follows

count = 0
@client.command()
async def queue(ctx):
  global count
  if count < 6:  # Smaller than 6 so that only 6 people can enter
    await ctx.send('Added to the queue!' f'{ctx.author.mention}')
    count += 1
    #member = ctx.author.mention
  else:
    await ctx.send('Queue full')

You could also look into discord's api wait_for that can be useful if your program should "wait" for certain event to happen after a command is triggered

Nathan Marotte
  • 771
  • 1
  • 5
  • 15
0

calling count = 0 in the beginning will reset your queue to 0 every time the command is called

What you want to do is save the queue state in memory or on disk. The dirty way to solve it in this scenario is with a global variable, but you generally want to avoid those. here's why

qcount = 0

@client.command()
async def queue(ctx):
    global qcount
    if qcount <= 6:
        qcount += 1
        await ctx.send('Added to the queue!' f'{ctx.author.mention}')
    else:
        await ctx.send('Queue full')

What you really want to do is pack the bot into a class (Cog) and start the queue in its init:

class Botname(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.qcount = 0
    @commands.command()
    async def queue(self, ctx):
        if self.qcount <= 6:
            self.qcount += 1
            await ctx.send('Added to the queue!' f'{ctx.author.mention}')
        else:
            await ctx.send('Queue full')
        return

if __name__ == '__main__':
    client.add_cog(Botname(client))
    client.run(TOKEN)

or alternatively you can use a SQL database to store the queue value

TheV
  • 63
  • 7