diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-01-30 23:55:32 +0100 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-01-30 23:55:32 +0100 |
commit | d2795bdd0a599cffefa26e93f4acbe5f3554b120 (patch) | |
tree | 3c8c3e6bcb555e79ab7e5f5e92c6ca51412d7279 | |
parent | Merge branch 'master' of https://github.com/discord-open-source/discord-server (diff) | |
download | server-d2795bdd0a599cffefa26e93f4acbe5f3554b120.tar.xz |
:zap: Convert BitField to BigInt
-rw-r--r-- | src/middlewares/RateLimit.ts | 2 | ||||
-rw-r--r-- | src/models/User.ts | 15 | ||||
-rw-r--r-- | src/util/BitField.ts | 34 | ||||
-rw-r--r-- | src/util/MessageFlags.ts | 10 | ||||
-rw-r--r-- | src/util/Permissions.ts | 62 | ||||
-rw-r--r-- | src/util/UserFlags.ts | 26 |
6 files changed, 78 insertions, 71 deletions
diff --git a/src/middlewares/RateLimit.ts b/src/middlewares/RateLimit.ts index abfc1c3d..6c4a1080 100644 --- a/src/middlewares/RateLimit.ts +++ b/src/middlewares/RateLimit.ts @@ -4,7 +4,7 @@ import { getIpAdress } from "./GlobalRateLimit"; export function RateLimit({ count = 10, timespan = 1000 * 5, name = "/" }) { return async (req: Request, res: Response, next: NextFunction) => { - let id = req.userid || getIpAdress(req); // TODO: .replaceAll(".", "_"); // for ip adress replace all dots to save in database + let id = req.userid || getIpAdress(req); const limit: { count: number; start: number } = (await db.data.ratelimit.routes[name][id].get()) || { count: 0, diff --git a/src/models/User.ts b/src/models/User.ts new file mode 100644 index 00000000..24184c57 --- /dev/null +++ b/src/models/User.ts @@ -0,0 +1,15 @@ +import { UserFlags } from "../util/UserFlags"; + +export interface User { + id: BigInt; + username: string; + discriminator: string; + avatar: string; + bot: boolean; + system: boolean; + mfa_enabled: boolean; + locale: string; + verified: boolean; + email: string; + flags: UserFlags; +} diff --git a/src/util/BitField.ts b/src/util/BitField.ts index 01349a0b..17eef796 100644 --- a/src/util/BitField.ts +++ b/src/util/BitField.ts @@ -3,26 +3,17 @@ // https://github.com/discordjs/discord.js/blob/master/src/util/BitField.js // Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah -export type BitFieldResolvable = number | BitField | string | BitFieldResolvable[]; +export type BitFieldResolvable = number | BigInt | BitField | string | BitFieldResolvable[]; /** * Data structure that makes it easy to interact with a bitfield. */ export class BitField { - public bitfield: number; + public bitfield: bigint = BigInt(0); + + public static FLAGS: Record<string, bigint>; - /** - * Numeric bitfield flags. - * <info>Defined in extension classes</info> - */ - public static FLAGS: Record<string, number>; - /** - */ constructor(bits: BitFieldResolvable = 0) { - /** - * Bitfield of the packed bits - * @type {number} - */ this.bitfield = BitField.resolve(bits); } @@ -30,7 +21,7 @@ export class BitField { * Checks whether the bitfield has a bit, or any of multiple bits. */ any(bit: BitFieldResolvable): boolean { - return (this.bitfield & BitField.resolve(bit)) !== 0; + return (this.bitfield & BitField.resolve(bit)) !== 0n; } /** @@ -45,8 +36,8 @@ export class BitField { */ has(bit: BitFieldResolvable): boolean { if (Array.isArray(bit)) return bit.every((p) => this.has(p)); - bit = BitField.resolve(bit); - return (this.bitfield & bit) === bit; + const BIT = BitField.resolve(bit); + return (this.bitfield & BIT) === BIT; } /** @@ -70,7 +61,7 @@ export class BitField { * @returns {BitField} These bits or new BitField if the instance is frozen. */ add(...bits: BitFieldResolvable[]): BitField { - let total = 0; + let total = 0n; for (const bit of bits) { total |= BitField.resolve(bit); } @@ -84,7 +75,7 @@ export class BitField { * @param {...BitFieldResolvable} [bits] Bits to remove */ remove(...bits: BitFieldResolvable[]) { - let total = 0; + let total = 0n; for (const bit of bits) { total |= BitField.resolve(bit); } @@ -136,10 +127,11 @@ export class BitField { * @param {BitFieldResolvable} [bit=0] - bit(s) to resolve * @returns {number} */ - static resolve(bit: BitFieldResolvable = 0): number { - if (typeof bit === "number" && bit >= 0) return bit; + static resolve(bit: BitFieldResolvable = 0n): bigint { + if ((typeof bit === "number" || typeof bit === "bigint") && bit >= 0n) return BigInt(bit); if (bit instanceof BitField) return bit.bitfield; - if (Array.isArray(bit)) return bit.map((p) => this.resolve(p)).reduce((prev, p) => prev | p, 0); + if (Array.isArray(bit)) + return bit.map((p) => this.resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n); if (typeof bit === "string" && typeof this.FLAGS[bit] !== "undefined") return this.FLAGS[bit]; throw new RangeError("BITFIELD_INVALID: " + bit); } diff --git a/src/util/MessageFlags.ts b/src/util/MessageFlags.ts index 381b460e..d3e6a07a 100644 --- a/src/util/MessageFlags.ts +++ b/src/util/MessageFlags.ts @@ -5,10 +5,10 @@ import { BitField } from "./BitField"; export class MessageFlags extends BitField { static FLAGS = { - CROSSPOSTED: 1 << 0, - IS_CROSSPOST: 1 << 1, - SUPPRESS_EMBEDS: 1 << 2, - SOURCE_MESSAGE_DELETED: 1 << 3, - URGENT: 1 << 4, + CROSSPOSTED: 1n << 0n, + IS_CROSSPOST: 1n << 1n, + SUPPRESS_EMBEDS: 1n << 2n, + SOURCE_MESSAGE_DELETED: 1n << 3n, + URGENT: 1n << 4n, }; } diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts index ff4e0f4e..f076e0c2 100644 --- a/src/util/Permissions.ts +++ b/src/util/Permissions.ts @@ -7,37 +7,37 @@ export type PermissionResolvable = string | number | Permissions | PermissionRes export class Permissions extends BitField { static FLAGS = { - CREATE_INSTANT_INVITE: 1 << 0, - KICK_MEMBERS: 1 << 1, - BAN_MEMBERS: 1 << 2, - ADMINISTRATOR: 1 << 3, - MANAGE_CHANNELS: 1 << 4, - MANAGE_GUILD: 1 << 5, - ADD_REACTIONS: 1 << 6, - VIEW_AUDIT_LOG: 1 << 7, - PRIORITY_SPEAKER: 1 << 8, - STREAM: 1 << 9, - VIEW_CHANNEL: 1 << 10, - SEND_MESSAGES: 1 << 11, - SEND_TTS_MESSAGES: 1 << 12, - MANAGE_MESSAGES: 1 << 13, - EMBED_LINKS: 1 << 14, - ATTACH_FILES: 1 << 15, - READ_MESSAGE_HISTORY: 1 << 16, - MENTION_EVERYONE: 1 << 17, - USE_EXTERNAL_EMOJIS: 1 << 18, - VIEW_GUILD_INSIGHTS: 1 << 19, - CONNECT: 1 << 20, - SPEAK: 1 << 21, - MUTE_MEMBERS: 1 << 22, - DEAFEN_MEMBERS: 1 << 23, - MOVE_MEMBERS: 1 << 24, - USE_VAD: 1 << 25, - CHANGE_NICKNAME: 1 << 26, - MANAGE_NICKNAMES: 1 << 27, - MANAGE_ROLES: 1 << 28, - MANAGE_WEBHOOKS: 1 << 29, - MANAGE_EMOJIS: 1 << 30, + CREATE_INSTANT_INVITE: 1n << 0n, + KICK_MEMBERS: 1n << 1n, + BAN_MEMBERS: 1n << 2n, + ADMINISTRATOR: 1n << 3n, + MANAGE_CHANNELS: 1n << 4n, + MANAGE_GUILD: 1n << 5n, + ADD_REACTIONS: 1n << 6n, + VIEW_AUDIT_LOG: 1n << 7n, + PRIORITY_SPEAKER: 1n << 8n, + STREAM: 1n << 9n, + VIEW_CHANNEL: 1n << 10n, + SEND_MESSAGES: 1n << 11n, + SEND_TTS_MESSAGES: 1n << 12n, + MANAGE_MESSAGES: 1n << 13n, + EMBED_LINKS: 1n << 14n, + ATTACH_FILES: 1n << 15n, + READ_MESSAGE_HISTORY: 1n << 16n, + MENTION_EVERYONE: 1n << 17n, + USE_EXTERNAL_EMOJIS: 1n << 18n, + VIEW_GUILD_INSIGHTS: 1n << 19n, + CONNECT: 1n << 20n, + SPEAK: 1n << 21n, + MUTE_MEMBERS: 1n << 22n, + DEAFEN_MEMBERS: 1n << 23n, + MOVE_MEMBERS: 1n << 24n, + USE_VAD: 1n << 25n, + CHANGE_NICKNAME: 1n << 26n, + MANAGE_NICKNAMES: 1n << 27n, + MANAGE_ROLES: 1n << 28n, + MANAGE_WEBHOOKS: 1n << 29n, + MANAGE_EMOJIS: 1n << 30n, }; any(permission: PermissionResolvable, checkAdmin = true) { diff --git a/src/util/UserFlags.ts b/src/util/UserFlags.ts index 44486cb0..6e532f93 100644 --- a/src/util/UserFlags.ts +++ b/src/util/UserFlags.ts @@ -5,18 +5,18 @@ import { BitField } from "./BitField"; export class UserFlags extends BitField { static FLAGS = { - DISCORD_EMPLOYEE: 1 << 0, - PARTNERED_SERVER_OWNER: 1 << 1, - HYPESQUAD_EVENTS: 1 << 2, - BUGHUNTER_LEVEL_1: 1 << 3, - HOUSE_BRAVERY: 1 << 6, - HOUSE_BRILLIANCE: 1 << 7, - HOUSE_BALANCE: 1 << 8, - EARLY_SUPPORTER: 1 << 9, - TEAM_USER: 1 << 10, - SYSTEM: 1 << 12, - BUGHUNTER_LEVEL_2: 1 << 14, - VERIFIED_BOT: 1 << 16, - EARLY_VERIFIED_BOT_DEVELOPER: 1 << 17, + DISCORD_EMPLOYEE: 1n << 0n, + PARTNERED_SERVER_OWNER: 1n << 1n, + HYPESQUAD_EVENTS: 1n << 2n, + BUGHUNTER_LEVEL_1: 1n << 3n, + HOUSE_BRAVERY: 1n << 6n, + HOUSE_BRILLIANCE: 1n << 7n, + HOUSE_BALANCE: 1n << 8n, + EARLY_SUPPORTER: 1n << 9n, + TEAM_USER: 1n << 10n, + SYSTEM: 1n << 12n, + BUGHUNTER_LEVEL_2: 1n << 14n, + VERIFIED_BOT: 1n << 16n, + EARLY_VERIFIED_BOT_DEVELOPER: 1n << 17n, }; } |