I'm having an issue getting the correct timeout timestamp when a timeout has been set for a user in the Discord client.
Basically the timestamp gives me a duration -27675028 minute and expiry like this Thu, 01 Jan 1970 00:00:00 GMT (sometimes the previous timestamp) if a timeout was set before.
What I'm trying to do is use the guildMemberUpdate event to listen for when a timeout has been added to a member and then log the details such as duration, expiration etc to a channel.
Here is what I'm working with:
module.exports = new Event("guildMemberUpdate", async (oldMember, newMember) => {
const log_channel = oldMember.channels.cache.find(c => c.name == config.log_channel);
let entry = await newMember.guild.fetchAuditLogs({limit: 1, type: AuditLogEvent.MemberUpdate}).then(audit => audit.entries.first());
if (oldMember.communicationDisabledUntilTimestamp !== newMember.communicationDisabledUntilTimestamp) {
if (!oldMember.communicationDisabled || oldMember.communicationDisabled > new Date().setUTCDate()) // new timeout added
{
const expiresAt = new Date(newMember.communicationDisabledUntilTimestamp).toUTCString(); // expiration date of timeout
let duration = newMember.communicationDisabledUntilTimestamp - new Date().valueOf(); // duration of timeout
if (duration < 3600000) {
if (duration <= 60000) {
duration = Math.ceil((duration / 1000) / 60) + ' minute';
} else {
duration = Math.ceil((duration / 1000) / 60) + ' minutes';
}
} else {
if (duration <= 3600000) {
duration = Math.ceil((duration / 1000) / 60 / 60) + ' hour';
} else {
duration = Math.ceil((duration / 1000) / 60 / 60) + ' hours';
}
}
const embed1 = new Discord.EmbedBuilder();
embed1
.setTitle(`Member Timeout`)
.setColor("Orange")
.setTimestamp()
.setFooter({text: `Time out by ${entry.executor.username}`})
.setFields(
{
name: 'Member',
value: `${newMember.user}`,
inline: true,
},
{
name: 'Duration',
value: `${duration}`,
inline: true
},
{
name: 'Expires',
value: `${expiresAt}`,
inline: true
},
{
name: "Reason",
value: `${entry.reason || "No reason provided"}`,
inline: false
}
)
log_channel.send({ embeds: [embed1] });
}
}
Here is what the issue looks like:
Help appreciated