From ece00a9be440f98378ee43077316a153eb72625e Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Thu, 10 Jun 2021 21:54:17 -0600 Subject: [PATCH] utility: add snowflake --- src/modules/utility.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/modules/utility.js b/src/modules/utility.js index e4eee37..4ed220f 100644 --- a/src/modules/utility.js +++ b/src/modules/utility.js @@ -209,3 +209,30 @@ lookupinvite.callback = async function (msg, line) { } }; hf.registerCommand(lookupinvite); + +const snowflake = new Command("snowflake"); +snowflake.category = CATEGORY; +snowflake.helpText = "Converts a snowflake ID into readable time."; +snowflake.usage = "<--twitter> [snowflake]"; +snowflake.callback = function (msg, line) { + let twitter = false; + if (line.startsWith("--twitter")) { + twitter = true; + line.replace("--twitter ", ""); + } + const num = parseInt(line); + if (!isNaN(num)) { + let binary = num.toString(2); + binary = "0".repeat(64 - binary.length) + binary; + const timestamp = + parseInt(binary.substr(0, 42), 2) + + (twitter ? 1288834974657 : 1420070400000); + + return `The timestamp for \`${line}\` is ${ + new Date(timestamp).toUTCString + }`; + } else { + return "Argument provided is not a number."; + } +}; +hf.registerCommand(snowflake);