【问题标题】:How can I get the timestamp of a message in Discord.py?如何在 Discord.py 中获取消息的时间戳?
【发布时间】:2020-07-05 01:37:45
【问题描述】:

所以我一直在尝试做一个机器人,它在消息发送时获取并将其上传到数据库。我就是想不通这个 Object 的事情。

这是我的一些代码:

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

我知道这是错误的,因为 discord.Message 给出了一个对象。如何从中获取整数或时间?

编辑:

@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)}')")

【问题讨论】:

  • 显示你的整个代码,否则我们不知道你从哪里得到消息
  • 你去。我已经编辑过了。

标签: python datetime object discord discord.py


【解决方案1】:

目前您正在调用discord.Message 类,但实际上您想调用从该方法获得的消息对象。所以只需使用message 而不是discord.Message

注意:message.created_at 实际上已经返回了一个日期时间对象。 documentation

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 2020-12-21
    • 2020-11-18
    • 2012-06-23
    • 2018-02-28
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多