zappyzep/backend/src/plugins/Utility/functions/getSnowflakeInfoEmbed.ts

53 lines
1.7 KiB
TypeScript
Raw Normal View History

import { MessageEmbedOptions } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
import moment from "moment-timezone";
import {
EmbedWith, preEmbedPadding
} from "../../../utils";
2020-08-09 17:28:21 +03:00
import { snowflakeToTimestamp } from "../../../utils/snowflakeToTimestamp";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { UtilityPluginType } from "../types";
2020-08-09 17:28:21 +03:00
const SNOWFLAKE_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/742020790471491668/snowflake.png";
export async function getSnowflakeInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
2020-08-09 17:28:21 +03:00
snowflake: string,
showUnknownWarning = false,
requestMemberId?: string,
): Promise<MessageEmbedOptions> {
const embed: EmbedWith<"fields"> = {
2020-08-09 17:28:21 +03:00
fields: [],
};
embed.author = {
name: `Snowflake: ${snowflake}`,
icon_url: SNOWFLAKE_ICON,
};
if (showUnknownWarning) {
embed.description =
"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);
2020-08-09 17:28:21 +03:00
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"));
2020-08-09 17:28:21 +03:00
const snowflakeAge = humanizeDuration(Date.now() - createdAtMS, {
largest: 2,
round: true,
});
embed.fields.push({
name: preEmbedPadding + "Basic information",
value: `Created: **${snowflakeAge} ago** (\`${prettyCreatedAt}\`)`,
2020-08-09 17:28:21 +03:00
});
return embed;
}