summary refs log tree commit diff
path: root/api/src/util/utility/RandomInviteID.ts
diff options
context:
space:
mode:
authorThesourtimes <cckhmck@gmail.com>2022-01-01 20:04:30 +0300
committerThesourtimes <cckhmck@gmail.com>2022-01-01 20:04:30 +0300
commit2b6e6ea0256325dc6a1097b9f0cfb2928feb12aa (patch)
tree7305f9762093fbdd5835e60d6080233413df64d0 /api/src/util/utility/RandomInviteID.ts
parentFix game statusses (diff)
downloadserver-2b6e6ea0256325dc6a1097b9f0cfb2928feb12aa.tar.xz
Organize @fosscord/api utils in directories
Diffstat (limited to 'api/src/util/utility/RandomInviteID.ts')
-rw-r--r--api/src/util/utility/RandomInviteID.ts32
1 files changed, 32 insertions, 0 deletions
diff --git a/api/src/util/utility/RandomInviteID.ts b/api/src/util/utility/RandomInviteID.ts
new file mode 100644

index 00000000..7ea344e0 --- /dev/null +++ b/api/src/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(""); +}