simply-xp 2.0.0-dev.0-fix.2 → 2.0.0-dev.2

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/README.md CHANGED
@@ -41,10 +41,10 @@ yarn add simply-xp@dev
41
41
  # ✅ V2 Additions
42
42
 
43
43
  - Added support for `SQLite` database
44
- - Added `auto_purge` + `debug` option for `connect()` function
44
+ - Added `debug`, `auto_create`, `auto_purge` options for `connect()` function
45
45
  - Added `db` class for extended database functionality
46
46
  - Added `leaderboardCard()` function
47
- - Added `convert()` function
47
+ - Added `convertFrom()` function
48
48
  - Added `migrate` class
49
49
 
50
50
  # 🎉 V2 Changes 🎉
@@ -55,14 +55,16 @@ yarn add simply-xp@dev
55
55
  - Better Code Quality (EsLint)
56
56
  - Complete TypeScript Rewrite
57
57
  - Deleted `chart.js` dependency
58
- - `fetch()` now also returns `position`
59
- - `roleSetup` functions now accept roleID arrays! `["role1", "role2", "role3"]`
60
- - `reset()` function now accepts "erase" as an optional argument
58
+ - `fetch()` now also returns `position`, and accepts `username` parameter
59
+ - `roleSetup` functions now accept roleID arrays! `["role1", "role2", "role3"]`, and will return `timestamp` as a bonus!
60
+ - `reset()` function now accepts `username` and `erase` as optional arguments
61
+ - `addLevel(), addXP(), setLevel(), setXP()` now has a `username` parameter, to automatically create the user if it doesn't exist.
61
62
 
62
63
  # ⚠️ V2 Breaking Changes ⚠️
63
64
 
64
- - `create()` Requires new arguments.
65
+ - `create()` Now requires `username` argument.
65
66
  - `charts()` Requires new arguments.
66
67
  - `rank()` is **deprecated**, use `rankCard()` instead.
67
68
  - `rankCard()` Requires completely new arguments.
68
- - `roleSetup()` functions loses `client` argument.
69
+ - `roleSetup()` functions loses `client` argument.
70
+ - `leaderboard()` loses `client` argument, and returns `user` instead of `userID`
Binary file
package/lib/src/add.d.ts CHANGED
@@ -1,35 +1,36 @@
1
+ import { UserResult } from "./functions/database";
1
2
  /**
2
3
  * Add XP to a user
3
4
  * @async
4
5
  * @param {string} userId
5
6
  * @param {string} guildId
6
7
  * @param {number} level
8
+ * @param {string} username - Username to use if auto_create is enabled
7
9
  * @link `Documentation:` https://simplyxp.js.org/docs/addlevel
8
- * @returns {Promise<{user: string, guild: string, level: number, xp: number}>} - Object of user data on success
10
+ * @returns {Promise<UserResult>} - Object of user data on success
9
11
  * @throws {XpFatal} - If parameters are not provided correctly
10
12
  */
11
- export declare function addLevel(userId: string, guildId: string, level: number): Promise<{
12
- user: string;
13
- guild: string;
14
- level: number;
15
- xp: number;
16
- }>;
13
+ export declare function addLevel(userId: string, guildId: string, level: number, username?: string): Promise<UserResult>;
14
+ /**
15
+ * XP Results
16
+ * @property {boolean} hasLevelledUp - Whether the user has levelled up or not.
17
+ */
18
+ interface XPResult extends UserResult {
19
+ hasLevelledUp: boolean;
20
+ }
17
21
  /**
18
22
  * Add XP to a user.
19
23
  * @async
20
24
  * @param {string} userId - The ID of the user.
21
25
  * @param {string} guildId - The ID of the guild.
22
- * @param {number | {min: number, max: number}} xp - The XP to add, can be a number or an object with min and max properties.
26
+ * @param {number | {min: number, max: number}} xpData - The XP to add, can be a number or an object with min and max properties.
27
+ * @param {string} username - Username to use if auto_create is enabled.
23
28
  * @link `Documentation:` https://simplyxp.js.org/docs/addxp
24
- * @returns {Promise<{user: string, guild: string, level: number, xp: number}>} - Object of user data on success.
29
+ * @returns {Promise<XPResult>} - Object of user data on success.
25
30
  * @throws {XpFatal} - If parameters are not provided correctly.
26
31
  */
27
- export declare function addXP(userId: string, guildId: string, xp: number | {
32
+ export declare function addXP(userId: string, guildId: string, xpData: number | {
28
33
  min: number;
29
34
  max: number;
30
- }): Promise<{
31
- user: string;
32
- guild: string;
33
- level: number;
34
- xp: number;
35
- }>;
35
+ }, username?: string): Promise<XPResult>;
36
+ export {};
package/lib/src/add.js CHANGED
@@ -5,17 +5,19 @@
5
5
  * @param {string} userId
6
6
  * @param {string} guildId
7
7
  * @param {number} level
8
+ * @param {string} username - Username to use if auto_create is enabled
8
9
  * @link `Documentation:` https://simplyxp.js.org/docs/addlevel
9
- * @returns {Promise<{user: string, guild: string, level: number, xp: number}>} - Object of user data on success
10
+ * @returns {Promise<UserResult>} - Object of user data on success
10
11
  * @throws {XpFatal} - If parameters are not provided correctly
11
- */async function addLevel(userId,guildId,level){if(!userId)throw new xplogs_1.XpFatal({function:"addLevel()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"addLevel()",message:"Guild ID was not provided"});var e;if(level)return(e=await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}}))?database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:e.level+level,xp:(0,convert_1.convert)("level",level+e.level)}}):database_1.db.createOne({collection:"simply-xps",data:{user:userId,guild:guildId,level:level,xp:(0,convert_1.convert)("level",level)}});throw new xplogs_1.XpFatal({function:"addLevel()",message:"Level was not provided"})}
12
+ */async function addLevel(userId,guildId,level,username){if(!userId)throw new xplogs_1.XpFatal({function:"addLevel()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"addLevel()",message:"Guild ID was not provided"});if(!level)throw new xplogs_1.XpFatal({function:"addLevel()",message:"Level was not provided"});var e=await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}});if(e)return database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:e.level+level,xp:(0,utilities_1.convertFrom)(level+e.level)}});if(xp_1.xp.auto_create&&username)return database_1.db.createOne({collection:"simply-xps",data:{guild:guildId,user:userId,name:username,level:level,xp:(0,utilities_1.convertFrom)(level)}});throw new xplogs_1.XpFatal({function:"addLevel()",message:"User does not exist"})}
12
13
  /**
13
14
  * Add XP to a user.
14
15
  * @async
15
16
  * @param {string} userId - The ID of the user.
16
17
  * @param {string} guildId - The ID of the guild.
17
- * @param {number | {min: number, max: number}} xp - The XP to add, can be a number or an object with min and max properties.
18
+ * @param {number | {min: number, max: number}} xpData - The XP to add, can be a number or an object with min and max properties.
19
+ * @param {string} username - Username to use if auto_create is enabled.
18
20
  * @link `Documentation:` https://simplyxp.js.org/docs/addxp
19
- * @returns {Promise<{user: string, guild: string, level: number, xp: number}>} - Object of user data on success.
21
+ * @returns {Promise<XPResult>} - Object of user data on success.
20
22
  * @throws {XpFatal} - If parameters are not provided correctly.
21
- */async function addXP(userId,guildId,xp){if(!("number"==typeof xp||"object"==typeof xp&&xp.min&&xp.max))throw new xplogs_1.XpFatal({function:"addXP()",message:"XP is not a number or object, make sure you are using the correct syntax"});if("object"==typeof xp&&(xp=Math.floor(Math.random()*(xp.max-xp.min)+xp.min)),!userId)throw new xplogs_1.XpFatal({function:"addXP()",message:"User ID was not provided"});var e;if(guildId)return(e=await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}}))?database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:(0,convert_1.convert)("xp",e.xp+xp),xp:e.xp+xp}}):database_1.db.createOne({collection:"simply-xps",data:{user:userId,guild:guildId,level:(0,convert_1.convert)("xp",xp),xp:xp}});throw new xplogs_1.XpFatal({function:"addXP()",message:"Guild ID was not provided"})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.addXP=exports.addLevel=void 0;const convert_1=require("./functions/convert"),xplogs_1=require("./functions/xplogs"),database_1=require("./functions/database");exports.addLevel=addLevel,exports.addXP=addXP;
23
+ */async function addXP(userId,guildId,xpData,username){if(!("number"==typeof xpData||"object"==typeof xp_1.xp&&xpData.min&&xpData.max))throw new xplogs_1.XpFatal({function:"addXP()",message:"XP is not a number or object, make sure you are using the correct syntax"});if("object"==typeof xpData&&(xpData=Math.floor(Math.random()*(xpData.max-xpData.min)+xpData.min)),!userId)throw new xplogs_1.XpFatal({function:"addXP()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"addXP()",message:"Guild ID was not provided"});var e=await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}});let a;if(e)a=await database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:(0,utilities_1.convertFrom)(e.xp+xpData,"xp"),xp:e.xp+xpData}}).catch(err=>{throw new xplogs_1.XpFatal({function:"addXP()",message:err.stack})});else{if(!xp_1.xp.auto_create||!username)throw new xplogs_1.XpFatal({function:"addXP()",message:"User does not exist"});a=await database_1.db.createOne({collection:"simply-xps",data:{guild:guildId,user:userId,name:username,level:(0,utilities_1.convertFrom)(xpData,"xp"),xp:xpData}}).catch(err=>{throw new xplogs_1.XpFatal({function:"addXP()",message:err.stack})})}return{...a,hasLevelledUp:a.level>e.level}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.addXP=exports.addLevel=void 0;const utilities_1=require("./functions/utilities"),xplogs_1=require("./functions/xplogs"),database_1=require("./functions/database"),xp_1=require("../xp");exports.addLevel=addLevel,exports.addXP=addXP;
@@ -2,14 +2,15 @@
2
2
  import { User } from "./leaderboard";
3
3
  type HexColor = `#${string}` | `0x${string}`;
4
4
  /**
5
- * @property artworkColors - [HEX, RGB, RGBA]
6
- * @property borderColor - [HEX, RGB, RGBA]
7
- * @property backgroundColor - [HEX, RGB, RGBA]
8
- * @property artworkImage - [PNG, JPG, WEBP]
9
- * @property font - ABSOLUTE FILE PATH
10
- * @property light - Use light theme
5
+ * @property {[HexColor, HexColor]} artworkColors - Gradient colors
6
+ * @property {URL} artworkImage
7
+ * @property {[HexColor, HexColor]} borderColors - Gradient colors
8
+ * @property {HexColor} backgroundColor
9
+ * @property {URL} backgroundImage
10
+ * @property {string} font - ABSOLUTE FILE PATH
11
+ * @property {boolean} light - Use light theme
11
12
  */
12
- export type LeaderboardOptions = {
13
+ export interface LeaderboardOptions {
13
14
  artworkColors?: [HexColor, HexColor];
14
15
  artworkImage?: URL;
15
16
  borderColors?: [HexColor, HexColor];
@@ -17,19 +18,23 @@ export type LeaderboardOptions = {
17
18
  backgroundImage?: URL;
18
19
  font?: string;
19
20
  light?: boolean;
20
- };
21
+ }
21
22
  /**
22
- * @property background - [PNG, JPG, WEBP]
23
- * @property font - ABSOLUTE FILE PATH
23
+ * @property {URL} background - Background image URL
24
+ * @property {HexColor} color
25
+ * @property {boolean} legacy - Use legacy card design
26
+ * @property {HexColor} lvlbar
27
+ * @property {HexColor} lvlbarBg
28
+ * @property {string} font - ABSOLUTE FILE PATH
24
29
  */
25
- export type RankCardOptions = {
30
+ export interface RankCardOptions {
26
31
  background?: URL;
27
32
  color?: HexColor;
28
33
  legacy?: boolean;
29
34
  lvlbar?: HexColor;
30
35
  lvlbarBg?: HexColor;
31
36
  font?: string;
32
- };
37
+ }
33
38
  export type UserOptions = {
34
39
  id: string;
35
40
  username: string;
package/lib/src/cards.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * @link [Documentation](https://simplyxp.js.org/docs/rankCard)
10
10
  * @returns {Promise<{attachment: Buffer, description: string, name: string}>}
11
11
  * @throws {XpFatal} - If parameters are not provided correctly
12
- */async function rankCard(guild,user,options={},locales={}){if(!guild)throw new xplogs_1.XpFatal({function:"rankCard",message:"No Guild Provided"});if(!user)throw new xplogs_1.XpFatal({function:"rankCard",message:"No User Provided"});options.legacy=!0;let e,a;if(locales?.level||(locales.level="Level"),locales?.next_level||(locales.next_level="Next Level"),locales?.xp||(locales.xp="XP"),xplogs_1.XpLog.debug("rankCard","LEGACY MODE ENABLED"),xplogs_1.XpLog.info("rankCard","Modern RankCard is not supported yet, coming soon!"),!user?.avatarURL.endsWith(".png")&&!user.avatarURL.endsWith(".jpg")&&!user.avatarURL.endsWith(".webp"))throw new xplogs_1.XpFatal({function:"rankCard",message:"Invalid avatar image, avatar image must be a png, jpg, or webp"});if(!user||!user.id||!user.username)throw new xplogs_1.XpFatal({function:"rankCard",message:"Invalid User Provided, user must contain id, username, and avatarURL."});canvas_1.GlobalFonts.registerFromPath(options?.font||(0,path_1.join)(__dirname,"Fonts","Baloo-Regular.ttf"),"Sans Serif"),cachedRankImage=cachedRankImage||await(0,canvas_1.loadImage)(options?.background||"https://i.ibb.co/dck2Tnt/rank-card.webp");var o,t,r,l,n,d,i,s,c=await database_1.db.findOne({collection:"simply-xps",data:{guild:guild.id,user:user.id}});if(c)return o=(await database_1.db.find({collection:"simply-xps",data:{guild:guild.id}})).sort((a,b)=>b.xp-a.xp).findIndex(u=>u.user===user.id)+1,options?.legacy?(t=user.username.replace(/[\u007f-\uffff]/g,""),r=options?.color||"#9900ff",l=options?.lvlbar||"#ffffff",options=options?.lvlbarBg||"#ffffff",s=shortener(c.xp)+(" "+locales.xp),n=locales.level+(" "+shortener(c.level)),d=(0,convert_1.convert)("level",c.level+1),i=(0,convert_1.convert)("level",c.level),i=100*(c.xp-i)/(d-i)*660/100,cachedRankContext&&cachedRankCanvas?(e=cachedRankCanvas,a=cachedRankContext):(e=(0,canvas_1.createCanvas)(1080,400),RoundedBox(a=e.getContext("2d"),0,0,e.width,e.height,50),a.clip(),a.fillStyle="#000000",a.fillRect(0,0,1080,400),a.globalAlpha=.7,a.drawImage(cachedRankImage,-5,0,1090,400),a.restore(),a.fillStyle="#000000",a.globalAlpha=.4,a.fillRect(40,0,240,e.height),a.globalAlpha=1),a.save(),RoundedBox(a,70,30,180,180,50),a.strokeStyle=r,a.lineWidth=15,a.stroke(),a.clip(),a.drawImage(await(0,canvas_1.loadImage)(user.avatarURL),70,30,180,180),a.restore(),a.save(),RoundedBox(a,70,320,180,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=r,a.globalAlpha=1,a.fillRect(70,320,180,50),a.globalAlpha=1,a.fillStyle="#ffffff",a.textAlign="center",dynamicFont(a,s,160,358,160,32),a.restore(),a.save(),RoundedBox(a,70,240,180,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=r,a.globalAlpha=1,a.fillRect(70,240,180,50),a.globalAlpha=1,a.fillStyle="#ffffff",a.textAlign="center",dynamicFont(a,n,160,278,160,32),a.restore(),a.save(),a.textAlign="left",a.fillStyle="#ffffff",a.shadowColor="#000000",a.shadowBlur=15,a.shadowOffsetX=1,a.shadowOffsetY=1,a.font='39px "Sans Serif"',a.fillText(t,390,80),a.restore(),a.save(),a.textAlign="right",a.fillStyle="#ffffff",a.shadowColor="#000000",a.shadowBlur=15,a.shadowOffsetX=1,a.shadowOffsetY=1,a.font='55px "Sans Serif"',a.fillText("#"+o,e.width-55,80),a.restore(),a.save(),RoundedBox(a,390,305,660,70,Number(20)),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle="#ffffff",a.textAlign="center",dynamicFont(a,guild.name,720,355,700,45),a.globalAlpha=.2,a.fillRect(390,305,660,70),a.restore(),a.save(),RoundedBox(a,390,145,660,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=options,a.globalAlpha=.2,a.fillRect(390,145,660,50),a.restore(),a.save(),RoundedBox(a,390,145,i,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=l,a.globalAlpha=.5,a.fillRect(390,145,i,50),a.restore(),a.save(),a.textAlign="left",a.fillStyle="#ffffff",a.globalAlpha=.8,a.font='30px "Sans Serif"',a.fillText(locales.next_level+": "+shortener(d)+" "+locales.xp,390,230),a.restore(),s="{current} / {needed}".replace(/{needed}/g,shortener(d)).replace(/{current}/g,shortener(c.xp)),a.textAlign="center",a.fillStyle="#474747",a.globalAlpha=1,a.font='30px "Sans Serif"',a.fillText(s,730,180)):e=(0,canvas_1.createCanvas)(1080,360),{attachment:e.toBuffer("image/png"),description:"Simply-XP Rank Card",name:"rank.png"};throw new xplogs_1.XpFatal({function:"rankCard",message:"User not found in database"})}
12
+ */async function rankCard(guild,user,options={},locales={}){if(!guild)throw new xplogs_1.XpFatal({function:"rankCard()",message:"No Guild Provided"});if(!user)throw new xplogs_1.XpFatal({function:"rankCard()",message:"No User Provided"});options.legacy=!0;let e,a;if(locales?.level||(locales.level="Level"),locales?.next_level||(locales.next_level="Next Level"),locales?.xp||(locales.xp="XP"),xplogs_1.XpLog.debug("rankCard()","LEGACY MODE ENABLED"),xplogs_1.XpLog.info("rankCard()","Modern RankCard is not supported yet, coming soon!"),!user?.avatarURL.endsWith(".png")&&!user.avatarURL.endsWith(".jpg")&&!user.avatarURL.endsWith(".webp"))throw new xplogs_1.XpFatal({function:"rankCard()",message:"Invalid avatar image, avatar image must be a png, jpg, or webp"});if(!user||!user.id||!user.username)throw new xplogs_1.XpFatal({function:"rankCard()",message:"Invalid User Provided, user must contain id, username, and avatarURL."});canvas_1.GlobalFonts.registerFromPath(options?.font||(0,path_1.join)(__dirname,"Fonts","Baloo-Regular.eot"),"Sans Serif"),cachedRankImage=cachedRankImage||await(0,canvas_1.loadImage)(options?.background||"https://i.ibb.co/dck2Tnt/rank-card.webp");let o=await database_1.db.findOne({collection:"simply-xps",data:{guild:guild.id,user:user.id}});if(!o){if(!xp_1.xp.auto_create)throw new xplogs_1.XpFatal({function:"rankCard()",message:"User not found in database"});o=await(0,xp_1.create)(guild.id,user.id,user.username)}var t,r,l,n,d,i,s,c=(await database_1.db.find({collection:"simply-xps",data:{guild:guild.id}})).sort((a,b)=>b.xp-a.xp).findIndex(u=>u.user===user.id)+1;return options?.legacy?(t=user.username.replace(/[\u007f-\uffff]/g,""),r=options?.color||"#9900ff",l=options?.lvlbar||"#ffffff",options=options?.lvlbarBg||"#ffffff",s=shortener(o.xp)+(" "+locales.xp),n=locales.level+(" "+shortener(o.level)),d=(0,utilities_1.convertFrom)(o.level+1),i=(0,utilities_1.convertFrom)(o.level),i=100*(o.xp-i)/(d-i)*660/100,cachedRankContext&&cachedRankCanvas?(e=cachedRankCanvas,a=cachedRankContext):(e=(0,canvas_1.createCanvas)(1080,400),RoundedBox(a=e.getContext("2d"),0,0,e.width,e.height,50),a.clip(),a.fillStyle="#000000",a.fillRect(0,0,1080,400),a.globalAlpha=.7,a.drawImage(cachedRankImage,-5,0,1090,400),a.restore(),a.fillStyle="#000000",a.globalAlpha=.4,a.fillRect(40,0,240,e.height),a.globalAlpha=1),a.save(),RoundedBox(a,70,30,180,180,50),a.strokeStyle=r,a.lineWidth=15,a.stroke(),a.clip(),a.drawImage(await(0,canvas_1.loadImage)(user.avatarURL),70,30,180,180),a.restore(),a.save(),RoundedBox(a,70,320,180,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=r,a.globalAlpha=1,a.fillRect(70,320,180,50),a.globalAlpha=1,a.fillStyle="#ffffff",a.textAlign="center",dynamicFont(a,s,160,358,160,32),a.restore(),a.save(),RoundedBox(a,70,240,180,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=r,a.globalAlpha=1,a.fillRect(70,240,180,50),a.globalAlpha=1,a.fillStyle="#ffffff",a.textAlign="center",dynamicFont(a,n,160,278,160,32),a.restore(),a.save(),a.textAlign="left",a.fillStyle="#ffffff",a.shadowColor="#000000",a.shadowBlur=15,a.shadowOffsetX=1,a.shadowOffsetY=1,a.font='39px "Sans Serif"',a.fillText(t,390,80),a.restore(),a.save(),a.textAlign="right",a.fillStyle="#ffffff",a.shadowColor="#000000",a.shadowBlur=15,a.shadowOffsetX=1,a.shadowOffsetY=1,a.font='55px "Sans Serif"',a.fillText("#"+c,e.width-55,80),a.restore(),a.save(),RoundedBox(a,390,305,660,70,Number(20)),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle="#ffffff",a.textAlign="center",dynamicFont(a,guild.name,720,355,700,45),a.globalAlpha=.2,a.fillRect(390,305,660,70),a.restore(),a.save(),RoundedBox(a,390,145,660,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=options,a.globalAlpha=.2,a.fillRect(390,145,660,50),a.restore(),a.save(),RoundedBox(a,390,145,i,50,20),a.strokeStyle="#BFC85A22",a.stroke(),a.clip(),a.fillStyle=l,a.globalAlpha=.5,a.fillRect(390,145,i,50),a.restore(),a.save(),a.textAlign="left",a.fillStyle="#ffffff",a.globalAlpha=.8,a.font='30px "Sans Serif"',a.fillText(locales.next_level+": "+shortener(d)+" "+locales.xp,390,230),a.restore(),s="{current} / {needed}".replace(/{needed}/g,shortener(d)).replace(/{current}/g,shortener(o.xp)),a.textAlign="center",a.fillStyle="#474747",a.globalAlpha=1,a.font='30px "Sans Serif"',a.fillText(s,730,180)):e=(0,canvas_1.createCanvas)(1080,360),{attachment:e.toBuffer("image/png"),description:"Simply-XP Rank Card",name:"rank.png"}}
13
13
  /**
14
14
  * Generate a simple leaderboard card
15
15
  * @async
@@ -20,4 +20,4 @@
20
20
  * @link [Documentation](https://simplyxp.js.org/docs/leaderboard)
21
21
  * @returns {Promise<{attachment: Buffer, description: string, name: string}>}
22
22
  * @throws {XpFatal} - If parameters are not provided correctly
23
- */async function leaderboardCard(data,options={},guildInfo,locales={}){var e,a;if(!data||data.length<1)throw new xplogs_1.XpFatal({function:"leaderboardCard",message:"There must be at least 1 user in the data array"});!cachedLeaderboardArtwork&&options?.artworkImage&&(cachedLeaderboardArtwork=await(0,canvas_1.loadImage)(options.artworkImage)),!cachedLeaderboardImage&&options?.backgroundImage&&(cachedLeaderboardImage=await(0,canvas_1.loadImage)(options.backgroundImage)),locales.level||(locales.level="LEVEL"),locales.members||(locales.members="Members"),data=data.slice(0,8);let o,t,r,l=(r=options?.light?{artworkColors:options?.artworkColors||["#374bff","#5f69ff"],backgroundColor:"#f0f0eb",borderColors:options?.borderColors||["#ffa237","#ffcc6b"],evenColor:"#dcdcdc",oddColor:"#c8c8c8",primaryTextColor:"#000000",secondaryTextColor:"rgba(0,0,0,0.5)"}:{artworkColors:options?.artworkColors||["#374bff","#333793"],backgroundColor:"#141414",borderColors:options?.borderColors||["#ffa237","#b67125"],evenColor:"#1e1e1e",oddColor:"#282828",primaryTextColor:"#ffffff",secondaryTextColor:"rgba(255,255,255,0.5)"},cachedLeaderboardCanvas&&cachedLeaderboardContext?(o=cachedLeaderboardCanvas,t=cachedLeaderboardContext):(o=(0,canvas_1.createCanvas)(1350,1080),RoundedBox(t=o.getContext("2d"),0,0,o.width,o.height,20),t.clip(),(e=t.createLinearGradient(0,0,o.width,0)).addColorStop(0,r.artworkColors[0]),e.addColorStop(1,r.artworkColors[1]),t.fillStyle=e,t.fillRect(0,0,o.width,220),cachedLeaderboardArtwork&&(t.fillStyle="#000000",t.fillRect(0,0,o.width,220),t.globalAlpha=.5,t.drawImage(cachedLeaderboardArtwork,0,0,o.width,220),t.globalAlpha=1),t.fillStyle=options.backgroundColor||r.backgroundColor,t.fillRect(0,220,o.width,1080),cachedLeaderboardImage&&(t.globalAlpha=.9,t.drawImage(cachedLeaderboardImage,0,220,o.width,1080),t.globalAlpha=1)),guildInfo&&guildInfo?.imageURL&&guildInfo?.name&&guildInfo?.memberCount&&(e=await(0,canvas_1.loadImage)(guildInfo.imageURL),t.save(),t.beginPath(),t.arc(150,110,90,0,2*Math.PI,!0),t.closePath(),t.clip(),t.drawImage(e,60,20,180,180),t.restore(),(options=t.createLinearGradient(0,0,0,220)).addColorStop(0,r.borderColors[0]),options.addColorStop(1,r.borderColors[1]),t.strokeStyle=options,t.lineWidth=10,t.beginPath(),t.arc(150,110,90,0,2*Math.PI,!0),t.stroke(),t.fillStyle=r.primaryTextColor,t.font='60px "Sans Serif"',t.fillText(guildInfo.name,270,110),t.fillStyle=r.secondaryTextColor,t.font='40px "Sans Serif"',t.fillText(guildInfo.memberCount+" "+locales.members,270,160)),r.evenColor);for(let e=0;e<data.length;e++)a=300+90*e,1===data.length?RoundedBox(t,30,a,1290,90,20):0===e?RoundedBox(t,30,a,1290,90,20,{top:!0,bottom:!1}):e===data.length-1?RoundedBox(t,30,a,1290,90,20,{top:!1,bottom:!0}):RoundedBox(t,30,a,1290,90,0),t.fillStyle=l,t.globalAlpha=cachedLeaderboardImage?.5:1,t.fill(),t.globalAlpha=1,t.textAlign="left",t.font='30px "Sans Serif"',t.fillStyle=r.secondaryTextColor,t.fillText(e+1+".",60,55+a),t.textAlign="left",t.font='40px "Sans Serif"',t.fillStyle=r.primaryTextColor,t.fillText(data[e]?.name||data[e]?.user||"???",120,60+a),t.textAlign="right",t.font='30px "Sans Serif"',t.fillStyle=r.primaryTextColor,t.fillText(shortener(data[e]?.level)||"???",1270,55+a),t.fillStyle=r.secondaryTextColor,t.fillText(locales.level,1270-t.measureText(shortener(data[e]?.level)||"???").width-15,55+a),l=l===r.evenColor?r.oddColor:r.evenColor;return{attachment:o.toBuffer("image/png"),description:"Simply-XP Leaderboard Card",name:"leaderboard.png"}}function RoundedBox(ctx,x,y,width,height,radius,roundCorners={top:!0,bottom:!0}){ctx.beginPath(),ctx.moveTo(x+(roundCorners.top?radius:0),y),ctx.lineTo(x+width-(roundCorners.top?radius:0),y),roundCorners.top&&ctx.quadraticCurveTo(x+width,y,x+width,y+radius),ctx.lineTo(x+width,y+height-(roundCorners.bottom?radius:0)),roundCorners.bottom&&ctx.quadraticCurveTo(x+width,y+height,x+width-radius,y+height),ctx.lineTo(x+(roundCorners.bottom?radius:0),y+height),roundCorners.bottom&&ctx.quadraticCurveTo(x,y+height,x,y+height-radius),ctx.lineTo(x,y+(roundCorners.top?radius:0)),roundCorners.top&&ctx.quadraticCurveTo(x,y,x+radius,y),ctx.closePath()}function shortener(count){var e,a=["","k","M","B","T","Qa","Qi","Sx","Sp"];return count&&0!==count?count===1/0?"∞":(e=Math.floor(Math.log(count)/Math.log(1e3)),a.length<=e?count.toFixed(0)+a[a.length-1]:(count/Math.pow(1e3,e)).toFixed(0===e?0:2)+a[e]):"0"}function dynamicFont(context,text,x,y,maxWidth,maxSize){let e=maxSize;for(;0<e&&(context.font=e+'px "Sans Serif"',!(context.measureText(text).width<maxWidth));)e--;context.textAlign="center",context.fillText(text,x,y)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.leaderboardCard=exports.rankCard=void 0;const canvas_1=require("@napi-rs/canvas"),xplogs_1=require("./functions/xplogs"),database_1=require("./functions/database"),convert_1=require("./functions/convert"),path_1=require("path");let cachedRankImage,cachedRankCanvas,cachedRankContext,cachedLeaderboardArtwork,cachedLeaderboardCanvas,cachedLeaderboardContext,cachedLeaderboardImage;exports.rankCard=rankCard,exports.leaderboardCard=leaderboardCard;
23
+ */async function leaderboardCard(data,options={},guildInfo,locales={}){var e,a;if(!data||data.length<1)throw new xplogs_1.XpFatal({function:"leaderboardCard()",message:"There must be at least 1 user in the data array"});!cachedLeaderboardArtwork&&options?.artworkImage&&(cachedLeaderboardArtwork=await(0,canvas_1.loadImage)(options.artworkImage)),!cachedLeaderboardImage&&options?.backgroundImage&&(cachedLeaderboardImage=await(0,canvas_1.loadImage)(options.backgroundImage)),canvas_1.GlobalFonts.registerFromPath(options?.font||(0,path_1.join)(__dirname,"Fonts","Baloo-Regular.eot"),"Sans Serif"),locales.level||(locales.level="LEVEL"),locales.members||(locales.members="Members"),data=data.slice(0,8);let o,t,r,l=(r=options?.light?{artworkColors:options?.artworkColors||["#374bff","#5f69ff"],backgroundColor:"#f0f0eb",borderColors:options?.borderColors||["#ffa237","#ffcc6b"],evenColor:"#dcdcdc",oddColor:"#c8c8c8",primaryTextColor:"#000000",secondaryTextColor:"rgba(0,0,0,0.5)"}:{artworkColors:options?.artworkColors||["#374bff","#333793"],backgroundColor:"#141414",borderColors:options?.borderColors||["#ffa237","#b67125"],evenColor:"#1e1e1e",oddColor:"#282828",primaryTextColor:"#ffffff",secondaryTextColor:"rgba(255,255,255,0.5)"},cachedLeaderboardCanvas&&cachedLeaderboardContext?(o=cachedLeaderboardCanvas,t=cachedLeaderboardContext):(o=(0,canvas_1.createCanvas)(1350,1080),RoundedBox(t=o.getContext("2d"),0,0,o.width,o.height,20),t.clip(),(e=t.createLinearGradient(0,0,o.width,0)).addColorStop(0,r.artworkColors[0]),e.addColorStop(1,r.artworkColors[1]),t.fillStyle=e,t.fillRect(0,0,o.width,220),cachedLeaderboardArtwork&&(t.fillStyle="#000000",t.fillRect(0,0,o.width,220),t.globalAlpha=.5,t.drawImage(cachedLeaderboardArtwork,0,0,o.width,220),t.globalAlpha=1),t.fillStyle=options.backgroundColor||r.backgroundColor,t.fillRect(0,220,o.width,1080),cachedLeaderboardImage&&(t.globalAlpha=.9,t.drawImage(cachedLeaderboardImage,0,220,o.width,1080),t.globalAlpha=1)),guildInfo&&guildInfo?.imageURL&&guildInfo?.name&&guildInfo?.memberCount&&(e=await(0,canvas_1.loadImage)(guildInfo.imageURL),t.save(),t.beginPath(),t.arc(150,110,90,0,2*Math.PI,!0),t.closePath(),t.clip(),t.drawImage(e,60,20,180,180),t.restore(),(options=t.createLinearGradient(0,0,0,220)).addColorStop(0,r.borderColors[0]),options.addColorStop(1,r.borderColors[1]),t.strokeStyle=options,t.lineWidth=10,t.beginPath(),t.arc(150,110,90,0,2*Math.PI,!0),t.stroke(),t.fillStyle=r.primaryTextColor,t.font='60px "Sans Serif"',t.fillText(guildInfo.name,270,110),t.fillStyle=r.secondaryTextColor,t.font='40px "Sans Serif"',t.fillText(guildInfo.memberCount+" "+locales.members,270,160)),r.evenColor);for(let e=0;e<data.length;e++)a=300+90*e,1===data.length?RoundedBox(t,30,a,1290,90,20):0===e?RoundedBox(t,30,a,1290,90,20,{top:!0,bottom:!1}):e===data.length-1?RoundedBox(t,30,a,1290,90,20,{top:!1,bottom:!0}):RoundedBox(t,30,a,1290,90,0),t.fillStyle=l,t.globalAlpha=cachedLeaderboardImage?.5:1,t.fill(),t.globalAlpha=1,t.textAlign="left",t.font='30px "Sans Serif"',t.fillStyle=r.secondaryTextColor,t.fillText(e+1+".",60,55+a),t.textAlign="left",t.font='40px "Sans Serif"',t.fillStyle=r.primaryTextColor,t.fillText(data[e]?.name||data[e]?.user||"???",120,60+a),t.textAlign="right",t.font='30px "Sans Serif"',t.fillStyle=r.primaryTextColor,t.fillText(shortener(data[e]?.level)||"???",1270,55+a),t.fillStyle=r.secondaryTextColor,t.fillText(locales.level,1270-t.measureText(shortener(data[e]?.level)||"???").width-15,55+a),l=l===r.evenColor?r.oddColor:r.evenColor;return{attachment:o.toBuffer("image/png"),description:"Simply-XP Leaderboard Card",name:"leaderboard.png"}}function RoundedBox(ctx,x,y,width,height,radius,roundCorners={top:!0,bottom:!0}){ctx.beginPath(),ctx.moveTo(x+(roundCorners.top?radius:0),y),ctx.lineTo(x+width-(roundCorners.top?radius:0),y),roundCorners.top&&ctx.quadraticCurveTo(x+width,y,x+width,y+radius),ctx.lineTo(x+width,y+height-(roundCorners.bottom?radius:0)),roundCorners.bottom&&ctx.quadraticCurveTo(x+width,y+height,x+width-radius,y+height),ctx.lineTo(x+(roundCorners.bottom?radius:0),y+height),roundCorners.bottom&&ctx.quadraticCurveTo(x,y+height,x,y+height-radius),ctx.lineTo(x,y+(roundCorners.top?radius:0)),roundCorners.top&&ctx.quadraticCurveTo(x,y,x+radius,y),ctx.closePath()}function shortener(count){var e,a=["","k","M","B","T","Qa","Qi","Sx","Sp"];return count&&0!==count?count===1/0?"∞":(e=Math.floor(Math.log(count)/Math.log(1e3)),a.length<=e?count.toFixed(0)+a[a.length-1]:(count/Math.pow(1e3,e)).toFixed(0===e?0:2)+a[e]):"0"}function dynamicFont(context,text,x,y,maxWidth,maxSize){let e=maxSize;for(;0<e&&(context.font=e+'px "Sans Serif"',!(context.measureText(text).width<maxWidth));)e--;context.textAlign="center",context.fillText(text,x,y)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.leaderboardCard=exports.rankCard=void 0;const canvas_1=require("@napi-rs/canvas"),xplogs_1=require("./functions/xplogs"),database_1=require("./functions/database"),utilities_1=require("./functions/utilities"),path_1=require("path"),xp_1=require("../xp");let cachedRankImage,cachedRankCanvas,cachedRankContext,cachedLeaderboardArtwork,cachedLeaderboardCanvas,cachedLeaderboardContext,cachedLeaderboardImage;exports.rankCard=rankCard,exports.leaderboardCard=leaderboardCard;
@@ -1,6 +1,7 @@
1
1
  export type ConnectionOptions = {
2
2
  type: "mongodb" | "sqlite" | undefined;
3
- auto_purge?: false | unknown;
3
+ auto_create?: boolean;
4
+ auto_purge?: boolean;
4
5
  notify?: boolean;
5
6
  debug?: boolean;
6
7
  };
@@ -15,3 +16,11 @@ export type ConnectionOptions = {
15
16
  * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
16
17
  */
17
18
  export declare function connect(uri: string, options?: ConnectionOptions): Promise<boolean | void>;
19
+ /**
20
+ * Check database package versions
21
+ * @private
22
+ * @param {"mongodb" | "sqlite"} type
23
+ * @returns {Promise<boolean>}
24
+ * @throws {XpFatal} If the package version is not supported
25
+ */
26
+ export declare function checkPackageVersion(type: "mongodb" | "sqlite"): Promise<boolean>;
@@ -8,7 +8,7 @@
8
8
  * @link `Documentation:` https://simplyxp.js.org/docs/connect
9
9
  * @returns {Promise<boolean>}
10
10
  * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
11
- */async function connect(uri,options={type:void 0}){var e,o,t,{type:n,auto_purge:r,notify:a,debug:i}=options;if(!uri)throw new xplogs_1.XpFatal({function:"connect()",message:"No URI Provided"});switch(!1===a&&(xp_1.xp.notify=!1),r&&(xp_1.xp.auto_purge=!0),i&&(xp_1.xp.debug=!0),n||(options.type="mongodb",xplogs_1.XpLog.warn("connect()","Database type not provided, defaulting to MongoDB")),n){case"mongodb":try{if(t=(await Promise.resolve().then(()=>__importStar(require("mongodb")))).MongoClient,!(await Promise.resolve().then(()=>__importStar(require("mongodb/package.json")))).version.startsWith("5"))return xplogs_1.XpLog.err("connect()","MongoDB V5 is required");e=await t.connect(uri).catch(error=>{throw new xplogs_1.XpFatal({function:"connect()",message:error.message})}),xp_1.xp.dbType="mongodb",xp_1.xp.database=e||void 0}catch(e){xplogs_1.XpLog.info("connect()","Installing MongoDB [5.x] | Please wait..."),(0,child_process_1.execSync)(await getPackageManager()+" add mongodb@5.x.x"),xplogs_1.XpLog.warn("connect()","Installed MongoDB. Please restart!"),process.exit(1)}break;case"sqlite":try{if(o=await Promise.resolve().then(()=>__importStar(require("better-sqlite3"))),!(await Promise.resolve().then(()=>__importStar(require("better-sqlite3/package.json")))).version.startsWith("8"))return xplogs_1.XpLog.err("connect()","SQLite V8 is required");xp_1.xp.database=new o.default(uri),xp_1.xp.dbType="sqlite",xp_1.xp.database.exec(`CREATE TABLE IF NOT EXISTS "simply-xps"
11
+ */async function connect(uri,options={type:void 0}){var e,{type:t,auto_create:o,auto_purge:r,notify:a,debug:n}=options;if(!uri)throw new xplogs_1.XpFatal({function:"connect()",message:"No URI Provided"});switch(!1===a&&(xp_1.xp.notify=!1),o&&(xp_1.xp.auto_create=!0),r&&(xp_1.xp.auto_purge=!0),n&&(xp_1.xp.debug=!0),t||(options.type="mongodb",xplogs_1.XpLog.warn("connect()","Database type not provided, defaulting to MongoDB")),t){case"mongodb":if(e=(await Promise.resolve().then(()=>__importStar(require("mongodb")))).MongoClient,!await checkPackageVersion("mongodb"))return xplogs_1.XpLog.err("connect()","MongoDB V4 or higher is required");e=await e.connect(uri).catch(error=>{throw new xplogs_1.XpFatal({function:"connect()",message:error.message})}),xp_1.xp.dbType="mongodb",xp_1.xp.database=e||void 0;break;case"sqlite":try{var[s,i]=await Promise.all([Promise.resolve().then(()=>__importStar(require("better-sqlite3"))),checkPackageVersion("sqlite")]);if(!i)return xplogs_1.XpLog.err("connect()","better-sqlite3 V7 or higher is required");xp_1.xp.database=new s.default(uri),xp_1.xp.dbType="sqlite",xp_1.xp.database.exec(`CREATE TABLE IF NOT EXISTS "simply-xps"
12
12
  (
13
13
  user
14
14
  TEXT
@@ -32,9 +32,16 @@
32
32
  TEXT
33
33
  NOT
34
34
  NULL
35
- )`)}catch(e){if("object"==typeof e&&null!==e&&void 0!==(t=e).code&&"MODULE_NOT_FOUND"!==t.code)throw new xplogs_1.XpFatal({function:"connect()",message:t.message});xplogs_1.XpLog.info("connect()","Installing better-sqlite3 [V8]..."),(0,child_process_1.execSync)(await getPackageManager()+" add better-sqlite3@8.x.x"),xplogs_1.XpLog.warn("connect()","Installed better-sqlite3. | Please reboot!"),process.exit(0)}break;default:throw new xplogs_1.XpFatal({function:"connect()",message:"DATABASE TYPE NOT PROVIDED OR INVALID"})}return!!xp_1.xp.database&&(xplogs_1.XpLog.info("connect()","Connected to database!"),!0)}
35
+ )`)}catch(t){if("object"==typeof t&&null!==t&&void 0!==(e=t).code&&"MODULE_NOT_FOUND"!==e.code)throw new xplogs_1.XpFatal({function:"connect()",message:e.message})}break;default:throw new xplogs_1.XpFatal({function:"connect()",message:"DATABASE TYPE NOT PROVIDED OR INVALID"})}return!!xp_1.xp.database&&(xplogs_1.XpLog.info("connect()","Connected to database!"),!0)}
36
36
  /**
37
37
  * Returns the package manager used
38
38
  * @private
39
39
  * @returns {Promise<"yarn" | "npm" | "pnpm">}
40
- */async function getPackageManager(){const e=(await Promise.resolve().then(()=>__importStar(require("fs")))).existsSync;var o=["yarn.lock","pnpm-lock.yaml","pnpm-lock.json","package-lock.json"].filter(lockfile=>e(lockfile));if(1===o.length){if("yarn.lock"===o[0])return xplogs_1.XpLog.debug("getPackageManager()","Using Yarn"),"yarn";if("pnpm-lock.yaml"===o[0]||"pnpm-lock.json"===o[0])return xplogs_1.XpLog.debug("getPackageManager()","Using PNPM"),"pnpm"}return xplogs_1.XpLog.debug("getPackageManager()","Using NPM"),"npm"}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,o;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(o in mod)"default"!==o&&Object.prototype.hasOwnProperty.call(mod,o)&&__createBinding(e,mod,o);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.connect=void 0;const xplogs_1=require("./functions/xplogs"),child_process_1=require("child_process"),xp_1=require("../xp");exports.connect=connect;
40
+ */async function getPackageManager(){const e=(await Promise.resolve().then(()=>__importStar(require("fs")))).existsSync;var t=["yarn.lock","pnpm-lock.yaml","pnpm-lock.json","package-lock.json"].filter(lockfile=>e(lockfile));if(1===t.length){if("yarn.lock"===t[0])return xplogs_1.XpLog.debug("getPackageManager()","Using Yarn"),"yarn";if("pnpm-lock.yaml"===t[0]||"pnpm-lock.json"===t[0])return xplogs_1.XpLog.debug("getPackageManager()","Using PNPM"),"pnpm"}return xplogs_1.XpLog.debug("getPackageManager()","Using NPM"),"npm"}
41
+ /**
42
+ * Check database package versions
43
+ * @private
44
+ * @param {"mongodb" | "sqlite"} type
45
+ * @returns {Promise<boolean>}
46
+ * @throws {XpFatal} If the package version is not supported
47
+ */async function checkPackageVersion(type){var e,t;switch(type){case"mongodb":try{return e=await Promise.resolve().then(()=>__importStar(require("mongodb/package.json"))),4<=parseInt(e.version.substring(0,1))}catch(e){return xplogs_1.XpLog.info("checkPackageVersion()","Installing MongoDB [5.x] | Please wait..."),(0,child_process_1.execSync)(await getPackageManager()+" add mongodb@5.x.x"),xplogs_1.XpLog.warn("checkPackageVersion()","Installed MongoDB. Please restart!"),process.exit(1)}case"sqlite":try{return t=await Promise.resolve().then(()=>__importStar(require("better-sqlite3/package.json"))),7<=parseInt(t.version.substring(0,1))}catch(e){return xplogs_1.XpLog.info("checkPackageVersion()","Installing better-sqlite3 [V8] | Please wait..."),(0,child_process_1.execSync)(await getPackageManager()+" add better-sqlite3@8.x.x"),xplogs_1.XpLog.warn("checkPackageVersion()","Installed better-sqlite3. Please restart!"),process.exit(1)}}}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkPackageVersion=exports.connect=void 0;const xplogs_1=require("./functions/xplogs"),child_process_1=require("child_process"),xp_1=require("../xp");exports.connect=connect,exports.checkPackageVersion=checkPackageVersion;
@@ -1,11 +1,12 @@
1
+ import { UserResult } from "./functions/database";
1
2
  /**
2
3
  * Create a new user in the database
3
4
  * @async
4
- * @param {string} guildId
5
5
  * @param {string} userId
6
+ * @param {string} guildId
6
7
  * @param {string} username
7
8
  * @link `Documentation:` https://simplyxp.js.org/docs/create
8
- * @returns {Promise<object>}
9
+ * @returns {Promise<UserResult>}
9
10
  * @throws {XpFatal} If invalid parameters are provided
10
11
  */
11
- export declare function create(guildId: string, userId: string, username: string): Promise<object>;
12
+ export declare function create(userId: string, guildId: string, username: string): Promise<UserResult>;
package/lib/src/create.js CHANGED
@@ -2,10 +2,10 @@
2
2
  /**
3
3
  * Create a new user in the database
4
4
  * @async
5
- * @param {string} guildId
6
5
  * @param {string} userId
6
+ * @param {string} guildId
7
7
  * @param {string} username
8
8
  * @link `Documentation:` https://simplyxp.js.org/docs/create
9
- * @returns {Promise<object>}
9
+ * @returns {Promise<UserResult>}
10
10
  * @throws {XpFatal} If invalid parameters are provided
11
- */async function create(guildId,userId,username){if(!guildId)throw new xplogs_1.XpFatal({function:"create()",message:"Guild ID was not provided"});if(!userId)throw new xplogs_1.XpFatal({function:"create()",message:"User ID was not provided"});var e;if(username)return await(e=(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db).findOne({collection:"simply-xps",data:{user:userId,guild:guildId}})||e.createOne({collection:"simply-xps",data:{name:username,user:userId,guild:guildId,level:0,xp:0}});throw new xplogs_1.XpFatal({function:"create()",message:"Username was not provided"})}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.create=void 0;const xplogs_1=require("./functions/xplogs");exports.create=create;
11
+ */async function create(userId,guildId,username){if(!userId)throw new xplogs_1.XpFatal({function:"create()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"create()",message:"Guild ID was not provided"});if(!username)throw new xplogs_1.XpFatal({function:"create()",message:"Username was not provided"});xplogs_1.XpLog.warn("create()","We just swapped userId with guildId again, this warning will be removed in the next DEV release.");var e=(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db;return await e.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}})||e.createOne({collection:"simply-xps",data:{name:username,user:userId,guild:guildId,level:0,xp:0}})}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.create=void 0;const xplogs_1=require("./functions/xplogs");exports.create=create;
@@ -1,17 +1,12 @@
1
+ import { User } from "./leaderboard";
1
2
  /**
2
3
  * Fetch user data
3
4
  * @async
4
5
  * @param {string} userId
5
6
  * @param {string} guildId
7
+ * @param {string} username - Username to use if auto_create is enabled
6
8
  * @link `Documentation:` https://simplyxp.js.org/docs/fetch
7
9
  * @returns {Promise<{name: string | null, user: string, guild: string, level: number, position: number, xp: number}>}
8
10
  * @throws {XpFatal} If invalid parameters are provided, or if the user data is not found.
9
11
  */
10
- export declare function fetch(userId: string, guildId: string): Promise<{
11
- name: string | null;
12
- user: string;
13
- guild: string;
14
- level: number;
15
- position: number;
16
- xp: number;
17
- }>;
12
+ export declare function fetch(userId: string, guildId: string, username: string): Promise<User>;
package/lib/src/fetch.js CHANGED
@@ -4,7 +4,8 @@
4
4
  * @async
5
5
  * @param {string} userId
6
6
  * @param {string} guildId
7
+ * @param {string} username - Username to use if auto_create is enabled
7
8
  * @link `Documentation:` https://simplyxp.js.org/docs/fetch
8
9
  * @returns {Promise<{name: string | null, user: string, guild: string, level: number, position: number, xp: number}>}
9
10
  * @throws {XpFatal} If invalid parameters are provided, or if the user data is not found.
10
- */async function fetch(userId,guildId){if(!userId)throw new xplogs_1.XpFatal({function:"create()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"create()",message:"Guild ID was not provided"});var e=(guildId=await(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db.find({collection:"simply-xps",data:{guild:guildId}})).find(u=>u.user===userId);if(e)return guildId=guildId.sort((a,b)=>b.xp-a.xp).findIndex(u=>u.user===userId)+1,{name:e?.name||null,user:e.user,guild:e.guild,level:e.level,position:guildId,xp:e.xp};throw new xplogs_1.XpFatal({function:"fetch()",message:"User data not found"})}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.fetch=void 0;const xplogs_1=require("./functions/xplogs");exports.fetch=fetch;
11
+ */async function fetch(userId,guildId,username){if(!userId)throw new xplogs_1.XpFatal({function:"create()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"create()",message:"Guild ID was not provided"});var e=await(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db.find({collection:"simply-xps",data:{guild:guildId}}),t=e.find(u=>u.user===userId);if(t)return e=e.sort((a,b)=>b.xp-a.xp).findIndex(u=>u.user===userId)+1,{name:t?.name,user:t.user,guild:t.guild,level:t.level,position:e,xp:t.xp};if(xp_1.xp.auto_create&&username)return(await Promise.resolve().then(()=>__importStar(require("./create")))).create(guildId,userId,username);throw new xplogs_1.XpFatal({function:"fetch()",message:"User data not found"})}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.fetch=void 0;const xplogs_1=require("./functions/xplogs"),xp_1=require("../xp");exports.fetch=fetch;
@@ -1,4 +1,14 @@
1
- export type UserOptions = {
1
+ /**
2
+ * Options for creating a user document.
3
+ * @property {string} collection - The collection to create the document in.
4
+ * @property {object} data - The data to create the document with.
5
+ * @property {string} data.guild - The guild ID.
6
+ * @property {string} [data.user] - The user ID.
7
+ * @property {string} [data.name] - The username.
8
+ * @property {number} [data.level] - The level.
9
+ * @property {number} [data.xp] - The XP.
10
+ */
11
+ export interface UserOptions {
2
12
  collection: "simply-xps";
3
13
  data: {
4
14
  guild: string;
@@ -7,28 +17,48 @@ export type UserOptions = {
7
17
  level?: number;
8
18
  xp?: number;
9
19
  };
10
- };
11
- export type UserResult = {
20
+ }
21
+ /**
22
+ * The result of a user document.
23
+ * @property {string} [_id] - The ID of the document.
24
+ * @property {string} user - The user ID.
25
+ * @property {string} [name] - The username.
26
+ * @property {string} guild - The guild ID.
27
+ * @property {number} level - The level.
28
+ * @property {number} xp - The XP.
29
+ */
30
+ export interface UserResult {
12
31
  _id?: string;
13
32
  user: string;
14
33
  name?: string;
15
34
  guild: string;
16
35
  level: number;
17
36
  xp: number;
18
- };
19
- export type LevelRoleOptions = {
37
+ }
38
+ /**
39
+ * Options for creating a level role document.
40
+ * @property {string} collection - The collection to create the document in.
41
+ * @property {object} data - The data to create the document with.
42
+ * @property {string} data.guild - The guild ID.
43
+ * @property {number} [data.level] - The level to assign the role at.
44
+ * @property {string | Array<string>} [data.roles] - The role(s) to assign.
45
+ * @property {string} data.timestamp - The timestamp of when the document was created.
46
+ */
47
+ export interface LevelRoleOptions {
20
48
  collection: "simply-xp-levelroles";
21
49
  data: {
22
50
  guild: string;
23
51
  level?: number;
24
52
  roles?: string | Array<string>;
53
+ timestamp: string;
25
54
  };
26
- };
55
+ }
27
56
  export type LevelRoleResult = {
28
57
  _id?: string;
29
58
  guild: string;
30
59
  level: number;
31
60
  roles: Array<string>;
61
+ timestamp: string;
32
62
  };
33
63
  /**
34
64
  * Database class providing methods to interact with the database.
@@ -0,0 +1,41 @@
1
+ import { MongoClient } from "mongodb";
2
+ import { Database } from "better-sqlite3";
3
+ /**
4
+ * Options for the XP client.
5
+ * @property {boolean} auto_create - Whether to automatically create a user if they don't exist in the database.
6
+ * @property {boolean} auto_purge - Whether to automatically purge inactive users.
7
+ * @property {object} dbOptions - The database options.
8
+ * @property {"mongodb" | "sqlite"} dbOptions.type
9
+ * @property {MongoClient | Database} dbOptions.database
10
+ * @property {boolean} notify - Enable/Disable console notifications.
11
+ * @property {boolean} debug - Whether to enable debug logs.
12
+ */
13
+ interface NewClientOptions {
14
+ auto_create: boolean;
15
+ auto_purge: boolean;
16
+ dbOptions: {
17
+ type: "mongodb" | "sqlite";
18
+ database: MongoClient | Database;
19
+ };
20
+ notify: boolean;
21
+ debug: boolean;
22
+ }
23
+ /**
24
+ * Convert XP to level and vice versa.
25
+ *
26
+ * @param {number} value.
27
+ * @param {"xp" | "level"} type - Type to convert from (Default: level).
28
+ * @link `Documentation:` https://simplyxp.js.org/docs/utilities/convert
29
+ * @returns {number} - The converted value. (XP to level or level to XP)
30
+ * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
31
+ */
32
+ export declare function convertFrom(value: number, type?: "xp" | "level"): number;
33
+ /**
34
+ * Updates the options of the XP client.
35
+ * @param {NewClientOptions} clientOptions - The new options to update.
36
+ * @link `Documentation:` https://simplyxp.js.org/docs/utilities/updateOptions
37
+ * @returns {void} - Nothing.
38
+ * @throws {XpFatal} If an invalid option is provided.
39
+ */
40
+ export declare function updateOptions(clientOptions: NewClientOptions): void;
41
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ /**
3
+ * Convert XP to level and vice versa.
4
+ *
5
+ * @param {number} value.
6
+ * @param {"xp" | "level"} type - Type to convert from (Default: level).
7
+ * @link `Documentation:` https://simplyxp.js.org/docs/utilities/convert
8
+ * @returns {number} - The converted value. (XP to level or level to XP)
9
+ * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
10
+ */function convertFrom(value,type="level"){if(!value)throw new xplogs_1.XpFatal({function:"convertFrom()",message:"Value was not provided"});if("xp"!==type&&"level"!==type)throw new xplogs_1.XpFatal({function:"convert()",message:"Invalid type provided"});if("level"===type)return Math.pow(value/.1,2);if("xp"===type)return Math.floor(.1*Math.sqrt(value));throw new xplogs_1.XpFatal({function:"convertFrom()",message:"Invalid type provided"})}
11
+ /**
12
+ * Updates the options of the XP client.
13
+ * @param {NewClientOptions} clientOptions - The new options to update.
14
+ * @link `Documentation:` https://simplyxp.js.org/docs/utilities/updateOptions
15
+ * @returns {void} - Nothing.
16
+ * @throws {XpFatal} If an invalid option is provided.
17
+ */function updateOptions(clientOptions){if(!clientOptions)throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Options were not provided"});if("object"!=typeof clientOptions)throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Options must be an object"});if(xp_1.xp.auto_create=clientOptions.auto_create,xp_1.xp.auto_purge=clientOptions.auto_purge,xp_1.xp.notify=clientOptions.notify,xp_1.xp.debug=clientOptions.debug,clientOptions.dbOptions&&"object"==typeof clientOptions.dbOptions&&clientOptions.dbOptions.type&&clientOptions.dbOptions.database){var{type:clientOptions,database:t}=clientOptions.dbOptions;if(!(clientOptions&&"mongodb"===clientOptions||"sqlite"===clientOptions))throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Invalid database type provided"});xp_1.xp.dbType=clientOptions,t&&(xp_1.xp.database=t,"mongodb"===xp_1.xp.dbType?(0,connect_1.checkPackageVersion)("mongodb").then(result=>{if(!result)throw new xplogs_1.XpFatal({function:"updateOptions()",message:"MongoDB V4 or higher is required"});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"})})}):"sqlite"===xp_1.xp.dbType&&(0,connect_1.checkPackageVersion)("sqlite").then(result=>{if(!result)throw new xplogs_1.XpFatal({function:"updateOptions()",message:"SQLite V7 or higher is required"});try{xp_1.xp.database.prepare("SELECT 1").get()}catch(t){throw new xplogs_1.XpFatal({function:"updateOptions()",message:"Invalid SQLite connection"})}}))}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateOptions=exports.convertFrom=void 0;const xplogs_1=require("./xplogs"),xp_1=require("../../xp"),connect_1=require("../connect");exports.convertFrom=convertFrom,exports.updateOptions=updateOptions;
@@ -1,11 +1,20 @@
1
- export type User = {
1
+ /**
2
+ * User object
3
+ * @property {string} guild - Guild ID
4
+ * @property {string} user - User ID
5
+ * @property {string} name - Username
6
+ * @property {number} position - Position in leaderboard
7
+ * @property {number} level - User level
8
+ * @property {number} xp - User XP
9
+ */
10
+ export interface User {
2
11
  guild: string;
3
12
  user: string;
4
- name?: string;
13
+ name?: string | null;
5
14
  position?: number;
6
15
  level: number;
7
16
  xp: number;
8
- };
17
+ }
9
18
  /**
10
19
  * Get array of all users in the leaderboard
11
20
  * @async
@@ -3,9 +3,10 @@
3
3
  * @async
4
4
  * @param {string} userId
5
5
  * @param {string} guildId
6
- * @param {boolean} erase - Erase user entry from database
6
+ * @param {string} username
7
+ * @param {boolean?} erase - Erase user entry from database
7
8
  * @link `Documentation:` https://simplyxp.js.org/docs/reset
8
9
  * @returns {Promise<boolean>}
9
10
  * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
10
11
  */
11
- export declare function reset(userId: string, guildId: string, erase: boolean): Promise<boolean>;
12
+ export declare function reset(userId: string, guildId: string, username: string, erase?: boolean): Promise<boolean>;
package/lib/src/reset.js CHANGED
@@ -4,8 +4,9 @@
4
4
  * @async
5
5
  * @param {string} userId
6
6
  * @param {string} guildId
7
- * @param {boolean} erase - Erase user entry from database
7
+ * @param {string} username
8
+ * @param {boolean?} erase - Erase user entry from database
8
9
  * @link `Documentation:` https://simplyxp.js.org/docs/reset
9
10
  * @returns {Promise<boolean>}
10
11
  * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
11
- */async function reset(userId,guildId,erase){if(!userId)throw new xplogs_1.XpFatal({function:"reset()",message:"No User ID Provided"});var e;if(guildId)return e=(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db,erase?e.deleteOne({collection:"simply-xps",data:{guild:guildId,user:userId}}).catch(error=>{throw new xplogs_1.XpFatal({function:"reset()",message:error})}):e.updateOne({collection:"simply-xps",data:{guild:guildId,user:userId}},{collection:"simply-xps",data:{guild:guildId,user:userId,level:0,xp:0}}).catch(error=>{throw new xplogs_1.XpFatal({function:"reset()",message:error})});throw new xplogs_1.XpFatal({function:"reset()",message:"No Guild ID Provided"})}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.reset=void 0;const xplogs_1=require("./functions/xplogs");exports.reset=reset;
12
+ */async function reset(userId,guildId,username,erase=!1){if(!userId)throw new xplogs_1.XpFatal({function:"reset()",message:"No User ID Provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"reset()",message:"No Guild ID Provided"});if(username)return!(await(username=(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db).findOne({collection:"simply-xps",data:{guild:guildId,user:userId}})||(xp_1.xp.auto_create&&!erase&&await username.createOne({collection:"simply-xps",data:{guild:guildId,user:userId,level:0,xp:0}}).then(()=>!0).catch(error=>{throw new xplogs_1.XpFatal({function:"reset()",message:error.stack})}),!erase))||(erase?username.deleteOne({collection:"simply-xps",data:{guild:guildId,user:userId}}).catch(error=>{throw new xplogs_1.XpFatal({function:"reset()",message:error})}):username.updateOne({collection:"simply-xps",data:{guild:guildId,user:userId}},{collection:"simply-xps",data:{guild:guildId,user:userId,level:0,xp:0}}).catch(error=>{throw new xplogs_1.XpFatal({function:"reset()",message:error})}));throw new xplogs_1.XpFatal({function:"reset()",message:"No Username Provided"})}var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){void 0===k2&&(k2=k);var e=Object.getOwnPropertyDescriptor(m,k);e&&("get"in e?m.__esModule:!e.writable&&!e.configurable)||(e={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,e)}:function(o,m,k,k2){o[k2=void 0===k2?k:k2]=m[k]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}:function(o,v){o.default=v}),__importStar=this&&this.__importStar||function(mod){var e,t;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(t in mod)"default"!==t&&Object.prototype.hasOwnProperty.call(mod,t)&&__createBinding(e,mod,t);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.reset=void 0;const xplogs_1=require("./functions/xplogs"),xp_1=require("../xp");exports.reset=reset;
@@ -1,8 +1,14 @@
1
- export type RoleSetupObject = {
1
+ /**
2
+ * Role setup object
3
+ * @property {string} guild - The guild ID
4
+ * @property {number} level - The level number
5
+ * @property {string[] | string} roles - The role(s) to add
6
+ */
7
+ export interface RoleSetupObject {
2
8
  guild?: string;
3
9
  level: number;
4
10
  roles: string[] | string;
5
- };
11
+ }
6
12
  /**
7
13
  * Setup roles for levels
8
14
  * @class roleSetup
@@ -8,7 +8,7 @@
8
8
  * @returns {Promise<boolean>} - True if successful
9
9
  * @throws {XpFatal} If an invalid type is provided or value is not provided.
10
10
  */
11
- static async add(guildId,options){if(!guildId)throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Guild ID was not provided"});if(!options)throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Options were not provided"});if(isNaN(options?.level))throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Level must be a number"});if(options?.roles)return"string"==typeof options?.roles&&(options.roles=[options.roles]),xp_1.db.createOne({collection:"simply-xp-levelroles",data:{guild:guildId,level:options.level,roles:options.roles}});throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Role was not provided"})}
11
+ static async add(guildId,options){if(!guildId)throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Guild ID was not provided"});if(!options)throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Options were not provided"});if(isNaN(options?.level))throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Level must be a number"});if(options?.roles)return"string"==typeof options?.roles&&(options.roles=[options.roles]),xp_1.db.createOne({collection:"simply-xp-levelroles",data:{guild:guildId,level:options.level,roles:options.roles,timestamp:(new Date).toISOString()}});throw new xplogs_1.XpFatal({function:"roleSetup.add()",message:"Role was not provided"})}
12
12
  /**
13
13
  * Find a role in roleSetup
14
14
  * @async
@@ -17,7 +17,7 @@ static async add(guildId,options){if(!guildId)throw new xplogs_1.XpFatal({functi
17
17
  * @link `Documentation:` https://simplyxp.js.org/docs/roleSetup/find
18
18
  * @returns {Promise<RoleSetupObject>} - The level role object
19
19
  * @throws {XpFatal} If an invalid type is provided or value is not provided.
20
- */static async find(guildId,levelNumber){if(!guildId)throw new xplogs_1.XpFatal({function:"roleSetup.find()",message:"Guild ID was not provided"});if(isNaN(levelNumber))throw new xplogs_1.XpFatal({function:"roleSetup.find()",message:"Level Number was not provided"});return xp_1.db.findOne({collection:"simply-xp-levelroles",data:{guild:guildId,level:levelNumber}})}
20
+ */static async find(guildId,levelNumber){if(!guildId)throw new xplogs_1.XpFatal({function:"roleSetup.find()",message:"Guild ID was not provided"});if(isNaN(levelNumber))throw new xplogs_1.XpFatal({function:"roleSetup.find()",message:"Level Number was not provided"});return xp_1.db.findOne({collection:"simply-xp-levelroles",data:{guild:guildId,level:levelNumber,timestamp:(new Date).toISOString()}})}
21
21
  /**
22
22
  * Remove a level from the role setup
23
23
  * @async
@@ -26,4 +26,4 @@ static async add(guildId,options){if(!guildId)throw new xplogs_1.XpFatal({functi
26
26
  * @link `Documentation:` https://simplyxp.js.org/docs/roleSetup/remove
27
27
  * @returns {Promise<boolean>} - True if successful
28
28
  * @throws {XpFatal} If an invalid type is provided or value is not provided.
29
- */static async remove(guildId,levelNumber){if(!guildId)throw new xplogs_1.XpFatal({function:"roleSetup.remove()",message:"Guild ID was not provided"});if(isNaN(levelNumber))throw new xplogs_1.XpFatal({function:"roleSetup.remove()",message:"Level Number was not provided"});return xp_1.db.deleteOne({collection:"simply-xp-levelroles",data:{guild:guildId,level:levelNumber}})}}exports.roleSetup=roleSetup;
29
+ */static async remove(guildId,levelNumber){if(!guildId)throw new xplogs_1.XpFatal({function:"roleSetup.remove()",message:"Guild ID was not provided"});if(isNaN(levelNumber))throw new xplogs_1.XpFatal({function:"roleSetup.remove()",message:"Level Number was not provided"});return xp_1.db.deleteOne({collection:"simply-xp-levelroles",data:{guild:guildId,level:levelNumber,timestamp:(new Date).toISOString()}})}}exports.roleSetup=roleSetup;
package/lib/src/set.d.ts CHANGED
@@ -1,32 +1,33 @@
1
+ import { UserResult } from "./functions/database";
1
2
  /**
2
3
  * Set user level
3
4
  * @async
4
5
  * @param {string} userId
5
6
  * @param {string} guildId
6
7
  * @param {number} level
8
+ * @param {string} username - Username to use if auto_create is enabled
7
9
  * @link `Documentation:` https://simplyxp.js.org/docs/setlevel
8
- * @returns {Promise<{user: string, guild: string, level: number, xp: number}>} - Object of user data on success
10
+ * @returns {Promise<UserResult>} - Object of user data on success
9
11
  * @throws {XpFatal} - If parameters are not provided correctly
10
12
  */
11
- export declare function setLevel(userId: string, guildId: string, level: number): Promise<{
12
- user: string;
13
- guild: string;
14
- level: number;
15
- xp: number;
16
- }>;
13
+ export declare function setLevel(userId: string, guildId: string, level: number, username?: string): Promise<UserResult>;
14
+ /**
15
+ * XP Results
16
+ * @property {boolean} hasLevelledUp - Whether the user has levelled up or not.
17
+ */
18
+ interface XPResult extends UserResult {
19
+ hasLevelledUp: boolean;
20
+ }
17
21
  /**
18
22
  * Set user XP
19
23
  * @async
20
24
  * @param {string} userId
21
25
  * @param {string} guildId
22
- * @param {number} xp
26
+ * @param {number} xpData
27
+ * @param {string} username - Username to use if auto_create is enabled
23
28
  * @link `Documentation:` https://simplyxp.js.org/docs/setxp
24
- * @returns {Promise<object>} - Object of user data on success
29
+ * @returns {Promise<XPResult>} - Object of user data on success
25
30
  * @throws {XpFatal} - If parameters are not provided correctly
26
31
  */
27
- export declare function setXP(userId: string, guildId: string, xp: number): Promise<{
28
- user: string;
29
- guild: string;
30
- level: number;
31
- xp: number;
32
- }>;
32
+ export declare function setXP(userId: string, guildId: string, xpData: number, username?: string): Promise<XPResult>;
33
+ export {};
package/lib/src/set.js CHANGED
@@ -5,17 +5,19 @@
5
5
  * @param {string} userId
6
6
  * @param {string} guildId
7
7
  * @param {number} level
8
+ * @param {string} username - Username to use if auto_create is enabled
8
9
  * @link `Documentation:` https://simplyxp.js.org/docs/setlevel
9
- * @returns {Promise<{user: string, guild: string, level: number, xp: number}>} - Object of user data on success
10
+ * @returns {Promise<UserResult>} - Object of user data on success
10
11
  * @throws {XpFatal} - If parameters are not provided correctly
11
- */async function setLevel(userId,guildId,level){if(!userId)throw new xplogs_1.XpFatal({function:"setLevel()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"setLevel()",message:"Guild ID was not provided"});if(level)return await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}})?database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:level,xp:(0,convert_1.convert)("level",level)}}):database_1.db.createOne({collection:"simply-xps",data:{user:userId,guild:guildId,level:level,xp:(0,convert_1.convert)("level",level)}});throw new xplogs_1.XpFatal({function:"setLevel()",message:"Level was not provided"})}
12
+ */async function setLevel(userId,guildId,level,username){if(!userId)throw new xplogs_1.XpFatal({function:"setLevel()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"setLevel()",message:"Guild ID was not provided"});if(!level)throw new xplogs_1.XpFatal({function:"setLevel()",message:"Level was not provided"});if(await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}}))return database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:level,xp:(0,utilities_1.convertFrom)(level)}});if(xp_1.xp.auto_create&&username)return database_1.db.createOne({collection:"simply-xps",data:{guild:guildId,user:userId,name:username,level:level,xp:(0,utilities_1.convertFrom)(level)}});throw new xplogs_1.XpFatal({function:"setLevel()",message:"User does not exist"})}
12
13
  /**
13
14
  * Set user XP
14
15
  * @async
15
16
  * @param {string} userId
16
17
  * @param {string} guildId
17
- * @param {number} xp
18
+ * @param {number} xpData
19
+ * @param {string} username - Username to use if auto_create is enabled
18
20
  * @link `Documentation:` https://simplyxp.js.org/docs/setxp
19
- * @returns {Promise<object>} - Object of user data on success
21
+ * @returns {Promise<XPResult>} - Object of user data on success
20
22
  * @throws {XpFatal} - If parameters are not provided correctly
21
- */async function setXP(userId,guildId,xp){if(!userId)throw new xplogs_1.XpFatal({function:"setXP()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"setXP()",message:"Guild ID was not provided"});if(xp)return await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}})?database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:(0,convert_1.convert)("xp",xp),xp:xp}}):database_1.db.createOne({collection:"simply-xps",data:{user:userId,guild:guildId,level:(0,convert_1.convert)("xp",xp),xp:xp}});throw new xplogs_1.XpFatal({function:"setXP()",message:"XP was not provided"})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.setXP=exports.setLevel=void 0;const xplogs_1=require("./functions/xplogs"),convert_1=require("./functions/convert"),database_1=require("./functions/database");exports.setLevel=setLevel,exports.setXP=setXP;
23
+ */async function setXP(userId,guildId,xpData,username){if(!userId)throw new xplogs_1.XpFatal({function:"setXP()",message:"User ID was not provided"});if(!guildId)throw new xplogs_1.XpFatal({function:"setXP()",message:"Guild ID was not provided"});if(!xpData)throw new xplogs_1.XpFatal({function:"setXP()",message:"XP was not provided"});var e=await database_1.db.findOne({collection:"simply-xps",data:{user:userId,guild:guildId}});let s;if(e)s=await database_1.db.updateOne({collection:"simply-xps",data:{user:userId,guild:guildId}},{collection:"simply-xps",data:{user:userId,guild:guildId,level:(0,utilities_1.convertFrom)(xpData),xp:xpData}});else{if(!xp_1.xp.auto_create||!username)throw new xplogs_1.XpFatal({function:"setXP()",message:"User does not exist"});s=await database_1.db.createOne({collection:"simply-xps",data:{guild:guildId,user:userId,name:username,level:(0,utilities_1.convertFrom)(xpData),xp:xpData}})}return{...s,hasLevelledUp:s.level>e.level}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.setXP=exports.setLevel=void 0;const xplogs_1=require("./functions/xplogs"),utilities_1=require("./functions/utilities"),database_1=require("./functions/database"),xp_1=require("../xp");exports.setLevel=setLevel,exports.setXP=setXP;
package/lib/xp.d.ts CHANGED
@@ -3,6 +3,7 @@ import { MongoClient } from "mongodb";
3
3
  export interface XPClient {
4
4
  dbType: "mongodb" | "sqlite";
5
5
  database: MongoClient | Database | undefined;
6
+ auto_create: boolean;
6
7
  auto_purge: boolean;
7
8
  notify: boolean;
8
9
  debug: boolean;
@@ -11,7 +12,7 @@ export { addLevel, addXP } from "./src/add";
11
12
  export { db } from "./src/functions/database";
12
13
  export { charts } from "./src/charts";
13
14
  export { connect } from "./src/connect";
14
- export { convert } from "./src/functions/convert";
15
+ export { convertFrom, updateOptions } from "./src/functions/utilities";
15
16
  export { create } from "./src/create";
16
17
  export { fetch } from "./src/fetch";
17
18
  export { leaderboard } from "./src/leaderboard";
package/lib/xp.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rank = exports.xp = exports.setXP = exports.setLevel = exports.roleSetup = exports.reset = exports.leaderboardCard = exports.rankCard = exports.migrate = exports.leaderboard = exports.fetch = exports.create = exports.convert = exports.connect = exports.charts = exports.db = exports.addXP = exports.addLevel = void 0;
3
+ exports.rank = exports.xp = exports.setXP = exports.setLevel = exports.roleSetup = exports.reset = exports.leaderboardCard = exports.rankCard = exports.migrate = exports.leaderboard = exports.fetch = exports.create = exports.updateOptions = exports.convertFrom = exports.connect = exports.charts = exports.db = exports.addXP = exports.addLevel = void 0;
4
4
  // EXPORTS
5
5
  var add_1 = require("./src/add");
6
6
  Object.defineProperty(exports, "addLevel", { enumerable: true, get: function () { return add_1.addLevel; } });
@@ -11,8 +11,9 @@ var charts_1 = require("./src/charts");
11
11
  Object.defineProperty(exports, "charts", { enumerable: true, get: function () { return charts_1.charts; } });
12
12
  var connect_1 = require("./src/connect");
13
13
  Object.defineProperty(exports, "connect", { enumerable: true, get: function () { return connect_1.connect; } });
14
- var convert_1 = require("./src/functions/convert");
15
- Object.defineProperty(exports, "convert", { enumerable: true, get: function () { return convert_1.convert; } });
14
+ var utilities_1 = require("./src/functions/utilities");
15
+ Object.defineProperty(exports, "convertFrom", { enumerable: true, get: function () { return utilities_1.convertFrom; } });
16
+ Object.defineProperty(exports, "updateOptions", { enumerable: true, get: function () { return utilities_1.updateOptions; } });
16
17
  var create_1 = require("./src/create");
17
18
  Object.defineProperty(exports, "create", { enumerable: true, get: function () { return create_1.create; } });
18
19
  var fetch_1 = require("./src/fetch");
@@ -32,6 +33,7 @@ var set_1 = require("./src/set");
32
33
  Object.defineProperty(exports, "setLevel", { enumerable: true, get: function () { return set_1.setLevel; } });
33
34
  Object.defineProperty(exports, "setXP", { enumerable: true, get: function () { return set_1.setXP; } });
34
35
  exports.xp = {
36
+ auto_create: false,
35
37
  auto_purge: false,
36
38
  database: undefined,
37
39
  debug: false,
@@ -41,4 +43,3 @@ exports.xp = {
41
43
  // DEPRECATED
42
44
  var rank_1 = require("./src/deprecated/rank");
43
45
  Object.defineProperty(exports, "rank", { enumerable: true, get: function () { return rank_1.rank; } });
44
- //# sourceMappingURL=xp.js.map
package/package.json CHANGED
@@ -1,70 +1,70 @@
1
- {
2
- "name": "simply-xp",
3
- "version": "2.0.0-dev.0-fix.2",
4
- "description": "The easiest way to implement xp system",
5
- "main": "lib/xp.js",
6
- "scripts": {
7
- "beta": "node Tests/clean.mjs && pnpm update && npm publish --tag beta",
8
- "clean": "node Tests/clean.mjs",
9
- "dev": "node Tests/clean.mjs && pnpm update && npm publish --tag dev",
10
- "docs": "node Tests/jsdocs.cjs",
11
- "latest": "node Tests/clean.mjs && pnpm update && npm publish",
12
- "test": "node Tests/clean.mjs && node Tests/test.cjs"
13
- },
14
- "author": "Rahuletto",
15
- "keywords": [
16
- "amaribot",
17
- "charts",
18
- "discord",
19
- "discord.js",
20
- "discord-xp",
21
- "easy",
22
- "fun",
23
- "guilded",
24
- "leaderboard",
25
- "level role",
26
- "level up",
27
- "mee6",
28
- "mongodb",
29
- "package",
30
- "simply",
31
- "simply-djs",
32
- "simplydjs",
33
- "sqlite",
34
- "system",
35
- "xp"
36
- ],
37
- "license": "Apache-2.0",
38
- "repository": {
39
- "type": "git",
40
- "url": "git+https://github.com/Rahuletto/simply-xp.git"
41
- },
42
- "dependencies": {
43
- "@napi-rs/canvas": "^0.1.41"
44
- },
45
- "devDependencies": {
46
- "@types/better-sqlite3": "^7.6.4",
47
- "@types/node": "^20.4.5",
48
- "@typescript-eslint/eslint-plugin": "^6.2.1",
49
- "@typescript-eslint/parser": "^6.2.1",
50
- "better-sqlite3": "8.5.0",
51
- "discord.js": "^14.12.1",
52
- "eslint": "^8.46.0",
53
- "guilded.ts": "^0.20.2",
54
- "jsdoc-to-markdown": "^8.0.0",
55
- "mongodb": "5.7.0",
56
- "typescript": "^5.1.6",
57
- "uglify-js": "^3.17.4"
58
- },
59
- "engines": {
60
- "node": ">=15.5.0"
61
- },
62
- "bugs": {
63
- "url": "https://github.com/Rahuletto/simply-xp/issues"
64
- },
65
- "homepage": "https://simplyxp.js.org",
66
- "directories": {
67
- "doc": "Mini_Docs",
68
- "lib": "lib"
69
- }
70
- }
1
+ {
2
+ "name": "simply-xp",
3
+ "version": "2.0.0-dev.2",
4
+ "description": "The easiest way to implement xp system",
5
+ "main": "lib/xp.js",
6
+ "scripts": {
7
+ "beta": "node Tests/clean.mjs && pnpm update && npm publish --tag beta",
8
+ "clean": "node Tests/clean.mjs",
9
+ "dev": "node Tests/clean.mjs && pnpm update && npm publish --tag dev",
10
+ "docs": "node Tests/jsdocs.cjs",
11
+ "latest": "node Tests/clean.mjs && pnpm update && npm publish",
12
+ "test": "node Tests/clean.mjs && node Tests/test.cjs"
13
+ },
14
+ "author": "Abadima",
15
+ "keywords": [
16
+ "amaribot",
17
+ "charts",
18
+ "discord",
19
+ "discord.js",
20
+ "discord-xp",
21
+ "easy",
22
+ "fun",
23
+ "guilded",
24
+ "leaderboard",
25
+ "level role",
26
+ "level up",
27
+ "mee6",
28
+ "mongodb",
29
+ "package",
30
+ "simply",
31
+ "simply-djs",
32
+ "simplydjs",
33
+ "sqlite",
34
+ "system",
35
+ "xp"
36
+ ],
37
+ "license": "Apache-2.0",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/Rahuletto/simply-xp.git"
41
+ },
42
+ "dependencies": {
43
+ "@napi-rs/canvas": "^0.1.41"
44
+ },
45
+ "devDependencies": {
46
+ "@types/better-sqlite3": "^7.6.4",
47
+ "@types/node": "^20.4.8",
48
+ "@typescript-eslint/eslint-plugin": "^6.2.1",
49
+ "@typescript-eslint/parser": "^6.2.1",
50
+ "better-sqlite3": "8.5.0",
51
+ "discord.js": "^14.12.1",
52
+ "eslint": "^8.46.0",
53
+ "guilded.ts": "^0.20.2",
54
+ "jsdoc-to-markdown": "^8.0.0",
55
+ "mongodb": "5.7.0",
56
+ "typescript": "^5.1.6",
57
+ "uglify-js": "^3.17.4"
58
+ },
59
+ "engines": {
60
+ "node": ">=15.5.0"
61
+ },
62
+ "bugs": {
63
+ "url": "https://github.com/Rahuletto/simply-xp/issues"
64
+ },
65
+ "homepage": "https://simplyxp.js.org",
66
+ "directories": {
67
+ "doc": "Mini_Docs",
68
+ "lib": "lib"
69
+ }
70
+ }
Binary file
@@ -1,10 +0,0 @@
1
- /**
2
- * Convert XP to level and vice versa.
3
- *
4
- * @param {"xp" | "level"} type - Type to convert from. Use either "xp" or "level".
5
- * @param {number} value - Value to convert.
6
- * @link https://simplyxp.js.org/docs/convert Documentation
7
- * @returns {number} - The converted value. (XP to level or level to XP)
8
- * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
9
- */
10
- export declare function convert(type: "xp" | "level", value: number): number;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- /**
3
- * Convert XP to level and vice versa.
4
- *
5
- * @param {"xp" | "level"} type - Type to convert from. Use either "xp" or "level".
6
- * @param {number} value - Value to convert.
7
- * @link https://simplyxp.js.org/docs/convert Documentation
8
- * @returns {number} - The converted value. (XP to level or level to XP)
9
- * @throws {XpFatal} If an invalid type is provided or if the value is not provided.
10
- */function convert(type,value){if("xp"===type||"level"===type){if(!value)throw new xplogs_1.XpFatal({function:"convert()",message:"Value was not provided"});if("level"===type)return Math.pow(value/.1,2);if("xp"===type)return Math.floor(.1*Math.sqrt(value))}throw new xplogs_1.XpFatal({function:"convert()",message:"Invalid type provided"})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.convert=void 0;const xplogs_1=require("./xplogs");exports.convert=convert;