From e1667e6865b6b1d42bcda6ed960746a679c21c8a Mon Sep 17 00:00:00 2001 From: Keanu Date: Thu, 25 Mar 2021 13:09:18 +0100 Subject: [PATCH] Added SnowflakeInfo module. --- SnowflakeInfo/goosemodModule.json | 11 ++++ SnowflakeInfo/index.js | 87 +++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 SnowflakeInfo/goosemodModule.json create mode 100644 SnowflakeInfo/index.js diff --git a/SnowflakeInfo/goosemodModule.json b/SnowflakeInfo/goosemodModule.json new file mode 100644 index 0000000..d164698 --- /dev/null +++ b/SnowflakeInfo/goosemodModule.json @@ -0,0 +1,11 @@ +{ + "main": "index.js", + + "name": "SnowflakeInfo", + "description": "Returns info about a given snowflake.", + "tags": ["commands"], + + "authors": ["717352467280691331"], + + "version": "1.0.0" +} diff --git a/SnowflakeInfo/index.js b/SnowflakeInfo/index.js new file mode 100644 index 0000000..a486704 --- /dev/null +++ b/SnowflakeInfo/index.js @@ -0,0 +1,87 @@ +import { commands, internalMessage } from '@goosemod/patcher'; + +const discordEpoch = Number(1420070400000); + +const idToBinary = (num) => { + let bin = ''; + let high = parseInt(num.slice(0, -10)) || 0; + let low = parseInt(num.slice(-10)); + try { + while (low > 0 || high > 0) { + bin = String(low & 1) + bin; + low = Math.floor(low / 2); + if (high > 0) { + low += 5000000000 * (high % 2); + high = Math.floor(high / 2); + } + } + } catch (e) { + console.error(e); + } + return bin; +}; + +export default { + goosemodHandlers: { + onImport: () => { + commands.add( + 'echo', + "Prints out all of the message's text in an internal message.", + (args) => { + internalMessage(args.text[0].text); + console.log(args); + }, + [ + { + type: 3, + name: 'text', + description: 'Text to be printed in an internal message.', + required: true, + }, + ], + ); + + commands.add( + 'snowflake', + 'Returns info about a given snowflake.', + (args) => { + // Check if the supplied argument is actually a snowflake or not + if (!/\d{1,20}/.test(args.snowflake[0].text)) { + internalMessage('The provided argument is not a snowflake.'); + } else { + const binary = idToBinary(args.snowflake[0].text).toString(2).padStart(64, '0'); + + const res = { + timestamp: parseInt(binary.substring(0, 42), 2) + discordEpoch, + workerID: parseInt(binary.substring(42, 47), 2), + processID: parseInt(binary.substring(47, 52), 2), + increment: parseInt(binary.substring(52, 64), 2), + binary: binary, + }; + + internalMessage( + `Snowflake info for \`${args.snowflake[0].text}\`\n\n` + + `**Timestamp**: ${new Date(res.timestamp).toUTCString()} (${res.timestamp})\n` + + `**Worker ID**: ${res.workerID}\n` + + `**Process ID**: ${res.processID}\n` + + `**Increment**: ${res.increment}\n` + + `**Binary**: ${res.binary}\n`, + ); + } + }, + [ + { + type: 3, + name: 'snowflake', + description: 'Snowflake to decrypt.', + required: true, + }, + ], + ); + }, + onRemove: () => { + commands.remove('echo'); + commands.remove('snowflake'); + }, + }, +};