@client.command() async def ping(ctx): before = time.monotonic() message = await ctx.send("Pong!") ping = (time.monotonic() - before) * 1000 await message.edit(content=f"Pong! `{int(ping)}ms`") #for discord.py rewrite
Here is what the above code is Doing:
1. We create a command called ping with the @client.command() decorator.
2. We define a function that takes a ctx argument.
3. We use the time.monotonic() function to get the current time in milliseconds.
4. We send a message with the ctx.send() function.
5. We calculate the difference in time between when the message was sent and now.
6. We edit the message with the new ping time.