Easy way to find out how old things are.
/*
You can either mention the user/role/channel or simply type all/part of the name.
Type '~howOld' with no parameters to get your own account's creation date.
*/
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const { stripIndents } = require('common-tags');
const event = context.params.event;
const { channel_id, guild_id } = event;
const content = event.content.split(' ').slice(1).join(' ').toLowerCase();
const snowflake = content.replace(/[^0-9]+/g, '');
const regex = new RegExp( content, 'gi' );
console.log(content? 'content => ' + content : '');
await lib.discord.channels['@dev'].messages.destroy({
channel_id, message_id: event.id,
});
await lib.discord.channels['@dev'].typing.create({ channel_id });
let role = await lib.discord.guilds['@dev'].roles.list({ guild_id })
.then((x) => x.find((r) => r.name.match(regex) || r.id == snowflake));
let channel = await lib.discord.guilds['@dev'].channels.list({ guild_id })
.then((x) => x.find((c) => c.name.match(regex) || c.id == snowflake));
let member = await lib.discord.guilds['@dev'].members.list({ guild_id, limit: 1000 })
.then((x) => x.find((m) => {
return m.user.id == snowflake
|| m.nick?.match(regex)
|| m.user.username.match(regex)
}));
let msg;
try {
if (!content[0]) {
console.log('self =>', event.author.username)
await send(`You created your account on:\n${retrieveDate(event.author.id)}`)
} else if (member) {
console.log('member =>', member.user.username)
await send(stripIndents`
**${member.nick ?? member.user.username}** created their account on:
${retrieveDate(member.user.id)}`);
} else if (channel) {
console.log('channel =>', channel.name)
await send(`**<#${channel.id}>** was created on:\n${retrieveDate(channel.id)}`)
} else if (role) {
console.log('role =>', role.name)
await send(stripIndents`
The role **'${role.name}'** was created on:
${retrieveDate(role.id)}`)
} else if (!retrieveDate(content).includes('Invalid')) {
console.log('unknown =>', retrieveDate(content))
await send(`**Unknown** was created on:\n${retrieveDate(content)}`)
} else {
console.log('no results/improper input')
await send(stripIndents`
No results found for **"${content}"**.
Check the spelling and try again.`)
await sleep(5000);
return lib.discord.channels['@dev'].messages.destroy({
channel_id, message_id: msg.id,
});
}
} catch (e) {
console.log(e)
await send(`\`${e}\``)
}
function retrieveDate(snowflake) {
const DISCORD_EPOCH = 1420070400000;
const date = new Date(snowflake / 4194304 + DISCORD_EPOCH);
const unix = ``;
return `${date.toLocaleDateString('en-US')} - ${unix}`
}
async function send(message) {
msg = await lib.discord.channels['@dev'].messages.create({
channel_id, content: message,
});
};
async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};