From 5e86d7ab9c5200d794c3adb2b422d20a2aefd2ce Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 13 Aug 2022 02:00:50 +0200 Subject: restructure to single project --- src/api/util/utility/RandomInviteID.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/api/util/utility/RandomInviteID.ts (limited to 'src/api/util/utility/RandomInviteID.ts') diff --git a/src/api/util/utility/RandomInviteID.ts b/src/api/util/utility/RandomInviteID.ts new file mode 100644 index 00000000..7ea344e0 --- /dev/null +++ b/src/api/util/utility/RandomInviteID.ts @@ -0,0 +1,32 @@ +import { Snowflake } from "@fosscord/util"; + +export function random(length = 6) { + // Declare all characters + let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + // Pick characers randomly + let str = ""; + for (let i = 0; i < length; i++) { + str += chars.charAt(Math.floor(Math.random() * chars.length)); + } + + return str; +} + +export function snowflakeBasedInvite() { + // Declare all characters + let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let base = BigInt(chars.length); + let snowflake = Snowflake.generateWorkerProcess(); + + // snowflakes hold ~10.75 characters worth of entropy; + // safe to generate a 8-char invite out of them + let str = ""; + for (let i=0; i < 10; i++) { + + str.concat(chars.charAt(Number(snowflake % base))); + snowflake = snowflake / base; + } + + return str.substr(3,8).split("").reverse().join(""); +} -- cgit 1.5.1