joinedAtTimeStamp 正在显示 Nan discord.js
joinedAtTimeStamp is showing Nan discord.js
我正在编写 userInfo
命令,但是当我使用该命令时,joinedAtTimeStamp
在嵌入中显示 <t:NaN:R>
。这是这段代码中唯一的问题。
我的代码:
const { MessageEmbed, ContextMenuInteraction } = require("discord.js");
module.exports = {
name: "userInfo",
aliases: ["user"],
permissions: ["SEND_MESSAGES", "ATTACH_FILES"],
description: "user",
async execute(message, args, cmd, client, Discord, profileData) {
const target = message.mentions.users.first();
if(!args[0]) {
const response2 = new MessageEmbed()
.setColor("RANDOM")
.setAuthor({name: message.author.tag, iconURL: message.author.displayAvatarURL({dynamic: true})})
.setThumbnail(message.author.displayAvatarURL({dynamic: true}))
.addFields(
{name: "ID", value: message.author.id},
{name: "Joined Server", value: `<t:${parseInt(message.author.joinedTimestamp / 1000)}:R>`, inline: true},
{name: "Account Created", value: `<t:${parseInt(message.author.createdTimestamp / 1000)}:R>`, inline: true},
);
message.reply({embeds:[response2]});
}
const response = new MessageEmbed()
.setColor("RANDOM")
.setAuthor({name: target.tag, iconURL: target.displayAvatarURL({dynamic: true})})
.setThumbnail(target.displayAvatarURL({dynamic: true}))
.addFields(
{name: "ID", value: target.id},
{name: "Joined Server", value: `<t:${parseInt(target.joinedTimestamp / 1000)}:R>`, inline: true},
{name: "Account Created", value: `<t:${parseInt(target.createdTimestamp / 1000)}:R>`, inline: true},
);
message.reply({embeds: [response], ephemeral: true})
}
}
我正在使用 discord.js v13 和节点 16。
你能确认 target.joinedTimestamp
的类型吗?
可能是您在这里进行的双重转换:
parseInt(target.joinedTimestamp / 1000)
毁了你的号码。
(您正在将 target.createdTimestamp
转换为数字,将其除以 100(使其成为浮点数),然后通过转换回整数来丢弃浮点数。)
message.author
是 User
,它没有 joinedTimestamp
属性,只有 GuildMember
有。 message.member
represents the author of the message as a guild member, so you can use that as it will have a joinedTimestamp
属性.
你看到 NaN
而不是正确值的原因是因为 parseInt
将 return NaN
如果你尝试解析 undefined
:
console.log('undefined:', parseInt(undefined / 1000, 10));
console.log('3459192421512:', parseInt(3459192421512 / 1000, 10));
以下代码应按预期工作
.addFields(
{ name: 'ID', value: message.author.id },
{
name: 'Joined Server',
value: `<t:${parseInt(message.member.joinedTimestamp / 1000, 10)}:R>`,
inline: true,
},
{
name: 'Account Created',
value: `<t:${parseInt(message.author.createdTimestamp / 1000, 10)}:R>`,
inline: true,
},
);
至于target
,也是一样的问题; message.mentions.users.first()
是一个 User
。您可以创建一个新变量,例如targetMember
并分配 message.mentions.members.first()
,所以它将是 GuildMember
:
const target = message.mentions.users.first();
const targetMember = message.mentions.members.first();
然后,只需替换 target
:
.addFields(
{ name: 'ID', value: target.id },
{
name: 'Joined Server',
value: `<t:${parseInt(targetMember.joinedTimestamp / 1000, 10)}:R>`,
inline: true,
},
{
name: 'Account Created',
value: `<t:${parseInt(target.createdTimestamp / 1000, 10)}:R>`,
inline: true,
},
);
PS: use the radix in parseInt
是个好主意。这就是为什么我在 parseInt
.
中添加 10
作为第二个参数的原因
我正在编写 userInfo
命令,但是当我使用该命令时,joinedAtTimeStamp
在嵌入中显示 <t:NaN:R>
。这是这段代码中唯一的问题。
我的代码:
const { MessageEmbed, ContextMenuInteraction } = require("discord.js");
module.exports = {
name: "userInfo",
aliases: ["user"],
permissions: ["SEND_MESSAGES", "ATTACH_FILES"],
description: "user",
async execute(message, args, cmd, client, Discord, profileData) {
const target = message.mentions.users.first();
if(!args[0]) {
const response2 = new MessageEmbed()
.setColor("RANDOM")
.setAuthor({name: message.author.tag, iconURL: message.author.displayAvatarURL({dynamic: true})})
.setThumbnail(message.author.displayAvatarURL({dynamic: true}))
.addFields(
{name: "ID", value: message.author.id},
{name: "Joined Server", value: `<t:${parseInt(message.author.joinedTimestamp / 1000)}:R>`, inline: true},
{name: "Account Created", value: `<t:${parseInt(message.author.createdTimestamp / 1000)}:R>`, inline: true},
);
message.reply({embeds:[response2]});
}
const response = new MessageEmbed()
.setColor("RANDOM")
.setAuthor({name: target.tag, iconURL: target.displayAvatarURL({dynamic: true})})
.setThumbnail(target.displayAvatarURL({dynamic: true}))
.addFields(
{name: "ID", value: target.id},
{name: "Joined Server", value: `<t:${parseInt(target.joinedTimestamp / 1000)}:R>`, inline: true},
{name: "Account Created", value: `<t:${parseInt(target.createdTimestamp / 1000)}:R>`, inline: true},
);
message.reply({embeds: [response], ephemeral: true})
}
}
我正在使用 discord.js v13 和节点 16。
你能确认 target.joinedTimestamp
的类型吗?
可能是您在这里进行的双重转换:
parseInt(target.joinedTimestamp / 1000)
毁了你的号码。
(您正在将 target.createdTimestamp
转换为数字,将其除以 100(使其成为浮点数),然后通过转换回整数来丢弃浮点数。)
message.author
是 User
,它没有 joinedTimestamp
属性,只有 GuildMember
有。 message.member
represents the author of the message as a guild member, so you can use that as it will have a joinedTimestamp
属性.
你看到 NaN
而不是正确值的原因是因为 parseInt
将 return NaN
如果你尝试解析 undefined
:
console.log('undefined:', parseInt(undefined / 1000, 10));
console.log('3459192421512:', parseInt(3459192421512 / 1000, 10));
以下代码应按预期工作
.addFields(
{ name: 'ID', value: message.author.id },
{
name: 'Joined Server',
value: `<t:${parseInt(message.member.joinedTimestamp / 1000, 10)}:R>`,
inline: true,
},
{
name: 'Account Created',
value: `<t:${parseInt(message.author.createdTimestamp / 1000, 10)}:R>`,
inline: true,
},
);
至于target
,也是一样的问题; message.mentions.users.first()
是一个 User
。您可以创建一个新变量,例如targetMember
并分配 message.mentions.members.first()
,所以它将是 GuildMember
:
const target = message.mentions.users.first();
const targetMember = message.mentions.members.first();
然后,只需替换 target
:
.addFields(
{ name: 'ID', value: target.id },
{
name: 'Joined Server',
value: `<t:${parseInt(targetMember.joinedTimestamp / 1000, 10)}:R>`,
inline: true,
},
{
name: 'Account Created',
value: `<t:${parseInt(target.createdTimestamp / 1000, 10)}:R>`,
inline: true,
},
);
PS: use the radix in parseInt
是个好主意。这就是为什么我在 parseInt
.
10
作为第二个参数的原因