Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

discord.py How can I send a message if difference between the time of message and current time is above time I specified?

timestamp = f'{message.created_at}'
msg_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
now_time = datetime.now()
diff = now_time - msg_time


print('msg_time:', msg_time)
print('now_time:', now_time)
print('diff    :', diff)

output :

msg_time: 2022-07-22 06:02:12.934000
now_time: 2022-07-23 01:53:52.375086
diff    : 19:51:39.441086

If diff is greater than the time I specify, I want it to send a message to the channel like this:

if diff > 00:01:00.000000:
  title2 = "test."
  embed2 = discord.Embed(title=title2, color=0xf1c40f)
  msg = await channels.send(embed=embed2)

I made it here so that if it’s longer than 1 minute, it can be sent, but I don’t know exactly, so it doesn’t work, how can I do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try using timedelta.total_seconds()

diff = diff.total_seconds()

This will convert the time difference into an integer representing the time in seconds. You can use that value much easier.

References:

https://pythontic.com/datetime/timedelta/total_seconds

https://www.geeksforgeeks.org/python-timedelta-total_seconds-method-with-example/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading