[英]How to get right time difference between server joining and today discord.js v12?
I'm working on my user info command and I've run into a problem, that when I display the time from someones joining the server, it sometimes shows the right hours, and sometimes not.我正在处理我的用户信息命令,但遇到了一个问题,当我显示某人加入服务器的时间时,它有时会显示正确的时间,有时则不会。 I think it's caused by timezones.
我认为这是由时区引起的。 My code is right here:
我的代码就在这里:
const { DiscordAPIError } = require('discord.js');
const Discord = require('discord.js');
const moment = require('moment');
module.exports = {
name: 'profile',
description: 'The bot will return the info about the user',
execute(message, args) {
let userinfoget =
message.mentions.members.first() ||
message.guild.members.cache.get(args[0]) ||
message.member;
var isBot = 0;
if (userinfoget.user.bot) {
isBot = 'This account is a bot';
} else {
isBot = "This account isn't a bot";
}
var status = userinfoget.presence.status;
if (status == 'online') {
status = `<:statusonline:750346592242106368> This user is currently online`;
} else if (status == 'idle') {
status = `<:statusidle:750346522847215760> This user is currently idle`;
} else if (status == 'dnd') {
status = `<:statusdnd:750346486398582815> This user has currently do not disturb mode on`;
} else if (status == 'offline') {
status = `<:statusoffline:750346558809047090> This user is currently offline (or lurking <:lurklias:750342622463262751>)`;
} else {
status == `Unable to get user's status`;
}
var today = new Date();
var joinDate = new Date(userinfoget.joinedAt);
if (1 == 1) {
var yearsDiffServerJoin = today.getFullYear() - joinDate.getFullYear();
var monthsDiffServerJoin = today.getMonth() - joinDate.getMonth();
var daysDiffServerJoin = today.getDate() - joinDate.getDate();
if (
monthsDiffServerJoin < 0 ||
(monthsDiffServerJoin === 0 && daysDiffServerJoin < 0)
)
yearsDiffServerJoin--;
if (monthsDiffServerJoin < 0) monthsDiffServerJoin += 12;
if (daysDiffServerJoin < 0) {
var joinDateAux = joinDate.getDate();
joinDate.setMonth(joinDate.getMonth() + 1, 0);
daysDiffServerJoin = joinDate.getDate() - joinDateAux + today.getDate();
monthsDiffServerJoin--;
}
if (daysDiffServerJoin > 7) {
var weeksDiffServerJoin = (
(daysDiffServerJoin - (daysDiffServerJoin % 7)) /
7
).toFixed(0);
daysDiffServerJoin = daysDiffServerJoin - weeksDiffServerJoin * 7;
} else var weeksDiffServerJoin = 0;
var hoursDiffServerJoin = (
((today - joinDate) / (1000 * 60 * 60)) %
24
).toFixed(0);
console.log((today - joinDate) / (1000 * 60 * 60));
var minutesDiffServerJoin = (
((today - joinDate) / (1000 * 60)) %
60
).toFixed(0);
var secondsDiffServerJoin = (((today - joinDate) / 1000) % 60).toFixed(0);
console.log('hours, mins, secs');
console.log(hoursDiffServerJoin);
console.log(minutesDiffServerJoin);
console.log(secondsDiffServerJoin);
var result = [];
if (yearsDiffServerJoin > 0)
result.push(
yearsDiffServerJoin + (yearsDiffServerJoin > 1 ? ' years' : ' year')
);
if (monthsDiffServerJoin > 0)
result.push(
monthsDiffServerJoin + (monthsDiffServerJoin > 1 ? ' months' : ' month')
);
if (daysDiffServerJoin > 0)
result.push(
daysDiffServerJoin + (daysDiffServerJoin > 1 ? ' days' : ' day')
);
console.log(yearsDiffServerJoin);
console.log(monthsDiffServerJoin);
console.log(weeksDiffServerJoin);
console.log(daysDiffServerJoin);
var highestRoleID = userinfoget.roles.highest.id;
var joined = moment(userinfoget.joinedAt).format('DD/MM/YY, HH:mm:ss');
console.log(`Highest role = ${highestRoleID}`);
console.log(`User = ${userinfoget}`);
var joinDate = new Date(userinfoget.joinedAt);
var today = Date.now();
var timeFromJoiningServer = today - joinDate;
var yearsServer = (
timeFromJoiningServer /
(1000 * 60 * 60 * 24 * 365.25)
).toFixed(0);
var monthsServer;
console.log(yearsServer);
console.log(timeFromJoiningServer);
console.log(userinfoget.joinedAt);
const embed = new Discord.MessageEmbed()
.setColor(userinfoget.displayHexColor)
.setAuthor(`${userinfoget.user.tag}`, userinfoget.user.displayAvatarURL())
.addFields({ name: `User ping`, value: `<@${userinfoget.id}>` })
.addFields({ name: `User ID`, value: `${userinfoget.id}` })
.addFields(
{
name: 'Joined Server',
value:
moment(userinfoget.joinedAt).format('LLLL') +
' ' +
timeFromJoiningServerMessage,
} // or moment(userinfoget.joinedAt).format('DD/MM/YY, HH:mm:ss')
)
.addFields(
{
name: 'Joined Discord',
value: moment(userinfoget.user.createdAt).format('LLLL'),
} // or moment(userinfoget.createdAt).format('DD/MM/YY, HH:mm:ss')
)
.addFields({ name: 'Highest role', value: `<@&${highestRoleID}>` })
.addFields({ name: 'Online Status', value: `${status}` })
.addFields({ name: 'Is a bot?', value: `${isBot}` })
.setFooter('Bot made by mkpanda');
message.channel.send(embed);
}
},
};
Is there a way to fix it?有办法解决吗? I tried converting
joinDate
and today
to UTC time (after getting the year, month, day and weeks difference) using .getUTCHours
or .toUTCString
, but the first caused hours, minutes and seconds always being 0, the second one caused it to be NaN.我尝试使用
.getUTCHours
或.toUTCString
将joinDate
和today
转换为 UTC 时间(在获得年、月、日和周的差异后),但第一个导致小时、分钟和秒始终为 0,第二个导致它为 NaN . Thanks in advance :)提前致谢 :)
Ok, turns out it's actually quite easy.好吧,事实证明这实际上很容易。 I changed these lines:
我改变了这些行:
var hoursDiffServerJoin = (
((today - joinDate) / (1000 * 60 * 60)) %
24
).toFixed(0);
console.log((today - joinDate) / (1000 * 60 * 60));
var minutesDiffServerJoin = (
((today - joinDate) / (1000 * 60)) %
60
).toFixed(0);
var secondsDiffServerJoin = (((today - joinDate) / 1000) % 60).toFixed(0);
into this:进入这个:
var hoursDiffServerJoin = today.getUTCHours() - joinDate.getUTCHours();
var minutesDiffServerJoin = today.getUTCMinutes() - joinDate.getUTCMinutes();
var secondsDiffServerJoin = today.getUTCSeconds() - joinDate.getUTCSeconds();
which I think gets all the values in UTC, so the timezones don't change anything anymore.我认为它以 UTC 格式获取所有值,因此时区不再改变任何内容。
Answering to Michal Kachlik回答 Michal Kachlik
By moment you can use :现在你可以使用:
moment.utc(message.author.createdTimestamp).fromNow()
My experience : https://prnt.sc/ug7mol我的经验: https : //prnt.sc/ug7mol
Even if it's not what you asked.即使不是你问的。 Moment is extremely helpful when it comes to time so try to check it's tutorials or documentation or search in the internet what do you want
当涉及到时间时,时刻非常有用,因此请尝试检查它的教程或文档或在互联网上搜索您想要什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.