Home > Net >  How to implement the removal of an item from the list after a certain time?
How to implement the removal of an item from the list after a certain time?

Time:11-30

It is not possible to implement this "function".

This is the code itself, where it should be used:

teamList = []

@bot.command()

@commands.cooldown(1, 120, commands.BucketType.user)

async def createteam(ctx, *args):
    await ctx.channel.purge(limit = 1)
    author = ctx.message.author
    global teamList
    teamList.append(f"{author.mention} - "   " ".join(args))
    await ctx.send("Поиск создан!")
    time.sleep(1)
    await ctx.channel.purge(limit = 1)

I tried to use the "datetime" library in order to remove items from the list after the time has elapsed, but in the end nothing happened.

CodePudding user response:

I do something like this in my bot. You can try this:

teamlist.append(f"{author.mention} - "   " ".join(args))

await asyncio.sleep(120) #the parameter is in seconds

teamlist.remove(<person to remove>)

Make sure you import the asyncio library (import asyncio so you can use asyncio.sleep. I don't recommend using time.sleep unless you want to stop the entire program. If you don't want sleep for 2 minutes, you could do something like this:

i = 0
while i <= 120: #the amount of seconds to wait
 asyncio.sleep(1)
 i  = 1

then remove the member from the team list.

  • Related