gpt4 book ai didi

javascript - 如何获得服务器加入和今天 discord.js v12 之间的正确时差?

转载 作者:行者123 更新时间:2023-12-04 08:55:34 26 4
gpt4 key购买 nike

我正在处理我的用户信息命令,但遇到了一个问题,当我显示某人加入服务器的时间时,它有时会显示正确的时间,有时则不会。我认为这是由时区引起的。我的代码就在这里:

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);
}
},
};
有办法解决吗?我尝试转换 joinDatetoday使用 .getUTCHours 到 UTC 时间(在获得年、月、日和周差异之后)或 .toUTCString ,但第一个导致小时、分钟和秒始终为 0,第二个导致它为 NaN。提前致谢 :)

最佳答案

好吧,事实证明这实际上很容易。我改变了这些行:

   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);
进入这个:
var hoursDiffServerJoin = today.getUTCHours() - joinDate.getUTCHours();
var minutesDiffServerJoin = today.getUTCMinutes() - joinDate.getUTCMinutes();
var secondsDiffServerJoin = today.getUTCSeconds() - joinDate.getUTCSeconds();
我认为它以 UTC 格式获取所有值,因此时区不再改变任何内容。

关于javascript - 如何获得服务器加入和今天 discord.js v12 之间的正确时差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63849761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com