Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

javascript - Difference between Discord.js message.createdAt time and system time

I'm developing a Discord bot with Discord.js, and when I do the simple following code :

const onMessage = message => {
    if (message.content === "&test") {
        console.log(message.createdAt); // time attached to the message, stored by Discord
        console.log(new Date()); // system time
    }
    // rest of the code
};

I get different results, as you can see :

2021-02-04T17:56:27.657Z
2021-02-04T17:55:46.094Z

As you can see, message.createdAt is ahead of about 40 seconds.

I thought that the difference could come from my system time being not perfect, so I've checked Time.js and my system time is late of about 3 seconds. This means that the time I get from message.createdAt is still 43 seconds ahead of the real time.

Does someone know what's the origin of this difference ?

EDIT : I've manually changed my system time, now I'm perfectly aligned with the real time, but I now have an offset of about 43 seconds between system time and createdAt.

EDIT : My problem is not about latency, everything responds fast, the problem is just about the time being significatively different.

question from:https://stackoverflow.com/questions/66052144/difference-between-discord-js-message-createdat-time-and-system-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally found the origin of the problem, with the help of a friend of me.

As I run the bot on Docker, the time used for new Date() is the time of the container I'm running the code in, while the message.createdAt comes from Discord API whose time is well synchronized. We have come to this conclusion after testing it directly in a console, out of Docker.

The time in the container in my case is directly taken from the time of Docker, which can be different from the system time, when you run some synchronization issues due to hibernation, for example.

After restarting my computer and killing all Docker process that were running, the time has been synchronized, without any additional command to run, and the message.createdAt and the new Date() give nearly the same time. All is fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...