feat: initial Papo bot scaffold
This commit is contained in:
commit
000481a3b0
12168 files changed
+1584750
No files matched your search
+203
@@ -0,0 +1,203 @@
|
||||
'use strict';
|
||||
|
||||
var __defProp = Object.defineProperty;
|
||||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
||||
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
||||
|
||||
// src/lib/Snowflake.ts
|
||||
var IncrementSymbol = Symbol("@sapphire/snowflake.increment");
|
||||
var EpochSymbol = Symbol("@sapphire/snowflake.epoch");
|
||||
var EpochNumberSymbol = Symbol("@sapphire/snowflake.epoch.number");
|
||||
var ProcessIdSymbol = Symbol("@sapphire/snowflake.processId");
|
||||
var WorkerIdSymbol = Symbol("@sapphire/snowflake.workerId");
|
||||
var MaximumWorkerId = 0b11111n;
|
||||
var MaximumProcessId = 0b11111n;
|
||||
var MaximumIncrement = 0b111111111111n;
|
||||
var TimestampFieldDivisor = 2 ** 22;
|
||||
var _a, _b, _c, _d, _e;
|
||||
_e = EpochSymbol, _d = EpochNumberSymbol, _c = IncrementSymbol, _b = ProcessIdSymbol, _a = WorkerIdSymbol;
|
||||
var _Snowflake = class _Snowflake {
|
||||
/**
|
||||
* @param epoch the epoch to use
|
||||
*/
|
||||
constructor(epoch) {
|
||||
/**
|
||||
* Alias for {@link deconstruct}
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
__publicField(this, "decode", this.deconstruct);
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor
|
||||
* @internal
|
||||
*/
|
||||
__publicField(this, _e);
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor as a number
|
||||
* @internal
|
||||
*/
|
||||
__publicField(this, _d);
|
||||
/**
|
||||
* Internal incrementor for generating snowflakes
|
||||
* @internal
|
||||
*/
|
||||
__publicField(this, _c, 0n);
|
||||
/**
|
||||
* The process ID that will be used by default in the generate method
|
||||
* @internal
|
||||
*/
|
||||
__publicField(this, _b, 1n);
|
||||
/**
|
||||
* The worker ID that will be used by default in the generate method
|
||||
* @internal
|
||||
*/
|
||||
__publicField(this, _a, 0n);
|
||||
this[EpochSymbol] = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);
|
||||
this[EpochNumberSymbol] = Number(this[EpochSymbol]);
|
||||
}
|
||||
/**
|
||||
* The epoch for this snowflake, as a bigint
|
||||
*/
|
||||
get epoch() {
|
||||
return this[EpochSymbol];
|
||||
}
|
||||
/**
|
||||
* The epoch for this snowflake, as a number
|
||||
*/
|
||||
get epochNumber() {
|
||||
return this[EpochNumberSymbol];
|
||||
}
|
||||
/**
|
||||
* Gets the configured process ID
|
||||
*/
|
||||
get processId() {
|
||||
return this[ProcessIdSymbol];
|
||||
}
|
||||
/**
|
||||
* Sets the process ID that will be used by default for the {@link generate} method
|
||||
* @param value The new value, will be coerced to BigInt and masked with `0b11111n`
|
||||
*/
|
||||
set processId(value) {
|
||||
this[ProcessIdSymbol] = BigInt(value) & MaximumProcessId;
|
||||
}
|
||||
/**
|
||||
* Gets the configured worker ID
|
||||
*/
|
||||
get workerId() {
|
||||
return this[WorkerIdSymbol];
|
||||
}
|
||||
/**
|
||||
* Sets the worker ID that will be used by default for the {@link generate} method
|
||||
* @param value The new value, will be coerced to BigInt and masked with `0b11111n`
|
||||
*/
|
||||
set workerId(value) {
|
||||
this[WorkerIdSymbol] = BigInt(value) & MaximumWorkerId;
|
||||
}
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when `increment` is not provided it defaults to the private `increment` of the instance
|
||||
* @example
|
||||
* ```typescript
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
generate({
|
||||
increment,
|
||||
timestamp = Date.now(),
|
||||
workerId = this[WorkerIdSymbol],
|
||||
processId = this[ProcessIdSymbol]
|
||||
} = {}) {
|
||||
if (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());
|
||||
else if (typeof timestamp === "number") timestamp = BigInt(timestamp);
|
||||
else if (typeof timestamp !== "bigint") {
|
||||
throw new TypeError(`"timestamp" argument must be a number, bigint, or Date (received ${typeof timestamp})`);
|
||||
}
|
||||
if (typeof increment !== "bigint") {
|
||||
increment = this[IncrementSymbol];
|
||||
this[IncrementSymbol] = increment + 1n & MaximumIncrement;
|
||||
}
|
||||
return timestamp - this[EpochSymbol] << 22n | (workerId & MaximumWorkerId) << 17n | (processId & MaximumProcessId) << 12n | increment & MaximumIncrement;
|
||||
}
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```typescript
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
deconstruct(id) {
|
||||
const bigIntId = BigInt(id);
|
||||
const epoch = this[EpochSymbol];
|
||||
return {
|
||||
id: bigIntId,
|
||||
timestamp: (bigIntId >> 22n) + epoch,
|
||||
workerId: bigIntId >> 17n & MaximumWorkerId,
|
||||
processId: bigIntId >> 12n & MaximumProcessId,
|
||||
increment: bigIntId & MaximumIncrement,
|
||||
epoch
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Retrieves the timestamp field's value from a snowflake.
|
||||
* @param id The snowflake to get the timestamp value from.
|
||||
* @returns The UNIX timestamp that is stored in `id`.
|
||||
*/
|
||||
timestampFrom(id) {
|
||||
return Math.floor(Number(id) / TimestampFieldDivisor) + this[EpochNumberSymbol];
|
||||
}
|
||||
/**
|
||||
* Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
|
||||
* snowflake in sort order.
|
||||
* @param a The first snowflake to compare.
|
||||
* @param b The second snowflake to compare.
|
||||
* @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
|
||||
* @example Sort snowflakes in ascending order
|
||||
* ```typescript
|
||||
* const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
|
||||
* console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
|
||||
* // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
|
||||
* ```
|
||||
* @example Sort snowflakes in descending order
|
||||
* ```typescript
|
||||
* const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
|
||||
* console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
|
||||
* // → ['1056191128120082432', '737141877803057244', '254360814063058944'];
|
||||
* ```
|
||||
*/
|
||||
static compare(a, b) {
|
||||
const typeA = typeof a;
|
||||
return typeA === typeof b ? typeA === "string" ? cmpString(a, b) : cmpBigInt(a, b) : cmpBigInt(BigInt(a), BigInt(b));
|
||||
}
|
||||
};
|
||||
__name(_Snowflake, "Snowflake");
|
||||
var Snowflake = _Snowflake;
|
||||
function cmpBigInt(a, b) {
|
||||
return a === b ? 0 : a < b ? -1 : 1;
|
||||
}
|
||||
__name(cmpBigInt, "cmpBigInt");
|
||||
function cmpString(a, b) {
|
||||
return a === b ? 0 : a.length < b.length ? -1 : a.length > b.length ? 1 : a < b ? -1 : 1;
|
||||
}
|
||||
__name(cmpString, "cmpString");
|
||||
|
||||
// src/lib/DiscordSnowflake.ts
|
||||
var DiscordSnowflake = new Snowflake(1420070400000n);
|
||||
|
||||
// src/lib/TwitterSnowflake.ts
|
||||
var TwitterSnowflake = new Snowflake(1288834974657n);
|
||||
|
||||
exports.DiscordSnowflake = DiscordSnowflake;
|
||||
exports.MaximumIncrement = MaximumIncrement;
|
||||
exports.MaximumProcessId = MaximumProcessId;
|
||||
exports.MaximumWorkerId = MaximumWorkerId;
|
||||
exports.Snowflake = Snowflake;
|
||||
exports.TwitterSnowflake = TwitterSnowflake;
|
||||
//# sourceMappingURL=index.cjs.map
|
||||
//# sourceMappingURL=index.cjs.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../src/lib/Snowflake.ts","../../src/lib/DiscordSnowflake.ts","../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":";;;;;;;;AAAA,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;AAC9D,IAAM,WAAA,GAAc,OAAO,2BAA2B,CAAA;AACtD,IAAM,iBAAA,GAAoB,OAAO,kCAAkC,CAAA;AACnE,IAAM,eAAA,GAAkB,OAAO,+BAA+B,CAAA;AAC9D,IAAM,cAAA,GAAiB,OAAO,8BAA8B,CAAA;AAKrD,IAAM,eAAkB,GAAA;AAKxB,IAAM,gBAAmB,GAAA;AAKzB,IAAM,gBAAmB,GAAA;AAEhC,IAAM,wBAAwB,CAAK,IAAA,EAAA;AArBnC,IAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA,EAAA;AA+CmB,EAMA,GAAA,WAAA,EAAA,EAAA,GAAA,iBAAA,EAMT,sBAMA,EAMA,GAAA,eAAA,EAAA,EAAA,GAAA,cAAA;AAnCH,IAAM,UAAA,GAAN,MAAM,UAAU,CAAA;AAAA;AAAA;AAAA;AAAA,EAwCf,YAAY,KAA+B,EAAA;AAnClD;AAAA;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,UAAS,IAAK,CAAA,WAAA,CAAA;AAMrB;AAAA;AAAA;AAAA;AAAA,IAAkB,aAAA,CAAA,IAAA,EAAA,EAAA,CAAA;AAMlB;AAAA;AAAA;AAAA;AAAA,IAAkB,aAAA,CAAA,IAAA,EAAA,EAAA,CAAA;AAMlB;AAAA;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAS,EAAmB,EAAA,EAAA,CAAA;AAM5B;AAAA;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,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nconst EpochSymbol = Symbol('@sapphire/snowflake.epoch');\nconst EpochNumberSymbol = Symbol('@sapphire/snowflake.epoch.number');\nconst ProcessIdSymbol = Symbol('@sapphire/snowflake.processId');\nconst WorkerIdSymbol = Symbol('@sapphire/snowflake.workerId');\n\n/**\n * The maximum value the `workerId` field accepts in snowflakes.\n */\nexport const MaximumWorkerId = 0b11111n;\n\n/**\n * The maximum value the `processId` field accepts in snowflakes.\n */\nexport const MaximumProcessId = 0b11111n;\n\n/**\n * The maximum value the `increment` field accepts in snowflakes.\n */\nexport const MaximumIncrement = 0b111111111111n;\n\nconst TimestampFieldDivisor = 2 ** 22;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigLine truncated
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
declare const IncrementSymbol: unique symbol;
|
||||
declare const EpochSymbol: unique symbol;
|
||||
declare const EpochNumberSymbol: unique symbol;
|
||||
declare const ProcessIdSymbol: unique symbol;
|
||||
declare const WorkerIdSymbol: unique symbol;
|
||||
/**
|
||||
* The maximum value the `workerId` field accepts in snowflakes.
|
||||
*/
|
||||
declare const MaximumWorkerId = 31n;
|
||||
/**
|
||||
* The maximum value the `processId` field accepts in snowflakes.
|
||||
*/
|
||||
declare const MaximumProcessId = 31n;
|
||||
/**
|
||||
* The maximum value the `increment` field accepts in snowflakes.
|
||||
*/
|
||||
declare const MaximumIncrement = 4095n;
|
||||
/**
|
||||
* A class for generating and deconstructing Twitter snowflakes.
|
||||
*
|
||||
* A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}
|
||||
* is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.
|
||||
*
|
||||
* If we have a snowflake `266241948824764416` we can represent it as binary:
|
||||
* ```
|
||||
* 64 22 17 12 0
|
||||
* 000000111011000111100001101001000101000000 00001 00000 000000000000
|
||||
* number of ms since epoch worker pid increment
|
||||
* ```
|
||||
*/
|
||||
declare class Snowflake {
|
||||
/**
|
||||
* Alias for {@link deconstruct}
|
||||
*/
|
||||
decode: (id: string | bigint) => DeconstructedSnowflake;
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor
|
||||
* @internal
|
||||
*/
|
||||
private readonly [EpochSymbol];
|
||||
/**
|
||||
* Internal reference of the epoch passed in the constructor as a number
|
||||
* @internal
|
||||
*/
|
||||
private readonly [EpochNumberSymbol];
|
||||
/**
|
||||
* Internal incrementor for generating snowflakes
|
||||
* @internal
|
||||
*/
|
||||
private [IncrementSymbol];
|
||||
/**
|
||||
* The process ID that will be used by default in the generate method
|
||||
* @internal
|
||||
*/
|
||||
private [ProcessIdSymbol];
|
||||
/**
|
||||
* The worker ID that will be used by default in the generate method
|
||||
* @internal
|
||||
*/
|
||||
private [WorkerIdSymbol];
|
||||
/**
|
||||
* @param epoch the epoch to use
|
||||
*/
|
||||
constructor(epoch: number | bigint | Date);
|
||||
/**
|
||||
* The epoch for this snowflake, as a bigint
|
||||
*/
|
||||
get epoch(): bigint;
|
||||
/**
|
||||
* The epoch for this snowflake, as a number
|
||||
*/
|
||||
get epochNumber(): number;
|
||||
/**
|
||||
* Gets the configured process ID
|
||||
*/
|
||||
get processId(): bigint;
|
||||
/**
|
||||
* Sets the process ID that will be used by default for the {@link generate} method
|
||||
* @param value The new value, will be coerced to BigInt and masked with `0b11111n`
|
||||
*/
|
||||
set processId(value: number | bigint);
|
||||
/**
|
||||
* Gets the configured worker ID
|
||||
*/
|
||||
get workerId(): bigint;
|
||||
/**
|
||||
* Sets the worker ID that will be used by default for the {@link generate} method
|
||||
* @param value The new value, will be coerced to BigInt and masked with `0b11111n`
|
||||
*/
|
||||
set workerId(value: number | bigint);
|
||||
/**
|
||||
* Generates a snowflake given an epoch and optionally a timestamp
|
||||
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
|
||||
*
|
||||
* **note** when `increment` is not provided it defaults to the private `increment` of the instance
|
||||
* @example
|
||||
* ```typescript
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).generate();
|
||||
* ```
|
||||
* @returns A unique snowflake
|
||||
*/
|
||||
generate({ increment, timestamp, workerId, processId }?: SnowflakeGenerateOptions): bigint;
|
||||
/**
|
||||
* Deconstructs a snowflake given a snowflake ID
|
||||
* @param id the snowflake to deconstruct
|
||||
* @returns a deconstructed snowflake
|
||||
* @example
|
||||
* ```typescript
|
||||
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
||||
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
||||
* ```
|
||||
*/
|
||||
deconstruct(id: string | bigint): DeconstructedSnowflake;
|
||||
/**
|
||||
* Retrieves the timestamp field's value from a snowflake.
|
||||
* @param id The snowflake to get the timestamp value from.
|
||||
* @returns The UNIX timestamp that is stored in `id`.
|
||||
*/
|
||||
timestampFrom(id: string | bigint): number;
|
||||
/**
|
||||
* Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
|
||||
* snowflake in sort order.
|
||||
* @param a The first snowflake to compare.
|
||||
* @param b The second snowflake to compare.
|
||||
* @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
|
||||
* @example Sort snowflakes in ascending order
|
||||
* ```typescript
|
||||
* const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
|
||||
* console.log(ids.sort((a, b) => Snowflake.compare(a, b)));
|
||||
* // → ['254360814063058944', '737141877803057244', '1056191128120082432'];
|
||||
* ```
|
||||
* @example Sort snowflakes in descending order
|
||||
* ```typescript
|
||||
* const ids = ['737141877803057244', '1056191128120082432', '254360814063058944'];
|
||||
* console.log(ids.sort((a, b) => -Snowflake.compare(a, b)));
|
||||
* // → ['1056191128120082432', '737141877803057244', '254360814063058944'];
|
||||
* ```
|
||||
*/
|
||||
static compare(a: string | bigint, b: string | bigint): -1 | 0 | 1;
|
||||
}
|
||||
/**
|
||||
* Options for Snowflake#generate
|
||||
*/
|
||||
interface SnowflakeGenerateOptions {
|
||||
/**
|
||||
* Timestamp or date of the snowflake to generate
|
||||
* @default Date.now()
|
||||
*/
|
||||
timestamp?: number | bigint | Date;
|
||||
/**
|
||||
* The increment to use
|
||||
* @default 0n
|
||||
* @remark keep in mind that this bigint is auto-incremented between generate calls
|
||||
*/
|
||||
increment?: bigint;
|
||||
/**
|
||||
* The worker ID to use, will be truncated to 5 bits (0-31)
|
||||
* @default 0n
|
||||
*/
|
||||
workerId?: bigint;
|
||||
/**
|
||||
* The process ID to use, will be truncated to 5 bits (0-31)
|
||||
* @default 1n
|
||||
*/
|
||||
processId?: bigint;
|
||||
}
|
||||
/**
|
||||
* Object returned by Snowflake#deconstruct
|
||||
*/
|
||||
interface DeconstructedSnowflake {
|
||||
/**
|
||||
* The id in BigInt form
|
||||
*/
|
||||
id: bigint;
|
||||
/**
|
||||
* The timestamp stored in the snowflake
|
||||
*/
|
||||
timestamp: bigint;
|
||||
/**
|
||||
* The worker id stored in the snowflake
|
||||
*/
|
||||
workerId: bigint;
|
||||
/**
|
||||
* The process id stored in the snowflake
|
||||
*/
|
||||
processId: bigint;
|
||||
/**
|
||||
* The increment stored in the snowflake
|
||||
*/
|
||||
increment: bigint;
|
||||
/**
|
||||
* The epoch to use in the snowflake
|
||||
*/
|
||||
epoch: bigint;
|
||||
}
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Discord's snowflake epoch
|
||||
*
|
||||
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
|
||||
*/
|
||||
declare const DiscordSnowflake: Snowflake;
|
||||
|
||||
/**
|
||||
* A class for parsing snowflake ids using Twitter's snowflake epoch
|
||||
*
|
||||
* Which is 2010-11-04 at 01:42:54.657 UTC+0, found in the archived snowflake repository {@linkplain https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala#L25}
|
||||
*/
|
||||
declare const TwitterSnowflake: Snowflake;
|
||||
|
||||
export { type DeconstructedSnowflake, DiscordSnowflake, MaximumIncrement, MaximumProcessId, MaximumWorkerId, Snowflake, type SnowflakeGenerateOptions, TwitterSnowflake };
|
||||
Reference in new issue
Block a user