Prefix command that returns when any user, channel, server (guild), role, or other object was created in Discord when you type "!createdat <mentionable>". Should work with any Discord id (snowflake).
// Credit to https://github.com/vegeta897/snow-stamp/blob/main/src/convert.js
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const DISCORD_EPOCH = 1420070400000;
function convertSnowflakeToDate(snowflake) {
return new Date(snowflake / 4194304 + DISCORD_EPOCH);
}
if (context.params.event.content.startsWith(`${process.env.PREFIX || '!'}createdat`)) {
let input = context.params.event.content.split(' ')[1] || `<@!${context.params.event.author.id}>`;
// Replace anything that isn't a digit
let snowflake = Number(input.replace(/[^0-9]+/g, ''));
if (!snowflake) {
return lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `What you provided doesn't appear to be a valid Discord ID. Please try again.`
});
}
let timestamp = convertSnowflakeToDate(snowflake)
if (isNaN(timestamp.getTime())) {
return lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Could not parse the snowflake you provided. Please try again.`
});
}
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `${input} was created on:\n\n`
});
}