mirror of
https://github.com/projectdysnomia/dysnomia.git
synced 2026-12-02 13:48:31 +02:00
Updates ESLint to v9, and adds an `eqeqeq` rule to enforce strict equality usage across the codebase (`== null` is still allowed). Additionally this disables prototype builtins in favor of Object.hasOwn.
88 lines
2.4 KiB
JavaScript
88 lines
2.4 KiB
JavaScript
"use strict";
|
|
|
|
const util = require("node:util");
|
|
|
|
/**
|
|
* Provides utilities for working with many Discord structures
|
|
*/
|
|
class Base {
|
|
constructor(id) {
|
|
if(id) {
|
|
/**
|
|
* A Discord snowflake identifying the object
|
|
* @type {String}
|
|
*/
|
|
this.id = id;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Timestamp of structure creation
|
|
* @type {Number}
|
|
*/
|
|
get createdAt() {
|
|
return Base.getCreatedAt(this.id);
|
|
}
|
|
|
|
/**
|
|
* Calculates the timestamp in milliseconds associated with a Discord ID/snowflake
|
|
* @param {String} id The ID of a structure
|
|
* @returns {Number}
|
|
*/
|
|
static getCreatedAt(id) {
|
|
return Base.getDiscordEpoch(id) + 1420070400000;
|
|
}
|
|
|
|
/**
|
|
* Gets the number of milliseconds since epoch represented by an ID/snowflake
|
|
* @param {String} id The ID of a structure
|
|
* @returns {Number}
|
|
*/
|
|
static getDiscordEpoch(id) {
|
|
return Math.floor(id / 4194304);
|
|
}
|
|
|
|
[util.inspect.custom]() {
|
|
// http://stackoverflow.com/questions/5905492/dynamic-function-name-in-javascript
|
|
const copy = new {[this.constructor.name]: class {}}[this.constructor.name]();
|
|
for(const key in this) {
|
|
if(Object.hasOwn(this, key) && !key.startsWith("_") && this[key] !== undefined) {
|
|
copy[key] = this[key];
|
|
}
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
toString() {
|
|
return `[${this.constructor.name} ${this.id}]`;
|
|
}
|
|
|
|
toJSON(props = []) {
|
|
const json = {};
|
|
if(this.id) {
|
|
json.id = this.id;
|
|
json.createdAt = this.createdAt;
|
|
}
|
|
for(const prop of props) {
|
|
const value = this[prop];
|
|
const type = typeof value;
|
|
if(value === undefined) {
|
|
continue;
|
|
} else if((type !== "object" && type !== "function" && type !== "bigint") || value === null) {
|
|
json[prop] = value;
|
|
} else if(value.toJSON !== undefined) {
|
|
json[prop] = value.toJSON();
|
|
} else if(value.values !== undefined) {
|
|
json[prop] = [...value.values()];
|
|
} else if(type === "bigint") {
|
|
json[prop] = value.toString();
|
|
} else if(type === "object") {
|
|
json[prop] = value;
|
|
}
|
|
}
|
|
return json;
|
|
}
|
|
}
|
|
|
|
module.exports = Base;
|