'Write timestamp on embed message discord.py
I'm developing a bot and I want to a message to say "Account created today" or something like that (as it does when using timestamp property on embeds) instead of "Account created on 2020-09-02 12:22:47.893000" The code I have now is this:
@client.event
async def on_member_join(member)
embed = discord.Embed(
description=f":inbox_tray: <@{member.id}> joined.",
colour=discord.colour.Color.dark_green(),
timestamp=datetime.now()
)
embed.set_thumbnail(url=member.avatar_url)
embed.add_field(name='Account created:', value=member.created_at)
embed.set_footer(
text=f'User Id: {member.id}')
await client.get_channel(logchannel).send(embed=embed)
Thanks in advance.
Solution 1:[1]
Try using this
embed = discord.Embed.from_dict({
"timestamp": str(datetime.utcnow()),
})
Discord will automatically format the text in the post. For instance, for the same day, you will see Today at h:m PM at the end of the embed.
I hope this is the answer you expected.
Solution 2:[2]
You can check the date when the member joined (<datetime object>.date()) to see if it's todays date, if so then put in Today at h:m PM, but if not today then you can check if the date is from yesterday by getting todays date (datetime.datetime.today().date()) then using the 'timedelta' to take away 1 which basically means it takes away a day from today leaving us with yesterdays date, and then you can check if the member-join date is equal to the date we got (yesterdays date), if so then Yesteday at h:m PM as done below. If something else rather than today and yesterday then leave it as dd/mm/yyyy as discord does (atleast if I remember correctly)
from datetime import timedelta, datetime
embed.add_field(name='Account created:', value=member.created_at.strftime(
'Today at %-I:%M %p' if member.created_at.date() == datetime.today().date()
else 'Yesterday at %-I:%M %p' if member.created_at.date() == (datetime.today() - timedelta(1)).date()
else '%d/%m/%Y')
)
Solution 3:[3]
This is how you can use timestamps.
import datetime
em = discord.Embed()
em.timestamp = datetime.datetime.utcnow()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Rovindu Thamuditha |

