summary refs log tree commit diff
path: root/src/api/util/utility/RandomInviteID.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 22:12:00 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 22:12:00 +1000
commitafefa5d64bd6cde7d6efa3a9a5a3ec67a6ca29a8 (patch)
tree07779150eba77c27bf75bc0c7890f4a3f976716e /src/api/util/utility/RandomInviteID.ts
parentremoved char joiners as they are actually useful, added page break (diff)
parentMerge remote-tracking branch 'Puyodead1/patch/prettier-config' into staging (diff)
downloadserver-afefa5d64bd6cde7d6efa3a9a5a3ec67a6ca29a8.tar.xz
Merge remote-tracking branch 'upstream/staging' into fix/categoryNames
Diffstat (limited to 'src/api/util/utility/RandomInviteID.ts')
-rw-r--r--src/api/util/utility/RandomInviteID.ts32
1 files changed, 32 insertions, 0 deletions
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(""); +}