How do I add delay onto await()

how do i add delay onto await()
so say for example i wanted to send a message

would it be await(2) ?? idk

2 Likes

Welcome to Ask!
Can you please provide a link to your repl so that we can help you easier?

3 Likes

I’m guessing that you need to do a delay. Here’s how

const delay = ms => new Promise(resolve => setTimeout(resolve, ms * 1000));

Usage:

async function myFunction() {
  await delay(seconds);
}
2 Likes

Try multiplying seconds by 1000 (setTimeout expects values in milliseconds, and there are 1000 milliseconds in every second)

3 Likes

Sorry, forgot to add that. Added ms * 1000.

1 Like
raise ConnectionRefusedError("All of your repls are discord self-bots or raiders, and many include slurs and insults. Please delete them.")
1 Like

Python solution here:

To add a delay on async functions, use

import asyncio

Then in your code where you want delay:

await asyncio.sleep(5) #5 seconds
3 Likes

I thought it was JS, but someone changed the cat to py.

yep, because all their repls are python discord bots
…and all of them are malicious (the oldest one which hasn’t been modified from its template doesn’t count), and we know that OP is still planning to make a sniper so I thought I’d hide the solution until we know they’re using it for something ethical, but :face_with_diagonal_mouth:

2 Likes

To do a delay, there’s this add on called time. To do the wait, try this.

import time
time.sleep([seconds])

If you don’t want to type time.sleep, and want something else, then you can type this:

from time import sleep as wait
wait([seconds])

If you do not want to use wait, then simply change “wait” to whatever you want from where I say as wait

1 Like

Example import time

wait.(2)

Hey @zededgacool welcome to the forums!

Can you please share a link to the repl?

6 Likes
import time

time.sleep(1) # seconds

If this worked, please mark this post as the solution.

8 Likes

Do note that the time is in seconds. It supports floats too. If you mark a solution, use the one by @OmegaOrbitals. I’m just elaborating on it.

2 Likes

actually @OmegaOrbitals is a bit wrong ;-; use

import asyncio

async def sleep(x: float) -> None:
    await asyncio.sleep(x)

asyncio.get_event_loop().run_until_complete(sleep(5)) # if outside of an async func
# otherwise
async def other() -> None:
    await sleep(5)

Lemme explain

Basically someone merged How do I add delay onto await() - #12 by zededgacool into this thread but Omega replied to that before it was merged so he didn’t know it was about async await stuff.

4 Likes

oh ok neverminda orz ian he made the right descision

Apologies for the confusion. I did merge the topics as it is essentially the same question. Thanks for all your help!

3 Likes

Partly correct, you first need to declare what wait means by saying

from time import sleep as wait

If you do not, it will not know what wait means, because the official command is time.sleep()