How can I get the timestamp of a message in Discord.py?

Viewed 13885

So I have been trying to do a bot, that gets when the message was sent and upload it to a database. I just can't figure this Object thing out.

Here is some of my code:

'{datetime.datetime.fromtimestamp(discord.Message.created_at)}'

I know this is wrong, because the discord.Message gives an object. How can I get an integer or a time out of it?

EDIT:

@client.command()
async def belep(message, url, felhasz, jelsz):
    if isinstance(message.channel, discord.DMChannel):
        for i in c:
            if i['Url'] == url:
                db = MySQLdb.connect(xxxxx)
                cursor = db.cursor()
                print(url + " " + felhasz + " " + jelsz)
                icode = i["InstituteCode"]
                headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'}
                payload = {'institute_code': f'{icode}', 'userName': f'{felhasz}', 'password': f'{jelsz}',
                           'grant_type': 'password', 'client_id': '919e0c1c-76a2-4646-a2fb-7085bbbf3c56'}

                response = requests.get(f"""https://{icode}.e-kreta.hu/idp/api/v1/Token""", headers=headers, data=payload)

                rjson = response.json()
                print()
                cursor.execute(
                    f"INSERT INTO token (mauthor, access_token, token_type, expires_in, refresh_token, msent, tokexpire) VALUES ('{message.author.id}', '{rjson['access_token']}', '{rjson['token_type']}', '{rjson['expires_in']}', '{rjson['refresh_token']}', '{datetime.datetime.fromtimestamp(discord.Message.created_at)}','{datetime.datetime.fromtimestamp(discord.Message.created_at)}')")
1 Answers

At the moment you are calling the discord.Message class, but you actually wanna call the message object you get from the method. So just use message instead of discord.Message.

Note: message.created_at actually already returns a datetime object. documentation

Related