feat: use native timestamps more

This commit is contained in:
Dragory 2023-06-24 11:55:49 +00:00
parent 0e70412bc4
commit ecaa16bf7c
10 changed files with 22 additions and 160 deletions

View file

@ -11,7 +11,7 @@
"MEMBER_UNBAN": "{timestamp} 🔓 User (`{userId}`) was unbanned by {userMention(mod)}",
"MEMBER_FORCEBAN": "{timestamp} 🔨 User (`{userId}`) was forcebanned by {userMention(mod)}",
"MEMBER_SOFTBAN": "{timestamp} 🔨 {userMention(member)} was softbanned by {userMention(mod)}",
"MEMBER_JOIN": "{timestamp} 📥 {new} {userMention(member)} joined (created {account_age} ago)",
"MEMBER_JOIN": "{timestamp} 📥 {new} {userMention(member)} joined (created <t:{account_age_ts}:R>)",
"MEMBER_LEAVE": "{timestamp} 📤 {userMention(member)} left the server",
"MEMBER_ROLE_ADD": "{timestamp} 🔑 {userMention(mod)} added roles for {userMention(member)}: **{roles}**",
"MEMBER_ROLE_REMOVE": "{timestamp} 🔑 {userMention(mod)} removed roles from {userMention(member)}: **{roles}**",

View file

@ -26,6 +26,7 @@ export function logMemberJoin(pluginData: GuildPluginData<LogsPluginType>, data:
member: memberToTemplateSafeMember(data.member),
new: data.member.user.createdTimestamp >= newThreshold ? " :new:" : "",
account_age: accountAge,
account_age_ts: Math.round(data.member.user.createdTimestamp / 1000).toString(),
}),
{
userId: data.member.id,

View file

@ -150,6 +150,7 @@ export const LogTypeData = z.object({
member: z.instanceof(TemplateSafeMember),
new: z.string(),
account_age: z.string(),
account_age_ts: z.string(),
}),
[LogType.MEMBER_LEAVE]: z.object({

View file

@ -1,9 +1,7 @@
import { APIEmbed, ChannelType, Snowflake, StageChannel, VoiceChannel } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { EmbedWith, MINUTES, formatNumber, preEmbedPadding, trimLines, verboseUserMention } from "../../../utils";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { UtilityPluginType } from "../types";
const TEXT_CHANNEL_ICON =
@ -74,17 +72,6 @@ export async function getChannelInfoEmbed(
channelName = channel.name;
}
const createdAt = moment.utc(channel.createdAt!, "x");
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const channelAge = humanizeDuration(Date.now() - channel.createdTimestamp!, {
largest: 2,
round: true,
});
const showMention = channel.type !== ChannelType.GuildCategory;
embed.fields.push({
@ -92,7 +79,7 @@ export async function getChannelInfoEmbed(
value: trimLines(`
Name: **${channelName}**
ID: \`${channel.id}\`
Created: **${channelAge} ago** (\`${prettyCreatedAt}\`)
Created: **<t:${Math.round(channel.createdTimestamp! / 1000)}:R>**
Type: **${channelType}**
${showMention ? `Mention: <#${channel.id}>` : ""}
`),

View file

@ -1,5 +1,4 @@
import { APIEmbed, ChannelType } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import {
EmbedWith,
@ -44,10 +43,6 @@ export async function getInviteInfoEmbed(
}
const serverCreatedAtTimestamp = snowflakeToTimestamp(invite.guild.id);
const serverAge = humanizeDuration(Date.now() - serverCreatedAtTimestamp, {
largest: 2,
round: true,
});
const memberCount = inviteHasCounts(invite) ? invite.memberCount : 0;
@ -58,7 +53,7 @@ export async function getInviteInfoEmbed(
value: trimLines(`
Name: **${invite.guild.name}**
ID: \`${invite.guild.id}\`
Created: **${serverAge} ago**
Created: **<t:${Math.round(serverCreatedAtTimestamp / 1000)}:R>**
Members: **${formatNumber(memberCount)}** (${formatNumber(presenceCount)} online)
`),
inline: true,
@ -68,15 +63,11 @@ export async function getInviteInfoEmbed(
invite.channel.type === ChannelType.GuildVoice ? `🔉 ${invite.channel.name}` : `#${invite.channel.name}`;
const channelCreatedAtTimestamp = snowflakeToTimestamp(invite.channel.id);
const channelAge = humanizeDuration(Date.now() - channelCreatedAtTimestamp, {
largest: 2,
round: true,
});
let channelInfo = trimLines(`
Name: **${channelName}**
ID: \`${invite.channel.id}\`
Created: **${channelAge} ago**
Created: **<t:${Math.round(channelCreatedAtTimestamp / 1000)}:R>**
`);
if (invite.channel.type !== ChannelType.GuildVoice) {
@ -120,17 +111,13 @@ export async function getInviteInfoEmbed(
}*/
const channelCreatedAtTimestamp = snowflakeToTimestamp(invite.channel!.id);
const channelAge = humanizeDuration(Date.now() - channelCreatedAtTimestamp, {
largest: 2,
round: true,
});
embed.fields.push({
name: preEmbedPadding + "Group DM information",
value: trimLines(`
Name: ${invite.channel!.name ? `**${invite.channel!.name}**` : `_Unknown_`}
ID: \`${invite.channel!.id}\`
Created: **${channelAge} ago**
Created: **<t:${Math.round(channelCreatedAtTimestamp / 1000)}:R>**
Members: **${formatNumber((invite as any).memberCount)}**
`),
inline: true,

View file

@ -1,7 +1,5 @@
import { APIEmbed, MessageType, Snowflake, TextChannel } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData, getDefaultMessageCommandPrefix } from "knub";
import moment from "moment-timezone";
import {
EmbedWith,
chunkMessageLines,
@ -39,28 +37,6 @@ export async function getMessageInfoEmbed(
},
};
const createdAt = moment.utc(message.createdAt, "x");
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const messageAge = humanizeDuration(Date.now() - message.createdTimestamp, {
largest: 2,
round: true,
});
const editedAt = message.editedTimestamp ? moment.utc(message.editedTimestamp!, "x") : undefined;
const tzEditedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, editedAt)
: timeAndDate.inGuildTz(editedAt);
const prettyEditedAt = tzEditedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const editAge =
message.editedTimestamp &&
humanizeDuration(Date.now() - message.editedTimestamp, {
largest: 2,
round: true,
});
const type =
{
[MessageType.Default]: "Regular message",
@ -82,45 +58,23 @@ export async function getMessageInfoEmbed(
ID: \`${message.id}\`
Channel: <#${message.channel.id}>
Channel ID: \`${message.channel.id}\`
Created: **${messageAge} ago** (\`${prettyCreatedAt}\`)
${editedAt ? `Edited at: **${editAge} ago** (\`${prettyEditedAt}\`)` : ""}
Created: **<t:${Math.round(message.createdTimestamp / 1000)}:R>**
${message.editedTimestamp ? `Edited at: **<t:${Math.round(message.editedTimestamp / 1000)}:R>**` : ""}
Type: **${type}**
Link: [**Go to message **](${messageLink(pluginData.guild.id, message.channel.id, message.id)})
`),
),
});
const authorCreatedAt = moment.utc(message.author.createdAt, "x");
const tzAuthorCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, authorCreatedAt)
: timeAndDate.inGuildTz(authorCreatedAt);
const prettyAuthorCreatedAt = tzAuthorCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const authorAccountAge = humanizeDuration(Date.now() - message.author.createdTimestamp, {
largest: 2,
round: true,
});
const authorJoinedAt = message.member && moment.utc(message.member.joinedTimestamp!, "x");
const tzAuthorJoinedAt = authorJoinedAt
? requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, authorJoinedAt)
: timeAndDate.inGuildTz(authorJoinedAt)
: null;
const prettyAuthorJoinedAt = tzAuthorJoinedAt?.format(timeAndDate.getDateFormat("pretty_datetime"));
const authorServerAge =
message.member &&
humanizeDuration(Date.now() - message.member.joinedTimestamp!, {
largest: 2,
round: true,
});
const authorJoinedAtTS = message.member?.joinedTimestamp;
embed.fields.push({
name: preEmbedPadding + "Author information",
value: trimLines(`
Name: **${renderUsername(message.author.username, message.author.discriminator)}**
ID: \`${message.author.id}\`
Created: **${authorAccountAge} ago** (\`${prettyAuthorCreatedAt}\`)
${authorJoinedAt ? `Joined: **${authorServerAge} ago** (\`${prettyAuthorJoinedAt}\`)` : ""}
Created: **<t:${Math.round(message.author.createdTimestamp / 1000)}:R>**
${authorJoinedAtTS ? `Joined: **<t:${Math.round(authorJoinedAtTS / 1000)}:R>**` : ""}
Mention: <@!${message.author.id}>
`),
});

View file

@ -1,10 +1,7 @@
import { APIEmbed, PermissionFlagsBits, Role } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { EmbedWith, preEmbedPadding, trimLines } from "../../../utils";
import { PERMISSION_NAMES } from "../../../utils/permissionNames.js";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { UtilityPluginType } from "../types";
const MENTION_ICON = "https://cdn.discordapp.com/attachments/705009450855039042/839284872152481792/mention.png";
@ -23,17 +20,6 @@ export async function getRoleInfoEmbed(
color: role.color,
};
const createdAt = moment.utc(role.createdAt, "x");
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const roleAge = humanizeDuration(Date.now() - role.createdTimestamp, {
largest: 2,
round: true,
});
const rolePerms = role.permissions.has(PermissionFlagsBits.Administrator)
? [PERMISSION_NAMES.Administrator]
: role.permissions.toArray().map((p) => PERMISSION_NAMES[p]);
@ -46,7 +32,7 @@ export async function getRoleInfoEmbed(
value: trimLines(`
Name: **${role.name}**
ID: \`${role.id}\`
Created: **${roleAge} ago** (\`${prettyCreatedAt}\`)
Created: **<t:${Math.round(role.createdTimestamp / 1000)}:R>**
Position: **${role.position} / ${totalGuildRoles}**
Color: **#${role.color.toString(16).toUpperCase().padStart(6, "0")}**
Mentionable: **${role.mentionable ? "Yes" : "No"}**

View file

@ -1,7 +1,5 @@
import { APIEmbed, ChannelType, GuildPremiumTier, Snowflake } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import {
EmbedWith,
MINUTES,
@ -15,7 +13,6 @@ import {
trimLines,
} from "../../../utils";
import { idToTimestamp } from "../../../utils/idToTimestamp";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { UtilityPluginType } from "../types";
import { getGuildPreview } from "./getGuildPreview";
@ -51,19 +48,10 @@ export async function getServerInfoEmbed(
};
// BASIC INFORMATION
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAt = moment.utc(idToTimestamp((guildPreview || restGuild)!.id)!, "x");
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const serverAge = humanizeDuration(moment.utc().valueOf() - createdAt.valueOf(), {
largest: 2,
round: true,
});
const createdAtTs = Number(idToTimestamp((guildPreview || restGuild)!.id)!);
const basicInformation: string[] = [];
basicInformation.push(`Created: **${serverAge} ago** (\`${prettyCreatedAt}\`)`);
basicInformation.push(`Created: **<t:${Math.round(createdAtTs / 1000)}:R>**`);
if (thisServer) {
const owner = await resolveUser(pluginData.client, thisServer.ownerId);

View file

@ -1,10 +1,7 @@
import { APIEmbed } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { EmbedWith, preEmbedPadding } from "../../../utils";
import { snowflakeToTimestamp } from "../../../utils/snowflakeToTimestamp";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { UtilityPluginType } from "../types";
const SNOWFLAKE_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/742020790471491668/snowflake.png";
@ -28,21 +25,11 @@ export async function getSnowflakeInfoEmbed(
"This is a valid [snowflake ID](https://discord.com/developers/docs/reference#snowflakes), but I don't know what it's for.";
}
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAtMS = snowflakeToTimestamp(snowflake);
const createdAt = moment.utc(createdAtMS, "x");
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const snowflakeAge = humanizeDuration(Date.now() - createdAtMS, {
largest: 2,
round: true,
});
embed.fields.push({
name: preEmbedPadding + "Basic information",
value: `Created: **${snowflakeAge} ago** (\`${prettyCreatedAt}\`)`,
value: `Created: **<t:${Math.round(createdAtMS / 1000)}:R>**`,
});
return embed;

View file

@ -1,7 +1,5 @@
import { APIEmbed } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import { CaseTypes } from "../../../data/CaseTypes";
import {
EmbedWith,
@ -50,36 +48,18 @@ export async function getUserInfoEmbed(
const avatarURL = user.displayAvatarURL();
embed.author.icon_url = avatarURL;
const createdAt = moment.utc(user.createdAt, "x");
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const accountAge = humanizeDuration(moment.utc().valueOf() - user.createdTimestamp, {
largest: 2,
round: true,
});
if (compact) {
embed.fields.push({
name: preEmbedPadding + `${user.bot ? "Bot" : "User"} information`,
value: trimLines(`
Profile: <@!${user.id}>
Created: **${accountAge} ago** (\`${prettyCreatedAt}\`)
Created: **<t:${Math.round(user.createdTimestamp / 1000)}:R>**
`),
});
if (member) {
const joinedAt = moment.utc(member.joinedTimestamp!, "x");
const tzJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
: timeAndDate.inGuildTz(joinedAt);
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedTimestamp!, {
largest: 2,
round: true,
});
embed.fields[0].value += `\n${user.bot ? "Added" : "Joined"}: **${joinAge} ago** (\`${prettyJoinedAt}\`)`;
embed.fields[0].value += `\n${user.bot ? "Added" : "Joined"}: **<t:${Math.round(
member.joinedTimestamp! / 1000,
)}:R>**`;
} else {
embed.fields.push({
name: preEmbedPadding + "!! NOTE !!",
@ -94,7 +74,7 @@ export async function getUserInfoEmbed(
if (user.discriminator !== "0") {
userInfoLines.push(`Discriminator: **${user.discriminator}**`);
}
userInfoLines.push(`Created: **${accountAge} ago** (\`${prettyCreatedAt}\`)`);
userInfoLines.push(`Created: **<t:${Math.round(user.createdTimestamp / 1000)}:R>**`);
userInfoLines.push(`Mention: <@!${user.id}>`);
embed.fields.push({
@ -103,22 +83,13 @@ export async function getUserInfoEmbed(
});
if (member) {
const joinedAt = moment.utc(member.joinedTimestamp!, "x");
const tzJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
: timeAndDate.inGuildTz(joinedAt);
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedTimestamp!, {
largest: 2,
round: true,
});
const roles = Array.from(member.roles.cache.values()).filter((r) => r.id !== pluginData.guild.id);
roles.sort(sorter("position", "DESC"));
embed.fields.push({
name: preEmbedPadding + "Member information",
value: trimLines(`
${user.bot ? "Added" : "Joined"}: **${joinAge} ago** (\`${prettyJoinedAt}\`)
${user.bot ? "Added" : "Joined"}: **<t:${Math.round(member.joinedTimestamp! / 1000)}:R>**
${roles.length > 0 ? "Roles: " + trimRoles(roles.map((r) => `<@&${r.id}>`)) : ""}
`),
});