simply-xp 1.3.7 → 2.0.0-beta.0-fix.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FUNDING.yml +2 -2
- package/README.md +73 -43
- package/lib/src/add.d.ts +35 -0
- package/lib/src/add.js +23 -0
- package/lib/src/cards.d.ts +160 -0
- package/lib/src/cards.js +35 -0
- package/lib/src/charts.d.ts +28 -0
- package/lib/src/charts.js +10 -0
- package/lib/src/connect.d.ts +29 -0
- package/lib/src/connect.js +43 -0
- package/lib/src/create.d.ts +12 -0
- package/lib/src/create.js +12 -0
- package/lib/src/fetch.d.ts +12 -0
- package/lib/src/fetch.js +11 -0
- package/lib/src/fonts/Baloo2-ExtraBold.woff2 +0 -0
- package/lib/src/functions/database.d.ts +164 -0
- package/lib/src/functions/database.js +78 -0
- package/lib/src/functions/https.d.ts +26 -0
- package/lib/src/functions/https.js +15 -0
- package/lib/src/functions/utilities.d.ts +82 -0
- package/lib/src/functions/utilities.js +43 -0
- package/lib/src/functions/xplogs.d.ts +81 -0
- package/lib/src/functions/xplogs.js +15 -0
- package/lib/src/leaderboard.d.ts +11 -0
- package/lib/src/leaderboard.js +10 -0
- package/lib/src/migrate.d.ts +27 -0
- package/lib/src/migrate.js +19 -0
- package/lib/src/remove.d.ts +29 -0
- package/lib/src/remove.js +23 -0
- package/lib/src/reset.d.ts +12 -0
- package/lib/src/reset.js +12 -0
- package/lib/src/roleSetup.d.ts +78 -0
- package/lib/src/roleSetup.js +47 -0
- package/lib/src/set.d.ts +26 -0
- package/lib/src/set.js +23 -0
- package/lib/xp.d.ts +62 -0
- package/lib/xp.js +56 -0
- package/package.json +49 -35
- package/index.d.ts +0 -108
- package/simplyxp.js +0 -33
- package/src/Fonts/Baloo-Regular.ttf +0 -0
- package/src/addLevel.js +0 -66
- package/src/addXP.js +0 -104
- package/src/charts.js +0 -92
- package/src/connect.js +0 -21
- package/src/create.js +0 -28
- package/src/fetch.js +0 -74
- package/src/leaderboard.js +0 -51
- package/src/lvlRole.js +0 -53
- package/src/models/level.js +0 -10
- package/src/models/lvlrole.js +0 -8
- package/src/rank.js +0 -295
- package/src/reset.js +0 -22
- package/src/roleSetup.js +0 -121
- package/src/setLevel.js +0 -42
- package/src/setXP.js +0 -23
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Collection } from "mongodb";
|
|
2
|
+
/**
|
|
3
|
+
* Options for creating a user document.
|
|
4
|
+
* @property {string} collection - The collection to create the document in.
|
|
5
|
+
* @property {object} data - The data to create the document with.
|
|
6
|
+
* @property {string} data.guild - The guild ID.
|
|
7
|
+
* @property {string} [data.user] - The user ID.
|
|
8
|
+
* @property {string} [data.name] - The username.
|
|
9
|
+
* @property {number} [data.level] - The level.
|
|
10
|
+
* @property {number} [data.xp] - The XP.
|
|
11
|
+
* @property {number} [data.xp_rate] - The XP rate.
|
|
12
|
+
*/
|
|
13
|
+
export interface UserOptions {
|
|
14
|
+
collection: "simply-xps";
|
|
15
|
+
data: {
|
|
16
|
+
guild: string;
|
|
17
|
+
user?: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
level?: number;
|
|
20
|
+
xp?: number;
|
|
21
|
+
xp_rate?: number;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The result of a user document.
|
|
26
|
+
* @property {string} [_id] - The ID of the document.
|
|
27
|
+
* @property {string} user - The user ID.
|
|
28
|
+
* @property {string} [name] - The username.
|
|
29
|
+
* @property {string} guild - The guild ID.
|
|
30
|
+
* @property {string} lastUpdated - ISO String of the last time the user was updated.
|
|
31
|
+
* @property {number} level - The level.
|
|
32
|
+
* @property {number} xp - The XP.
|
|
33
|
+
* @property {number} [xp_rate] - The XP rate.
|
|
34
|
+
*/
|
|
35
|
+
export interface UserResult {
|
|
36
|
+
_id?: string;
|
|
37
|
+
user: string;
|
|
38
|
+
name?: string;
|
|
39
|
+
guild: string;
|
|
40
|
+
lastUpdated: string;
|
|
41
|
+
level: number;
|
|
42
|
+
xp: number;
|
|
43
|
+
xp_rate?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Options for creating a level role document.
|
|
47
|
+
* @property {string} collection - The collection to create the document in.
|
|
48
|
+
* @property {object} data - The data to create the document with.
|
|
49
|
+
* @property {string} data.guild - The guild ID.
|
|
50
|
+
* @property {object} data.lvlrole - The level role data.
|
|
51
|
+
* @property {number} data.lvlrole.lvl - The level.
|
|
52
|
+
* @property {string | Array<string>} [data.lvlrole.role] - The role ID(s).
|
|
53
|
+
*/
|
|
54
|
+
export interface LevelRoleOptions {
|
|
55
|
+
collection: "simply-xp-levelroles";
|
|
56
|
+
data: {
|
|
57
|
+
guild: string;
|
|
58
|
+
lvlrole: {
|
|
59
|
+
lvl: number | null;
|
|
60
|
+
role?: string | Array<string> | null;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The result of a level role document.
|
|
66
|
+
* @property {string} [_id] - The ID of the document.
|
|
67
|
+
* @property {string} guild - The guild ID.
|
|
68
|
+
* @property {object} lvlrole - The level role data.
|
|
69
|
+
* @property {number} lvlrole.lvl - The level.
|
|
70
|
+
* @property {string | Array<string>} [lvlrole.role] - The role ID(s).
|
|
71
|
+
* @property {string} lastUpdated - ISO String of the last time the level role was updated.
|
|
72
|
+
*/
|
|
73
|
+
export type LevelRoleResult = {
|
|
74
|
+
_id?: string;
|
|
75
|
+
guild: string;
|
|
76
|
+
lvlrole: {
|
|
77
|
+
lvl: number;
|
|
78
|
+
role?: string | Array<string>;
|
|
79
|
+
};
|
|
80
|
+
lastUpdated: string;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Database class providing methods to interact with the database.
|
|
84
|
+
* @class db
|
|
85
|
+
*/
|
|
86
|
+
export declare class db {
|
|
87
|
+
/**
|
|
88
|
+
* Gets a collection from the database.
|
|
89
|
+
* @param {collection} collection - The collection to get.
|
|
90
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#getCollection Documentation
|
|
91
|
+
* @returns {Collection} The collection.
|
|
92
|
+
* @throws {XpFatal} Throws an error if there is no database connection, or database type is invalid.
|
|
93
|
+
*/
|
|
94
|
+
static getCollection(collection: string): Collection;
|
|
95
|
+
/**
|
|
96
|
+
* Creates one document in the database.
|
|
97
|
+
*
|
|
98
|
+
* @async
|
|
99
|
+
* @param {UserOptions | LevelRoleOptions} query - The document to create.
|
|
100
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#createOne Documentation
|
|
101
|
+
* @returns {Promise<UserResult | LevelRoleResult>} The created document.
|
|
102
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
103
|
+
*/
|
|
104
|
+
static createOne(query: UserOptions | LevelRoleOptions): Promise<UserResult | LevelRoleResult>;
|
|
105
|
+
/**
|
|
106
|
+
* Deletes multiple documents from the database.
|
|
107
|
+
* @async
|
|
108
|
+
* @param {UserOptions | LevelRoleOptions} query - The documents to delete.
|
|
109
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#deleteMany Documentation
|
|
110
|
+
* @returns {Promise<boolean>} `true` if the documents were successfully deleted, otherwise `false`.
|
|
111
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
112
|
+
*/
|
|
113
|
+
static deleteMany(query: UserOptions | LevelRoleOptions): Promise<boolean>;
|
|
114
|
+
/**
|
|
115
|
+
* Deletes one document from the database.
|
|
116
|
+
*
|
|
117
|
+
* @async
|
|
118
|
+
* @param {UserOptions | LevelRoleOptions} query - The document to delete.
|
|
119
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#deleteOne Documentation
|
|
120
|
+
* @returns {Promise<boolean>} `true` if the document was successfully deleted, otherwise `false`.
|
|
121
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
122
|
+
*/
|
|
123
|
+
static deleteOne(query: UserOptions | LevelRoleOptions): Promise<boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* Finds one document in the database.
|
|
126
|
+
*
|
|
127
|
+
* @async
|
|
128
|
+
* @param {UserOptions | LevelRoleOptions} query - The query to search for the document.
|
|
129
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#findOne Documentation
|
|
130
|
+
* @returns {Promise<UserResult | LevelRoleResult>} The found document.
|
|
131
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
132
|
+
*/
|
|
133
|
+
static findOne(query: UserOptions | LevelRoleOptions): Promise<UserResult | LevelRoleResult>;
|
|
134
|
+
/**
|
|
135
|
+
* Finds multiple documents in the database.
|
|
136
|
+
*
|
|
137
|
+
* @async
|
|
138
|
+
* @param {UserOptions | LevelRoleOptions} query - The query to search for multiple documents.
|
|
139
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#find Documentation
|
|
140
|
+
* @returns {Promise<UserResult[] | LevelRoleResult[]>} An array of found documents.
|
|
141
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
142
|
+
*/
|
|
143
|
+
static find(query: UserOptions | LevelRoleOptions): Promise<UserResult[] | LevelRoleResult[]>;
|
|
144
|
+
/**
|
|
145
|
+
* Finds all documents in a collection.
|
|
146
|
+
* @param {"simply-xps" | "simply-xp-levelroles"} collection - The collection to search for all documents.
|
|
147
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#findAll Documentation
|
|
148
|
+
* @returns {Promise<UserResult[] | LevelRoleResult[]>} An array of found documents.
|
|
149
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
150
|
+
*/
|
|
151
|
+
static findAll(collection: "simply-xps" | "simply-xp-levelroles"): Promise<UserResult[] | LevelRoleResult[]>;
|
|
152
|
+
/**
|
|
153
|
+
* Updates one document in the database.
|
|
154
|
+
*
|
|
155
|
+
* @async
|
|
156
|
+
* @param {UserOptions | LevelRoleOptions} filter - The document to update.
|
|
157
|
+
* @param {UserOptions | LevelRoleOptions} update - The document update data.
|
|
158
|
+
* @param {object} [options] - MongoDB options for updating the document.
|
|
159
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#updateOne Documentation
|
|
160
|
+
* @returns {Promise<UserResult | LevelRoleResult>} The updated document.
|
|
161
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
162
|
+
*/
|
|
163
|
+
static updateOne(filter: UserOptions | LevelRoleOptions, update: UserOptions | LevelRoleOptions, options?: object): Promise<UserResult | LevelRoleResult>;
|
|
164
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Handle database errors
|
|
4
|
+
* @param {Error} error
|
|
5
|
+
* @param {string} functionName
|
|
6
|
+
* @returns {void}
|
|
7
|
+
* @private
|
|
8
|
+
*/function handleError(e,a){throw new xplogs_1.XpFatal({function:"db."+a,message:e})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.db=void 0;const xplogs_1=require("./xplogs"),xp_1=require("../../xp");class db{
|
|
9
|
+
/**
|
|
10
|
+
* Gets a collection from the database.
|
|
11
|
+
* @param {collection} collection - The collection to get.
|
|
12
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#getCollection Documentation
|
|
13
|
+
* @returns {Collection} The collection.
|
|
14
|
+
* @throws {XpFatal} Throws an error if there is no database connection, or database type is invalid.
|
|
15
|
+
*/
|
|
16
|
+
static getCollection(e){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"getCollection()",message:"No database connection"});if("mongodb"!==xp_1.xp.dbType)throw new xplogs_1.XpFatal({function:"getCollection()",message:"MongoDB has to be your database type to use this function."});return xp_1.xp.database.db().collection(e)}
|
|
17
|
+
/**
|
|
18
|
+
* Creates one document in the database.
|
|
19
|
+
*
|
|
20
|
+
* @async
|
|
21
|
+
* @param {UserOptions | LevelRoleOptions} query - The document to create.
|
|
22
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#createOne Documentation
|
|
23
|
+
* @returns {Promise<UserResult | LevelRoleResult>} The created document.
|
|
24
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
25
|
+
*/static async createOne(e){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"createOne()",message:"No database connection"});switch(xp_1.xp.dbType){case"mongodb":xp_1.xp.database.db().collection(e.collection).insertOne({...e.data,lastUpdated:(new Date).toISOString()}).catch(e=>handleError(e,"createOne()"));break;case"sqlite":"simply-xps"===e.collection?xp_1.xp.database.prepare('INSERT INTO "simply-xps" (user, guild, level, name, lastUpdated, xp, xp_rate) VALUES (?, ?, ?, ?, ?, ?, ?)').run(e.data.user,e.data.guild,e.data.level,e.data?.name,(new Date).toISOString(),e.data.xp,e.data.xp_rate):xp_1.xp.database.prepare('INSERT INTO "simply-xp-levelroles" (gid, lvlrole, lastUpdated) VALUES (?, ?, ?)').run(e.data.guild,JSON.stringify(e.data.lvlrole),(new Date).toISOString())}return db.findOne(e)}
|
|
26
|
+
/**
|
|
27
|
+
* Deletes multiple documents from the database.
|
|
28
|
+
* @async
|
|
29
|
+
* @param {UserOptions | LevelRoleOptions} query - The documents to delete.
|
|
30
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#deleteMany Documentation
|
|
31
|
+
* @returns {Promise<boolean>} `true` if the documents were successfully deleted, otherwise `false`.
|
|
32
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
33
|
+
*/static async deleteMany(e){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"deleteMany()",message:"No database connection"});let a;switch(xp_1.xp.dbType){case"mongodb":a=xp_1.xp.database.db().collection(e.collection).deleteMany(e.data).catch(e=>handleError(e,"deleteMany()"));break;case"sqlite":a=("simply-xps"===e.collection?xp_1.xp.database.prepare('DELETE FROM "simply-xps" WHERE guild = ?'):xp_1.xp.database.prepare('DELETE FROM "simply-xp-levelroles" WHERE gid = ?')).run(e.data.guild)}return!!a}
|
|
34
|
+
/**
|
|
35
|
+
* Deletes one document from the database.
|
|
36
|
+
*
|
|
37
|
+
* @async
|
|
38
|
+
* @param {UserOptions | LevelRoleOptions} query - The document to delete.
|
|
39
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#deleteOne Documentation
|
|
40
|
+
* @returns {Promise<boolean>} `true` if the document was successfully deleted, otherwise `false`.
|
|
41
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
42
|
+
*/static async deleteOne(a){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"deleteOne()",message:"No database connection"});let e;switch(xp_1.xp.dbType){case"mongodb":e=xp_1.xp.database.db().collection(a.collection).deleteOne(a.data).catch(e=>handleError(e,"deleteOne()"));break;case"sqlite":e="simply-xps"===a.collection?xp_1.xp.database.prepare('DELETE FROM "simply-xps" WHERE guild = ? AND user = ?').run(a.data.guild,a.data.user):(e=(e=await this.find(a)).filter(e=>e.lvlrole.lvl===a.data.lvlrole.lvl)[0],xp_1.xp.database.prepare('DELETE FROM "simply-xp-levelroles" WHERE gid = ? AND lvlrole = ?').run(a.data.guild,JSON.stringify(e.lvlrole)))}return!!e}
|
|
43
|
+
/**
|
|
44
|
+
* Finds one document in the database.
|
|
45
|
+
*
|
|
46
|
+
* @async
|
|
47
|
+
* @param {UserOptions | LevelRoleOptions} query - The query to search for the document.
|
|
48
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#findOne Documentation
|
|
49
|
+
* @returns {Promise<UserResult | LevelRoleResult>} The found document.
|
|
50
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
51
|
+
*/static async findOne(a){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"findOne()",message:"No database connection"});let e;switch(xp_1.xp.dbType){case"mongodb":e="simply-xps"===a.collection?await xp_1.xp.database.db().collection(a.collection).findOne(a.data).catch(e=>handleError(e,"findOne()")):await xp_1.xp.database.db().collection(a.collection).findOne({guild:a.data.guild,"lvlrole.lvl":a.data.lvlrole.lvl}).catch(e=>handleError(e,"findOne()"));break;case"sqlite":e="simply-xps"===a.collection?xp_1.xp.database.prepare('SELECT * FROM "simply-xps" WHERE guild = ? AND user = ?').get(a.data.guild,a.data.user):(e=await this.find(a)).filter(e=>e.lvlrole.lvl===a.data.lvlrole.lvl)[0]}return e}
|
|
52
|
+
/**
|
|
53
|
+
* Finds multiple documents in the database.
|
|
54
|
+
*
|
|
55
|
+
* @async
|
|
56
|
+
* @param {UserOptions | LevelRoleOptions} query - The query to search for multiple documents.
|
|
57
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#find Documentation
|
|
58
|
+
* @returns {Promise<UserResult[] | LevelRoleResult[]>} An array of found documents.
|
|
59
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
60
|
+
*/static async find(e){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"find()",message:"No database connection"});let a;switch(xp_1.xp.dbType){case"mongodb":a=xp_1.xp.database.db().collection(e.collection).find(e.data).toArray().catch(e=>handleError(e,"find()"));break;case"sqlite":"simply-xps"===e.collection?a=xp_1.xp.database.prepare('SELECT * FROM "simply-xps" WHERE guild = ?').all(e.data.guild):(a=xp_1.xp.database.prepare('SELECT * FROM "simply-xp-levelroles" WHERE gid = ?').all(e.data.guild)).length&&(a=await Promise.all(a.map(async e=>(e.lvlrole=JSON.parse(e.lvlrole),e))))}return a}
|
|
61
|
+
/**
|
|
62
|
+
* Finds all documents in a collection.
|
|
63
|
+
* @param {"simply-xps" | "simply-xp-levelroles"} collection - The collection to search for all documents.
|
|
64
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#findAll Documentation
|
|
65
|
+
* @returns {Promise<UserResult[] | LevelRoleResult[]>} An array of found documents.
|
|
66
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
67
|
+
*/static async findAll(e){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"findAll()",message:"No database connection"});let a;switch(xp_1.xp.dbType){case"mongodb":a=xp_1.xp.database.db().collection(e).find().toArray().catch(e=>handleError(e,"findAll()"));break;case"sqlite":"simply-xps"===e?a=xp_1.xp.database.prepare('SELECT * FROM "simply-xps"').all():(a=xp_1.xp.database.prepare('SELECT * FROM "simply-xp-levelroles"').all()).length&&(a=await Promise.all(a.map(async e=>(e.lvlrole=JSON.parse(e.lvlrole),e))))}return a}
|
|
68
|
+
/**
|
|
69
|
+
* Updates one document in the database.
|
|
70
|
+
*
|
|
71
|
+
* @async
|
|
72
|
+
* @param {UserOptions | LevelRoleOptions} filter - The document to update.
|
|
73
|
+
* @param {UserOptions | LevelRoleOptions} update - The document update data.
|
|
74
|
+
* @param {object} [options] - MongoDB options for updating the document.
|
|
75
|
+
* @link https://simplyxp.js.org/docs/next/handlers/database#updateOne Documentation
|
|
76
|
+
* @returns {Promise<UserResult | LevelRoleResult>} The updated document.
|
|
77
|
+
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
78
|
+
*/static async updateOne(e,a,t){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"updateOne()",message:"No database connection"});switch(xp_1.xp.dbType){case"mongodb":await xp_1.xp.database.db().collection(a.collection).updateOne(e.data,{$set:{...a.data,lastUpdated:(new Date).toISOString()}},t).catch(e=>handleError(e,"updateOne()"));break;case"sqlite":if("simply-xps"===e.collection&&"simply-xps"===a.collection)xp_1.xp.database.prepare(`UPDATE "simply-xps" SET xp_rate = ?, xp = ?, lastUpdated = ?, level = ? ${a.data?.name?", name = ?":""} WHERE guild = ? AND user = ?`).run(a.data?.name?[a.data.xp_rate,a.data.xp,(new Date).toISOString(),a.data.level,a.data.name,e.data.guild,e.data.user]:[a.data.xp_rate,a.data.xp,(new Date).toISOString(),a.data.level,e.data.guild,e.data.user]);else{if("simply-xp-levelroles"!==e.collection||"simply-xp-levelroles"!==a.collection)throw new xplogs_1.XpFatal({function:"updateOne()",message:"Collection mismatch, expected same collection on both filter and update."});xp_1.xp.database.prepare('UPDATE "simply-xp-levelroles" SET lvlrole = ? WHERE gid = ?').run(JSON.stringify(a.data.lvlrole),e.data.guild)}}return db.findOne(a)}}exports.db=db;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
interface HttpsOptions {
|
|
3
|
+
body?: object;
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
method?: "GET" | "POST";
|
|
7
|
+
responseType?: "json" | "stream" | "text";
|
|
8
|
+
statusCode?: number;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Send HTTPS Request.
|
|
13
|
+
* @param {string} url
|
|
14
|
+
* @param {HttpsOptions} options
|
|
15
|
+
* @param {Object} [options.body]
|
|
16
|
+
* @param {string} [options.endpoint]
|
|
17
|
+
* @param {Record<string, string>} [options.headers]
|
|
18
|
+
* @param {("GET"|"POST")} [options.method="GET"]
|
|
19
|
+
* @param {("json"|"stream"|"text")} [options.responseType="json"] - Expected response type.
|
|
20
|
+
* @param {number} [options.statusCode] - Expected status code.
|
|
21
|
+
* @param {number} [options.timeout] - Request Timeout in Milliseconds
|
|
22
|
+
* @returns {Promise<Object|Buffer|string>} - Returns the response from the request.
|
|
23
|
+
* @throws {PromiseRejectedResult} - If the request fails.
|
|
24
|
+
*/
|
|
25
|
+
export declare function https(url: string, options?: HttpsOptions): Promise<object | Buffer | string>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Send HTTPS Request.
|
|
4
|
+
* @param {string} url
|
|
5
|
+
* @param {HttpsOptions} options
|
|
6
|
+
* @param {Object} [options.body]
|
|
7
|
+
* @param {string} [options.endpoint]
|
|
8
|
+
* @param {Record<string, string>} [options.headers]
|
|
9
|
+
* @param {("GET"|"POST")} [options.method="GET"]
|
|
10
|
+
* @param {("json"|"stream"|"text")} [options.responseType="json"] - Expected response type.
|
|
11
|
+
* @param {number} [options.statusCode] - Expected status code.
|
|
12
|
+
* @param {number} [options.timeout] - Request Timeout in Milliseconds
|
|
13
|
+
* @returns {Promise<Object|Buffer|string>} - Returns the response from the request.
|
|
14
|
+
* @throws {PromiseRejectedResult} - If the request fails.
|
|
15
|
+
*/function https(e,o={}){return new Promise((s,r)=>{const t=(0,https_1.request)({agent:new https_1.Agent({keepAlive:!0}),headers:o?.headers||{"Content-Type":"application/json"},hostname:new url_1.URL(e).hostname,method:o?.method||"GET",path:o?.endpoint||new url_1.URL(e).pathname},t=>{const e=[];t.on("error",r),t.on("data",t=>e.push(Buffer.from(t))),t.on("end",()=>{o?.statusCode&&t.statusCode!==o?.statusCode&&r({error:"Unexpected Status Code",status:t.statusCode});try{switch(o?.responseType||"json"){case"json":s(JSON.parse(Buffer.concat(e).toString()));break;case"stream":s(Buffer.concat(e));break;default:s(Buffer.concat(e).toString())}}catch(t){r(t)}})}).on("error",r);t.setTimeout(o?.timeout||5e3,()=>{t.destroy(),r({error:"Request Timed Out",status:408})}),o?.body&&t.write(JSON.stringify(o.body)),t.end()})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.https=void 0;const https_1=require("https"),url_1=require("url");exports.https=https;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Database } from "better-sqlite3";
|
|
2
|
+
import { Plugin } from "../../xp";
|
|
3
|
+
import { MongoClient } from "mongodb";
|
|
4
|
+
/**
|
|
5
|
+
* Options for clean function.
|
|
6
|
+
* @property {boolean} [db=false] - Whether to clean the database or not.
|
|
7
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/clean
|
|
8
|
+
*/
|
|
9
|
+
type CleanOptions = {
|
|
10
|
+
db?: boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Options for the XP client.
|
|
14
|
+
* @property {boolean} auto_create - Whether to automatically create a user if they don't exist in the database.
|
|
15
|
+
* @property {boolean} auto_clean - Whether to automatically clean database and cache.
|
|
16
|
+
* @property {object} dbOptions - The database options.
|
|
17
|
+
* @property {"mongodb" | "sqlite"} dbOptions.type - The database type.
|
|
18
|
+
* @property {MongoClient | Database} dbOptions.database - The database connection.
|
|
19
|
+
* @property {boolean} debug - Whether to enable debug logs.
|
|
20
|
+
* @property {boolean} notify - Enable/Disable console notifications.
|
|
21
|
+
* @property {"slow" | "normal" | "fast" | number} xp_rate - The XP rate.
|
|
22
|
+
*/
|
|
23
|
+
interface NewClientOptions {
|
|
24
|
+
auto_create: boolean;
|
|
25
|
+
auto_clean: boolean;
|
|
26
|
+
dbOptions: {
|
|
27
|
+
type: "mongodb";
|
|
28
|
+
database: MongoClient;
|
|
29
|
+
} | {
|
|
30
|
+
type: "sqlite";
|
|
31
|
+
database: Database;
|
|
32
|
+
};
|
|
33
|
+
debug: boolean;
|
|
34
|
+
notify: boolean;
|
|
35
|
+
xp_rate?: "slow" | "normal" | "fast" | number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Helps to clean the database and cache, more in the future, maybe.
|
|
39
|
+
* @param {CleanOptions} [options={}] - The options.
|
|
40
|
+
* @param {boolean?} options.db - Whether to clean the database or not.
|
|
41
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/clean
|
|
42
|
+
* @returns {void} - Nothing.
|
|
43
|
+
* @throws {XpFatal} If an error occurs.
|
|
44
|
+
*/
|
|
45
|
+
export declare function clean(options?: CleanOptions): void;
|
|
46
|
+
/**
|
|
47
|
+
* Convert XP to level and vice versa.
|
|
48
|
+
*
|
|
49
|
+
* @param {number} value.
|
|
50
|
+
* @param {"xp" | "level"} type - Type to convert from (Default: level).
|
|
51
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/convert
|
|
52
|
+
* @returns {number} - The converted value. (XP to level or level to XP)
|
|
53
|
+
* @throws {XpFatal} If an invalid type is provided or if the value is not provided.
|
|
54
|
+
*/
|
|
55
|
+
export declare function convertFrom(value: number, type?: ("xp" | "level")): number;
|
|
56
|
+
/**
|
|
57
|
+
* Registers fonts from URLs or paths (For convenience).
|
|
58
|
+
* @param {string} pathOrURL - The path or URL to the font.
|
|
59
|
+
* @param {string} name - The name of the font.
|
|
60
|
+
* @param {number} [timeout=1500] - The timeout for the request.
|
|
61
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/registerFont
|
|
62
|
+
* @returns {Promise<void>} - Nothing.
|
|
63
|
+
* @throws {XpFatal} If an invalid path or URL is provided.
|
|
64
|
+
*/
|
|
65
|
+
export declare function registerFont(pathOrURL: string, name: string, timeout?: number): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Register Simply-XP Plugins.
|
|
68
|
+
* @param {Plugin[]} plugins - The plugins to register.
|
|
69
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/registerPlugins
|
|
70
|
+
* @returns {void} - Nothing.
|
|
71
|
+
* @throws {XpFatal} If an invalid plugin is provided.
|
|
72
|
+
*/
|
|
73
|
+
export declare function registerPlugins(plugins: Plugin[]): void;
|
|
74
|
+
/**
|
|
75
|
+
* Updates the options of the XP client.
|
|
76
|
+
* @param {NewClientOptions} clientOptions - The new options to update.
|
|
77
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/updateOptions
|
|
78
|
+
* @returns {void} - Nothing.
|
|
79
|
+
* @throws {XpFatal} If an invalid option is provided.
|
|
80
|
+
*/
|
|
81
|
+
export declare function updateOptions(clientOptions: NewClientOptions): void;
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Helps to clean the database and cache, more in the future, maybe.
|
|
4
|
+
* @param {CleanOptions} [options={}] - The options.
|
|
5
|
+
* @param {boolean?} options.db - Whether to clean the database or not.
|
|
6
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/clean
|
|
7
|
+
* @returns {void} - Nothing.
|
|
8
|
+
* @throws {XpFatal} If an error occurs.
|
|
9
|
+
*/function clean(e={}){e?.db&&xp_1.xp?.database&&xp_1.db.findAll("simply-xps").then(e=>{e.forEach(async e=>{0===e.level&&0===e.xp&&await xp_1.db.deleteOne({collection:"simply-xps",data:{user:e.user,guild:e.guild}})}),xplogs_1.XpLog.debug("clean()","REMOVED ALL USERS WITHOUT XP")}),(0,canvas_1.clearAllCache)(),xplogs_1.XpLog.debug("clean()","CLEARED CANVAS CACHE")}
|
|
10
|
+
/**
|
|
11
|
+
* Convert XP to level and vice versa.
|
|
12
|
+
*
|
|
13
|
+
* @param {number} value.
|
|
14
|
+
* @param {"xp" | "level"} type - Type to convert from (Default: level).
|
|
15
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/convert
|
|
16
|
+
* @returns {number} - The converted value. (XP to level or level to XP)
|
|
17
|
+
* @throws {XpFatal} If an invalid type is provided or if the value is not provided.
|
|
18
|
+
*/function convertFrom(e,t="level"){if(isNaN(e))throw new xplogs_1.XpFatal({function:"convertFrom()",message:"Value was not provided"});if("xp"!==t&&"level"!==t)throw new xplogs_1.XpFatal({function:"convert()",message:"Invalid type provided"});if("level"===t)return Math.pow(Math.max(0,e)/xp_1.xp.xp_rate,2);if("xp"===t)return Math.floor(xp_1.xp.xp_rate*Math.sqrt(Math.max(0,e)));throw new xplogs_1.XpFatal({function:"convertFrom()",message:"Invalid type provided"})}
|
|
19
|
+
/**
|
|
20
|
+
* Registers fonts from URLs or paths (For convenience).
|
|
21
|
+
* @param {string} pathOrURL - The path or URL to the font.
|
|
22
|
+
* @param {string} name - The name of the font.
|
|
23
|
+
* @param {number} [timeout=1500] - The timeout for the request.
|
|
24
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/registerFont
|
|
25
|
+
* @returns {Promise<void>} - Nothing.
|
|
26
|
+
* @throws {XpFatal} If an invalid path or URL is provided.
|
|
27
|
+
*/async function registerFont(e,t,a=1500){xp_1.xp.registeredFonts.includes(t)||(e.startsWith("https://")?await(0,xp_1.https)(e,{responseType:"stream",timeout:a}).then(e=>{canvas_1.GlobalFonts.register(e,t),xp_1.xp.registeredFonts.push(t)}).catch(e=>{throw new xplogs_1.XpFatal({function:"registerFont()",message:`Failed to register font from URL
|
|
28
|
+
`+JSON.stringify(e)})}):(canvas_1.GlobalFonts.registerFromPath(e,t),xp_1.xp.registeredFonts.push(t)))}
|
|
29
|
+
/**
|
|
30
|
+
* Register Simply-XP Plugins.
|
|
31
|
+
* @param {Plugin[]} plugins - The plugins to register.
|
|
32
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/registerPlugins
|
|
33
|
+
* @returns {void} - Nothing.
|
|
34
|
+
* @throws {XpFatal} If an invalid plugin is provided.
|
|
35
|
+
*/function registerPlugins(e){if(!Array.isArray(e))throw new xplogs_1.XpFatal({function:"registerPlugins()",message:"Plugins must be an array"});e.forEach(async t=>{let a=!1,p=!0;if(t?.initialize&&t?.name||(p=!1,xplogs_1.XpLog.warn("registerPlugins()",t?.name?t.name.toUpperCase()+" PLUGIN NOT INITIALIZED: No initialize function provided.":"INVALID PLUGIN PROVIDED")),Array.isArray(t?.requiredVersions)&&!t.requiredVersions.includes(xp_1.xp.version)&&(t.requiredVersions.forEach(e=>{parseInt(e)?p=!(!parseInt(e)||1!==e.length||xp_1.xp.version.split(".")[0]!==e):e.match(/\d\.\d\.\d/)||p||(a=!0,p=!0)}),a&&xplogs_1.XpLog.warn("registerPlugins()",t.name.toUpperCase()+" PLUGIN FAILED VERSION CHECKS, ANYWAYS..."),p||a||xplogs_1.XpLog.warn("registerPlugins()",t.name.toUpperCase()+" PLUGIN NOT INITIALIZED: Requires: v"+t.requiredVersions.join(", v"))),p)try{await t.initialize(xp_1.xp).then(()=>{xplogs_1.XpLog.info("registerPlugins()",t.name.toUpperCase()+" PLUGIN INITIALIZED")})}catch(e){throw new xplogs_1.XpFatal({function:"registerPlugins()",message:`Failed to initialize plugin: ${t.name}
|
|
36
|
+
`+e})}})}
|
|
37
|
+
/**
|
|
38
|
+
* Updates the options of the XP client.
|
|
39
|
+
* @param {NewClientOptions} clientOptions - The new options to update.
|
|
40
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/Functions/updateOptions
|
|
41
|
+
* @returns {void} - Nothing.
|
|
42
|
+
* @throws {XpFatal} If an invalid option is provided.
|
|
43
|
+
*/function updateOptions(e){if(!e)throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Options were not provided"});if("object"!=typeof e)throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Options must be an object"});if(e?.auto_clean&&(xp_1.xp.auto_clean=!0),e?.auto_create&&(xp_1.xp.auto_create=!0),e?.debug&&(xp_1.xp.debug=!0),e?.notify&&(xp_1.xp.notify=e.notify),e?.xp_rate&&("number"==typeof e.xp_rate||["fast","normal","slow"].includes(e.xp_rate))&&(xp_1.xp.xp_rate="slow"===e.xp_rate?.05:"normal"===e.xp_rate?.1:"fast"===e.xp_rate?.5:e.xp_rate),e.dbOptions&&"object"==typeof e.dbOptions&&e.dbOptions.type&&e.dbOptions.database){var{type:t,database:a}=e.dbOptions;if(!(t&&"mongodb"===t||"sqlite"===t))throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Invalid database type provided"});if(xp_1.xp.dbType=t,a){xp_1.xp.database=a;const p="mongodb"===xp_1.xp.dbType?{name:"MongoDB",type:"mongodb",min:3,max:6}:{name:"Better-SQLite3",type:"better-sqlite3",min:7,max:9};(0,connect_1.checkPackageVersion)(p.type,p.min,p.max).then(e=>{if(!e)throw new xplogs_1.XpFatal({function:"updateOptions()",message:`${p.name} V${p.min} up to V${p.max} is required.`});switch(xp_1.xp.dbType){case"mongodb":xp_1.xp.database.db().command({ping:1}).catch(()=>{throw xp_1.xp.database=void 0,new xplogs_1.XpFatal({function:"updateOptions()",message:"Invalid MongoDB connection"})});break;case"sqlite":try{xp_1.xp.database.prepare("SELECT 1").get()}catch(e){throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Invalid SQLite connection"})}}})}}e?.auto_clean&&clean({db:!0}),xp_1.db.findAll("simply-xps").then(e=>{e.filter(e=>e?.xp_rate!==xp_1.xp.xp_rate).map(async e=>{await xp_1.db.updateOne({collection:"simply-xps",data:{user:e.user,guild:e.guild}},{collection:"simply-xps",data:{user:e.user,guild:e.guild,level:convertFrom(e.xp,"xp"),xp:e.xp,xp_rate:xp_1.xp.xp_rate}})}),xplogs_1.XpLog.debug("updateOptions()","UPDATED ALL USERS WITH NEW XP RATE")})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateOptions=exports.registerPlugins=exports.registerFont=exports.convertFrom=exports.clean=void 0;const connect_1=require("../connect"),canvas_1=require("@napi-rs/canvas"),xp_1=require("../../xp"),xplogs_1=require("./xplogs");exports.clean=clean,exports.convertFrom=convertFrom,exports.registerFont=registerFont,exports.registerPlugins=registerPlugins,exports.updateOptions=updateOptions;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { UserResult } from "./database";
|
|
2
|
+
type errOptions = {
|
|
3
|
+
function: string;
|
|
4
|
+
message: string | Error;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Emits a fatal error message
|
|
8
|
+
* @class XpFatal
|
|
9
|
+
*/
|
|
10
|
+
export declare class XpFatal extends Error {
|
|
11
|
+
/**
|
|
12
|
+
* Emits a simple error message
|
|
13
|
+
* @param {errOptions} options
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
constructor(options: errOptions);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Emits a log message
|
|
20
|
+
* @class XpLog
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
23
|
+
export declare class XpLog {
|
|
24
|
+
/**
|
|
25
|
+
* Emits a log message with the specified level
|
|
26
|
+
* @param {("debug" | "error" | "info" | "warn")} level - The log level (e.g., 'info', 'error', 'warn')
|
|
27
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
28
|
+
* @param {string} message - The log message
|
|
29
|
+
* @private
|
|
30
|
+
*/
|
|
31
|
+
static log(level: ("debug" | "error" | "info" | "warn"), xpFunction: string, message: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Emits a debug log
|
|
34
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
35
|
+
* @param {string} message - The log message
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
static debug(xpFunction: string, message: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Emits an info log
|
|
41
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
42
|
+
* @param {string} message - The log message
|
|
43
|
+
* @returns {boolean} Returns true
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
static info(xpFunction: string, message: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Emits an error log
|
|
49
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
50
|
+
* @param {string} message - The log message
|
|
51
|
+
* @returns {boolean} Returns false
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
static err(xpFunction: string, message: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Emits a warning log
|
|
57
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
58
|
+
* @param {string} message - The log message
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
static warn(xpFunction: string, message: string): void;
|
|
62
|
+
}
|
|
63
|
+
export type XpEventCallback = {
|
|
64
|
+
custom: (message: string) => void;
|
|
65
|
+
debug: (xpFunction: string, message: string) => void;
|
|
66
|
+
error: (xpFunction: string, message: string) => void;
|
|
67
|
+
info: (xpFunction: string, message: string) => void;
|
|
68
|
+
levelDown: (data: UserResult, lostRoles: string[]) => void;
|
|
69
|
+
levelUp: (data: UserResult, newRoles: string[]) => void;
|
|
70
|
+
warn: (xpFunction: string, message: string) => void;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Event handler for XP events
|
|
74
|
+
* @class XpEvents
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
export declare class XpEvents {
|
|
78
|
+
static eventCallback: XpEventCallback;
|
|
79
|
+
static on(callbacks: XpEventCallback): void;
|
|
80
|
+
}
|
|
81
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.XpEvents=exports.XpLog=exports.XpFatal=void 0;const xp_1=require("../../xp");class XpFatal extends Error{constructor(e){super(e.function+": "+e.message)}}exports.XpFatal=XpFatal,Object.defineProperty(XpFatal.prototype,"name",{value:"SimplyXpFatal"});class XpLog{static log(e,t,o){var p=XpEvents.eventCallback?.[e],r=e.toUpperCase(),a=t.toUpperCase();p&&"function"==typeof p?p(t,o):console.log(`[35m[SIMPLY XP][0m ${{debug:"[36m",info:"[34m",error:"[31m",warn:"[33m"}[e]}(${r})[0m ${a}: `+o)}static debug(e,t){xp_1.xp.debug&&XpLog.log("debug",e,t)}
|
|
2
|
+
/**
|
|
3
|
+
* Emits an info log
|
|
4
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
5
|
+
* @param {string} message - The log message
|
|
6
|
+
* @returns {boolean} Returns true
|
|
7
|
+
* @private
|
|
8
|
+
*/static info(e,t){return xp_1.xp.notify&&XpLog.log("info",e,t),!0}
|
|
9
|
+
/**
|
|
10
|
+
* Emits an error log
|
|
11
|
+
* @param {string} xpFunction - The command or context of the log message
|
|
12
|
+
* @param {string} message - The log message
|
|
13
|
+
* @returns {boolean} Returns false
|
|
14
|
+
* @private
|
|
15
|
+
*/static err(e,t){return XpLog.log("error",e,t),!1}static warn(e,t){XpLog.log("warn",e,t)}}exports.XpLog=XpLog;class XpEvents{static eventCallback;static on(e){XpEvents.eventCallback=e}}exports.XpEvents=XpEvents;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { User } from "../xp";
|
|
2
|
+
/**
|
|
3
|
+
* Get array of all users in the leaderboard
|
|
4
|
+
* @async
|
|
5
|
+
* @param {string?} guildId - Guild ID (optional)
|
|
6
|
+
* @param {number} limit - Limit of users to return
|
|
7
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/functions/leaderboard
|
|
8
|
+
* @returns {Promise<User[]>} Array of all users in the leaderboard
|
|
9
|
+
* @throws {XpFatal} If guild ID is not provided or limit is less than 1
|
|
10
|
+
*/
|
|
11
|
+
export declare function leaderboard(guildId?: string, limit?: number): Promise<User[]>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Get array of all users in the leaderboard
|
|
4
|
+
* @async
|
|
5
|
+
* @param {string?} guildId - Guild ID (optional)
|
|
6
|
+
* @param {number} limit - Limit of users to return
|
|
7
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/functions/leaderboard
|
|
8
|
+
* @returns {Promise<User[]>} Array of all users in the leaderboard
|
|
9
|
+
* @throws {XpFatal} If guild ID is not provided or limit is less than 1
|
|
10
|
+
*/async function leaderboard(e,r){if(r&&!(1<=r))throw new xplogs_1.XpFatal({function:"leaderboard()",message:"Limit must be a number greater than 0"});const a=new Set;return e=e?(await xp_1.db.find({collection:"simply-xps",data:{guild:e}})).sort((e,r)=>r.xp-e.xp):(await xp_1.db.findAll("simply-xps")).sort((e,r)=>r.xp-e.xp).filter(e=>!a.has(e.user)&&(a.add(e.user),!0)),await Promise.all(e.map(async(e,r)=>(e.position=r+1,e))),e.slice(0,r)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.leaderboard=void 0;const xplogs_1=require("./functions/xplogs"),xp_1=require("../xp");exports.leaderboard=leaderboard;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MongoClient } from "mongodb";
|
|
2
|
+
import { Database } from "better-sqlite3";
|
|
3
|
+
/**
|
|
4
|
+
* Migration functions
|
|
5
|
+
* @class migrate
|
|
6
|
+
*/
|
|
7
|
+
export declare class migrate {
|
|
8
|
+
/**
|
|
9
|
+
* Effortlessly migrate from discord-xp to simply-xp.
|
|
10
|
+
* @async
|
|
11
|
+
* @param {boolean} deleteOld - Delete old data after migration
|
|
12
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/classes/migrate#migratediscord_xp
|
|
13
|
+
* @returns {Promise<boolean>} - Returns true if migration is successful
|
|
14
|
+
* @throws {XpLog.err} - If migration fails.
|
|
15
|
+
*/
|
|
16
|
+
static discord_xp(deleteOld?: boolean): Promise<boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Effortlessly migrate from MongoDB to SQLite. (or vice versa)
|
|
19
|
+
* @async
|
|
20
|
+
* @param {"mongodb"|"sqlite"} dbType
|
|
21
|
+
* @param {Database | MongoClient} connection
|
|
22
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/classes/migrate#migratefromdb
|
|
23
|
+
* @returns {Promise<boolean>} - Returns true if migration is successful
|
|
24
|
+
* @throws {XpFatal} - If parameters are not provided correctly
|
|
25
|
+
*/
|
|
26
|
+
static fromDB(dbType: "mongodb" | "sqlite", connection: Database | MongoClient): Promise<boolean>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.migrate=void 0;const xplogs_1=require("./functions/xplogs"),xp_1=require("../xp"),connect_1=require("./connect");class migrate{
|
|
2
|
+
/**
|
|
3
|
+
* Effortlessly migrate from discord-xp to simply-xp.
|
|
4
|
+
* @async
|
|
5
|
+
* @param {boolean} deleteOld - Delete old data after migration
|
|
6
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/classes/migrate#migratediscord_xp
|
|
7
|
+
* @returns {Promise<boolean>} - Returns true if migration is successful
|
|
8
|
+
* @throws {XpLog.err} - If migration fails.
|
|
9
|
+
*/
|
|
10
|
+
static async discord_xp(e=!1){var r=await xp_1.db.getCollection("levels").find().toArray();xplogs_1.XpLog.debug("migrate.discord_xp()",`FOUND ${r.length} DOCUMENTS`);try{for(const t of r)await xp_1.db.findOne({collection:"simply-xps",data:{guild:t.guildID,user:t.userID}})||(await xp_1.db.createOne({collection:"simply-xps",data:{guild:t.guildID,user:t.userID,xp:t.xp,level:(0,xp_1.convertFrom)(t.xp,"xp")}}),e&&await xp_1.db.getCollection("levels").deleteOne({userID:t.userID,guildID:t.guildID}));return!0}catch(e){return xplogs_1.XpLog.err("migrate.discord_xp()",e),!1}}
|
|
11
|
+
/**
|
|
12
|
+
* Effortlessly migrate from MongoDB to SQLite. (or vice versa)
|
|
13
|
+
* @async
|
|
14
|
+
* @param {"mongodb"|"sqlite"} dbType
|
|
15
|
+
* @param {Database | MongoClient} connection
|
|
16
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/classes/migrate#migratefromdb
|
|
17
|
+
* @returns {Promise<boolean>} - Returns true if migration is successful
|
|
18
|
+
* @throws {XpFatal} - If parameters are not provided correctly
|
|
19
|
+
*/static async fromDB(e,r){if(!e)throw new xplogs_1.XpFatal({function:"migrate.database()",message:"No database type provided"});if(!r)throw new xplogs_1.XpFatal({function:"migrate.database()",message:"No database connection provided"});if(xp_1.xp.dbType===e)return xplogs_1.XpLog.info("migrate.fromDB()","Same database received, that was unnecessary!");let t;switch(e){case"mongodb":try{if(!await(0,connect_1.checkPackageVersion)("mongodb",4,6))return xplogs_1.XpLog.err("migrate.fromDB()","MongoDB V4 up to V6 is required");t=await r.db().collection("simply-xps").find().toArray()}catch(e){return xplogs_1.XpLog.err("migrate.fromDB()",e),!1}break;case"sqlite":try{if(!await(0,connect_1.checkPackageVersion)("better-sqlite3",7,8))return xplogs_1.XpLog.err("migrate.fromDB()","better-sqlite3 V7 up to V8 is required");t=r.prepare("SELECT * FROM `simply-xps`").all()}catch(e){return xplogs_1.XpLog.err("migrate.fromDB()",e),!1}}return xplogs_1.XpLog.debug("migrate.fromDB()",`FOUND ${t.length} RESULTS`),await Promise.all(t.map(async e=>await xp_1.db.findOne({collection:"simply-xps",data:{guild:e.guild,user:e.user}})?xp_1.db.updateOne({collection:"simply-xps",data:{guild:e.guild,user:e.user}},{collection:"simply-xps",data:{guild:e.guild,user:e.user,name:e.name,xp:e.xp,level:e.level}}):xp_1.db.createOne({collection:"simply-xps",data:{guild:e.guild,user:e.user,xp:e.xp,level:e.level}}))),!0}}exports.migrate=migrate;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { UserResult } from "./functions/database";
|
|
2
|
+
import { XPResult } from "./add";
|
|
3
|
+
/**
|
|
4
|
+
* Add XP to a user
|
|
5
|
+
* @async
|
|
6
|
+
* @param {string} userId
|
|
7
|
+
* @param {string} guildId
|
|
8
|
+
* @param {number} level
|
|
9
|
+
* @param {string} username - Username to use if auto_create is enabled
|
|
10
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/functions/addlevel
|
|
11
|
+
* @returns {Promise<UserResult>} - Object of user data on success
|
|
12
|
+
* @throws {XpFatal} - If parameters are not provided correctly
|
|
13
|
+
*/
|
|
14
|
+
export declare function removeLevel(userId: string, guildId: string, level: number, username?: string): Promise<UserResult>;
|
|
15
|
+
/**
|
|
16
|
+
* Add XP to a user.
|
|
17
|
+
* @async
|
|
18
|
+
* @param {string} userId - The ID of the user.
|
|
19
|
+
* @param {string} guildId - The ID of the guild.
|
|
20
|
+
* @param {number | {min: number, max: number}} xpData - The XP to add, can be a number or an object with min and max properties.
|
|
21
|
+
* @param {string} username - Username to use if auto_create is enabled.
|
|
22
|
+
* @link `Documentation:` https://simplyxp.js.org/docs/next/functions/addxp
|
|
23
|
+
* @returns {Promise<XPResult>} - Object of user data on success.
|
|
24
|
+
* @throws {XpFatal} - If parameters are not provided correctly.
|
|
25
|
+
*/
|
|
26
|
+
export declare function removeXP(userId: string, guildId: string, xpData: number | {
|
|
27
|
+
min: number;
|
|
28
|
+
max: number;
|
|
29
|
+
}, username?: string): Promise<XPResult>;
|