simply-xp 2.0.0-dev.3-fix.1 → 2.0.0-dev.4
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 +1 -1
- package/lib/src/Fonts/BalooBhaijaan-Regular.otf +0 -0
- package/lib/src/add.js +1 -1
- package/lib/src/cards.d.ts +14 -0
- package/lib/src/cards.js +2 -2
- package/lib/src/charts.d.ts +18 -8
- package/lib/src/charts.js +3 -3
- package/lib/src/deprecated/rank.js +2 -1
- package/lib/src/fetch.js +1 -1
- package/lib/src/functions/database.d.ts +1 -1
- package/lib/src/functions/database.js +1 -1
- package/lib/src/leaderboard.d.ts +1 -1
- package/lib/src/migrate.js +2 -2
- package/package.json +9 -10
- package/lib/src/Fonts/Baloo-Regular.eot +0 -0
package/README.md
CHANGED
|
@@ -57,7 +57,7 @@ yarn add simply-xp@dev
|
|
|
57
57
|
- Deleted `chart.js` dependency
|
|
58
58
|
- `fetch()` now also returns `position`, and accepts `username` parameter
|
|
59
59
|
- `roleSetup` functions now accept roleID arrays! `["role1", "role2", "role3"]`, and will return `timestamp` as a bonus!
|
|
60
|
-
- `reset()` function now accepts `
|
|
60
|
+
- `reset()` function now accepts `erase` and `username` as optional arguments
|
|
61
61
|
- `addLevel(), addXP(), setLevel(), setXP()` now has a `username` parameter, to automatically create the user if it doesn't exist.
|
|
62
62
|
|
|
63
63
|
# ⚠️ V2 Breaking Changes ⚠️
|
|
Binary file
|
package/lib/src/add.js
CHANGED
|
@@ -20,4 +20,4 @@
|
|
|
20
20
|
* @link `Documentation:` https://simplyxp.js.org/docs/addxp
|
|
21
21
|
* @returns {Promise<XPResult>} - Object of user data on success.
|
|
22
22
|
* @throws {XpFatal} - If parameters are not provided correctly.
|
|
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
|
|
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:!e||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;
|
package/lib/src/cards.d.ts
CHANGED
|
@@ -88,4 +88,18 @@ export declare function leaderboardCard(data: Array<User>, options?: Leaderboard
|
|
|
88
88
|
description: string;
|
|
89
89
|
name: string;
|
|
90
90
|
}>;
|
|
91
|
+
/**
|
|
92
|
+
* @constructor
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
95
|
+
export declare function RoundedBox(ctx: {
|
|
96
|
+
beginPath: () => void;
|
|
97
|
+
moveTo: (arg0: number, arg1: number) => void;
|
|
98
|
+
lineTo: (arg0: number, arg1: number) => void;
|
|
99
|
+
quadraticCurveTo: (arg0: number, arg1: number, arg2: number, arg3: number) => void;
|
|
100
|
+
closePath: () => void;
|
|
101
|
+
}, x: number, y: number, width: number, height: number, radius: number, roundCorners?: {
|
|
102
|
+
top?: boolean;
|
|
103
|
+
bottom?: boolean;
|
|
104
|
+
}): void;
|
|
91
105
|
export {};
|
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
|
|
12
|
+
*/async function rankCard(guild,user,options={},locales={}){var e,a,o,t,r,l,n,d;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 i,s;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 coming in dev.5"),!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","BalooBhaijaan-Regular.otf"),"Sans Serif"),cachedRankImage=cachedRankImage||await(0,canvas_1.loadImage)(options?.background||"https://i.ibb.co/dck2Tnt/rank-card.webp").catch(()=>{throw new xplogs_1.XpFatal({function:"rankCard()",message:"Unable to load background image, is it valid?"})}),n=await(0,canvas_1.loadImage)(user.avatarURL).catch(()=>{throw new xplogs_1.XpFatal({function:"rankCard()",message:"Unable to load user's AvatarURL, is it reachable?"})});let c=await database_1.db.findOne({collection:"simply-xps",data:{guild:guild.id,user:user.id}});if(!c){if(!xp_1.xp.auto_create)throw new xplogs_1.XpFatal({function:"rankCard()",message:"User not found in database"});c=await(0,xp_1.create)(user.id,guild.id,user.username)}return d=await database_1.db.find({collection:"simply-xps",data:{guild:guild.id}}),c.position=d.sort((a,b)=>b.xp-a.xp).findIndex(u=>u.user===user.id)+1,options?.legacy?(d=user.username.replace(/[\u007f-\uffff]/g,""),e=options?.color||"#9900ff",a=options?.lvlbar||"#ffffff",options=options?.lvlbarBg||"#ffffff",o=shortener(c.xp)+(" "+locales.xp),t=locales.level+(" "+shortener(c.level)),r=(0,utilities_1.convertFrom)(c.level+1),l=(0,utilities_1.convertFrom)(c.level),l=100*(c.xp-l)/(r-l)*660/100,cachedRankContext&&cachedRankCanvas?(i=cachedRankCanvas,s=cachedRankContext):(i=(0,canvas_1.createCanvas)(1080,400),RoundedBox(s=i.getContext("2d"),0,0,i.width,i.height,50),s.clip(),s.fillStyle="#000000",s.fillRect(0,0,1080,400),s.globalAlpha=.7,s.drawImage(cachedRankImage,-5,0,1090,400),s.restore(),s.fillStyle="#000000",s.globalAlpha=.4,s.fillRect(40,0,240,i.height),s.globalAlpha=1),s.save(),RoundedBox(s,70,30,180,180,50),s.strokeStyle=e,s.lineWidth=15,s.stroke(),s.clip(),s.drawImage(n,70,30,180,180),s.restore(),s.save(),RoundedBox(s,70,320,180,50,20),s.strokeStyle="#BFC85A22",s.stroke(),s.clip(),s.fillStyle=e,s.globalAlpha=1,s.fillRect(70,320,180,50),s.globalAlpha=1,s.fillStyle="#ffffff",s.textAlign="center",dynamicFont(s,o,160,358,160,32),s.restore(),s.save(),RoundedBox(s,70,240,180,50,20),s.strokeStyle="#BFC85A22",s.stroke(),s.clip(),s.fillStyle=e,s.globalAlpha=1,s.fillRect(70,240,180,50),s.globalAlpha=1,s.fillStyle="#ffffff",s.textAlign="center",dynamicFont(s,t,160,278,160,32),s.restore(),s.save(),s.textAlign="left",s.fillStyle="#ffffff",s.shadowColor="#000000",s.shadowBlur=15,s.shadowOffsetX=1,s.shadowOffsetY=1,s.font='39px "Sans Serif"',s.fillText(d,390,80),s.restore(),s.save(),s.textAlign="right",s.fillStyle="#ffffff",s.shadowColor="#000000",s.shadowBlur=15,s.shadowOffsetX=1,s.shadowOffsetY=1,s.font='55px "Sans Serif"',s.fillText("#"+c.position,i.width-55,80),s.restore(),s.save(),RoundedBox(s,390,305,660,70,Number(20)),s.strokeStyle="#BFC85A22",s.stroke(),s.clip(),s.fillStyle="#ffffff",s.textAlign="center",dynamicFont(s,guild.name,720,355,700,45),s.globalAlpha=.2,s.fillRect(390,305,660,70),s.restore(),s.save(),RoundedBox(s,390,145,660,50,20),s.strokeStyle="#BFC85A22",s.stroke(),s.clip(),s.fillStyle=options,s.globalAlpha=.2,s.fillRect(390,145,660,50),s.restore(),s.save(),RoundedBox(s,390,145,l,50,20),s.strokeStyle="#BFC85A22",s.stroke(),s.clip(),s.fillStyle=a,s.globalAlpha=.5,s.fillRect(390,145,l,50),s.restore(),s.save(),s.textAlign="left",s.fillStyle="#ffffff",s.globalAlpha=.8,s.font='30px "Sans Serif"',s.fillText(locales.next_level+": "+shortener(r)+" "+locales.xp,390,230),s.restore(),n="{current} / {needed}".replace(/{needed}/g,shortener(r)).replace(/{current}/g,shortener(c.xp)),s.textAlign="center",s.fillStyle="#474747",s.globalAlpha=1,s.font='30px "Sans Serif"',s.fillText(n,730,180)):i=(0,canvas_1.createCanvas)(1080,360),{attachment:i.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)),canvas_1.GlobalFonts.registerFromPath(options?.font||(0,path_1.join)(__dirname,"Fonts","
|
|
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).catch(()=>{throw new xplogs_1.XpFatal({function:"leaderboardCard()",message:"Unable to load artwork image, is it valid?"})})),!cachedLeaderboardImage&&options?.backgroundImage&&(cachedLeaderboardImage=await(0,canvas_1.loadImage)(options.backgroundImage).catch(()=>{throw new xplogs_1.XpFatal({function:"leaderboardCard()",message:"Unable to load background image, is it valid?"})})),canvas_1.GlobalFonts.registerFromPath(options?.font||(0,path_1.join)(__dirname,"Fonts","BalooBhaijaan-Regular.otf"),"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.RoundedBox=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,exports.RoundedBox=RoundedBox;
|
package/lib/src/charts.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* Chart options
|
|
4
|
+
* @property {string} font - Font of the chart
|
|
5
|
+
* @property {"blue" | "dark" | "discord" | "green" | "orange" | "red" | "space" | "yellow"} theme - Theme of the chart
|
|
6
|
+
* @property {number} limit - Limit of users to return (2-10)
|
|
7
|
+
*/
|
|
2
8
|
export interface ChartOptions {
|
|
3
|
-
|
|
4
|
-
limit?:
|
|
5
|
-
|
|
9
|
+
font?: string;
|
|
10
|
+
limit?: 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
|
|
11
|
+
theme?: "blue" | "dark" | "discord" | "green" | "orange" | "red" | "space" | "yellow";
|
|
12
|
+
type?: "bar" | "doughnut" | "pie";
|
|
6
13
|
}
|
|
7
14
|
/**
|
|
8
15
|
* Creates a chart
|
|
@@ -10,8 +17,11 @@ export interface ChartOptions {
|
|
|
10
17
|
* @param {string} guildId
|
|
11
18
|
* @param {ChartOptions?} options
|
|
12
19
|
* @link `Documentation:` https://simplyxp.js.org/docs/charts
|
|
13
|
-
* @returns {Promise<
|
|
14
|
-
* @throws {XpFatal} If invalid parameters are provided
|
|
20
|
+
* @returns {Promise<{attachment: Buffer, description: string, name: string}>} Chart attachment
|
|
21
|
+
* @throws {XpFatal} If invalid parameters are provided, or if there are not enough users to create a chart
|
|
15
22
|
*/
|
|
16
|
-
export declare function charts(guildId: string, options?: ChartOptions): Promise<
|
|
17
|
-
|
|
23
|
+
export declare function charts(guildId: string, options?: ChartOptions): Promise<{
|
|
24
|
+
attachment: Buffer;
|
|
25
|
+
description: string;
|
|
26
|
+
name: string;
|
|
27
|
+
}>;
|
package/lib/src/charts.js
CHANGED
|
@@ -5,6 +5,6 @@
|
|
|
5
5
|
* @param {string} guildId
|
|
6
6
|
* @param {ChartOptions?} options
|
|
7
7
|
* @link `Documentation:` https://simplyxp.js.org/docs/charts
|
|
8
|
-
* @returns {Promise<
|
|
9
|
-
* @throws {XpFatal} If invalid parameters are provided
|
|
10
|
-
*/async function charts(guildId,options={}){if(!guildId)throw new xplogs_1.XpFatal({function:"charts()",message:"No Guild ID Provided"});if(options)throw
|
|
8
|
+
* @returns {Promise<{attachment: Buffer, description: string, name: string}>} Chart attachment
|
|
9
|
+
* @throws {XpFatal} If invalid parameters are provided, or if there are not enough users to create a chart
|
|
10
|
+
*/async function charts(guildId,options={}){var t,r,a,F,o,l,i,s,d,n;if(!guildId)throw new xplogs_1.XpFatal({function:"charts()",message:"No Guild ID Provided"});if(!options)throw new xplogs_1.XpFatal({function:"charts()",message:"No Options Provided"});options.theme||(options.theme="blue"),options.type||(options.type="bar");let f={background:"#FFFFFF",barColor:"#FFFFFF",pieColors:["#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF"],textColor:"#FFFFFF"};if((t=await(0,leaderboard_1.leaderboard)(guildId,Math.min(Math.max(options?.limit||10,2),10)).catch(XPError=>{throw new xplogs_1.XpFatal({function:"charts()",message:XPError.message})})).length<2)throw new xplogs_1.XpFatal({function:"charts()",message:"Not enough users to create a chart"});switch(t.sort((a,b)=>b.position-a.position),canvas_1.GlobalFonts.registerFromPath(options?.font||(0,path_1.join)(__dirname,"Fonts","BalooBhaijaan-Regular.otf"),"Sans Serif"),options.theme){case"blue":f={background:"#1e1e3c",barColor:"#747fff",pieColors:["#747fff","#55b9f3","#4dc7ec","#3ad5e5","#32e3dd","#2cf2d4","#26ffd2","#30edb4","#3cda96","#48c878"],textColor:"#FFFFFF"};break;case"dark":f={background:"#1e1e1e",barColor:"#747474",pieColors:["#747474","#8f8f8f","#a8a8a8","#c1c1c1","#dadada","#f4f4f4","#ffffff","#ffffff","#ffffff","#ffffff"],textColor:"#FFFFFF"};break;case"discord":f={background:"#36393f",barColor:"#5865F2",pieColors:["#5865F2","#57F287","#FEE75C","#ED4245","#F47FFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF"],textColor:"#FFFFFF"};break;case"green":f={background:"#1e321e",barColor:"#74ff7f",pieColors:["#74ff7f","#55f3a0","#4decb2","#3dd5c3","#32cdd5","#2cc6e6","#26bfee","#30a8e6","#3c91dd","#487ad4"],textColor:"#FFFFFF"};break;case"orange":f={background:"#321e1e",barColor:"#ff9f74",pieColors:["#ff9f74","#f3b055","#ecbe4d","#d5c63d","#cdd532","#c6e62c","#bfe626","#a8e630","#91dd3c","#7ad448"],textColor:"#FFFFFF"};break;case"red":f={background:"#321e1e",barColor:"#ff7474",pieColors:["#ff7474","#f35555","#ec4d4d","#d53d3d","#cd3232","#c62c2c","#bf2626","#a83030","#913c3c","#7a4848"],textColor:"#FFFFFF"};break;case"space":f={background:"#001F3F",barColor:"#192E5B",pieColors:["#192E5B","#1F3F7F","#264FA3","#2C5FC7","#337FEA","#3D8FFF","#4D9FFF","#5DAFFF","#6DBFFF","#7DCFFF"],textColor:"#FFFFFF"};break;case"yellow":f={background:"#32321e",barColor:"#ffff74",pieColors:["#ffff74","#f3f355","#ecf24d","#d5eb3d","#cde532","#c6e02c","#bfe626","#a8df30","#91d93c","#7ad448"],textColor:"#FFFFFF"}}const c=(0,canvas_1.createCanvas)(920,600),h=c.getContext("2d"),u=Math.max(...t.map(user=>user.level));if((0,cards_1.RoundedBox)(h,0,0,c.width,c.height,25),h.clip(),h.fillStyle=f.background,h.fillRect(0,0,c.width,c.height),"space"===options.theme){h.clearRect(0,0,c.width,c.height),(guildId=h.createRadialGradient(c.width/2,c.height/2,1,c.width/2,c.height/2,Math.max(c.width,c.height))).addColorStop(0,"#000000"),guildId.addColorStop(1,"#001F3F"),h.fillStyle=guildId,h.fillRect(0,0,c.width,c.height),(guildId=h.createRadialGradient(150,150,10,150,150,100)).addColorStop(0,"#F2F2F2"),guildId.addColorStop(.8,"#D3D3D3"),guildId.addColorStop(1,"#001F3F"),h.fillStyle=guildId,h.beginPath(),h.arc(150,150,100,0,2*Math.PI),h.fill(),r=["#6B6B6B","#AA8F00","#473E83","#456579"];for(let e=0;e<r.length;e++)a=Math.random()*c.width,F=Math.random()*c.height,o=50*Math.random()+30,h.beginPath(),h.arc(a,F,o,0,2*Math.PI),h.fillStyle=r[e]||"#FFFFFF",h.fill();h.filter="blur(5px)",h.drawImage(c,0,0),h.filter="none";for(let e=0;e<100;e++)l=Math.random()*c.width,i=Math.random()*c.height,s=2*Math.random(),h.beginPath(),h.arc(l,i,s,0,2*Math.PI),h.fillStyle="#FFFFFF",h.fill()}switch(options.type){case"bar":{n=h.measureText(u.toString()).width,d=c.width-n-60-40;const g=c.height-100-40,p=d/t.length-20,x=20+n+40,b=c.height-50-20;await Promise.all(t.map(async(user,index)=>{var e,t,r=user.level/u*g,index=x+index*(20+p),a=b-r,r=(h.fillStyle=f.barColor,h.strokeStyle=f.barColor,h.lineWidth=2,(0,cards_1.RoundedBox)(h,index,a,p,r,10),h.fill(),h.stroke(),index+p/2),index=(h.fillStyle=f.textColor,h.font="22px Sans Serif",user.level.toString()),F=h.measureText(index).width,a=a-10,index=(h.fillText(index,r-F/2,a),user?.name||user.user),F=h.measureText(index).width,a=(h.font=Math.min(Math.floor(p/F*16),18)+"px Sans Serif",F=h.measureText(index).width,30+b);"space"===options.theme&&(e=r-(user=F+10)/2,t=a-18,h.fillStyle="rgba(0, 0, 0, 0.5)",h.fillRect(e,t,user,22)),h.fillStyle=f.textColor,h.fillText(index,r-F/2,a)}))}break;case"doughnut":{d=c.width-40,n=c.height-40;const C=t.reduce((sum,user)=>sum+user.level,0),m=c.width/2,w=c.height/2,S=Math.min(d,n)/3,y=.6*S;let e=-Math.PI/2;await Promise.all(t.map(async(user,index)=>{user=user.level/C,user=e+2*Math.PI*user,h.fillStyle=f.pieColors[index%f.pieColors.length]||"#FFFFFF",h.beginPath(),h.moveTo(m+S*Math.cos(e),w+S*Math.sin(e)),h.arc(m,w,S,e,user),h.lineTo(m+y*Math.cos(user),w+y*Math.sin(user)),h.arc(m,w,y,user,e,!0),h.closePath(),h.fill(),e=user}));const M=c.height-20-20*t.length;h.fillStyle="rgba(0,0,0,0.25)",h.fillRect(15,M-5,200,20*t.length+5),h.font="12px Sans Serif",t.forEach((user,index)=>{var user=user?.name||user.user,e=f.pieColors[index%f.pieColors.length],index=M+20*index;h.fillStyle=e||"#FFFFFF",h.fillRect(20,index,15,15),h.fillStyle=f.textColor,h.fillText(user,40,11.5+index)})}break;case"pie":{d=c.width-40,n=c.height-40;const v=t.reduce((sum,user)=>sum+user.level,0),k=c.width/2,P=c.height/2,I=Math.min(d,n)/3;let e=-Math.PI/2;await Promise.all(t.map(async(user,index)=>{user=user.level/v,user=e+2*Math.PI*user,h.fillStyle=f.pieColors[index%f.pieColors.length]||"#FFFFFF",h.beginPath(),h.moveTo(k,P),h.arc(k,P,I,e,user),h.closePath(),h.fill(),e=user}));const _=c.height-20-20*t.length;h.fillStyle="rgba(0,0,0,0.25)",h.fillRect(15,_-5,200,20*t.length+5),h.font="12px Sans Serif",t.forEach((user,index)=>{var user=user?.name||user.user,e=f.pieColors[index%f.pieColors.length],index=_+20*index;h.fillStyle=e||"#FFFFFF",h.fillRect(20,index,15,15),h.fillStyle=f.textColor,h.fillText(user,40,11.5+index)})}break;default:throw new xplogs_1.XpFatal({function:"charts()",message:"Invalid chart type provided"})}return{attachment:c.toBuffer("image/png"),description:"Chart",name:"chart.png"}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.charts=void 0;const canvas_1=require("@napi-rs/canvas"),path_1=require("path"),leaderboard_1=require("./leaderboard"),cards_1=require("./cards"),xplogs_1=require("./functions/xplogs");exports.charts=charts;
|
|
@@ -9,4 +9,5 @@
|
|
|
9
9
|
* @link `Documentation:` https://simplyxp.js.org/docs/deprecated/rank
|
|
10
10
|
* @returns {Promise<{attachment: Buffer, description: string, name: string}>}
|
|
11
11
|
* @throws {XpFatal} - If parameters are not provided correctly
|
|
12
|
-
*/
|
|
12
|
+
*/
|
|
13
|
+
async function rank(message,userId,_guildId,options){var e;try{e=await Promise.resolve().then(()=>__importStar(require("discord.js/package.json"))),parseInt(e.version.split(".")[0])<13?xplogs_1.XpLog.warn("rank()","This may not work with Discord.JS v12 or below."):xplogs_1.XpLog.debug("rank()",`Discord.JS v${e.version} detected.`)}catch(e){throw new xplogs_1.XpFatal({function:"rank()",message:"This function requires Discord.JS, as it is only for Discord bots. | Use rankCard() instead."})}if(!message||!message?.guild)throw new xplogs_1.XpFatal({function:"rank()",message:"Invalid Message Provided"});if(!userId)throw new xplogs_1.XpFatal({function:"rank()",message:"No User ID Provided"});if(xplogs_1.XpLog.warn("rank()","DEPRECATED FUNCTION!! Please use rankCard() instead."),e=await message.guild.members.fetch(userId).catch(()=>null))return(0,cards_1.rankCard)({id:message.guild.id,name:message.guild.name},{id:e.id,username:e.user.username,avatarURL:e.user.displayAvatarURL()},options);throw new xplogs_1.XpFatal({function:"rank()",message:"Member 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,r;if(mod&&mod.__esModule)return mod;if(e={},null!=mod)for(r in mod)"default"!==r&&Object.prototype.hasOwnProperty.call(mod,r)&&__createBinding(e,mod,r);return __setModuleDefault(e,mod),e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.rank=void 0;const xplogs_1=require("../functions/xplogs"),cards_1=require("../cards");exports.rank=rank;
|
package/lib/src/fetch.js
CHANGED
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
* @link `Documentation:` https://simplyxp.js.org/docs/fetch
|
|
9
9
|
* @returns {Promise<{name: string | null, user: string, guild: string, level: number, position: number, xp: number}>}
|
|
10
10
|
* @throws {XpFatal} If invalid parameters are provided, or if the user data is not found.
|
|
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"});
|
|
11
|
+
*/async function fetch(userId,guildId,username){var e;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"});let t=(e=await(await Promise.resolve().then(()=>__importStar(require("./functions/database")))).db.find({collection:"simply-xps",data:{guild:guildId}})).find(u=>u.user===userId);if(!t){if(!xp_1.xp.auto_create||!username)throw new xplogs_1.XpFatal({function:"fetch()",message:"User data not found"});t=await(await Promise.resolve().then(()=>__importStar(require("./create")))).create(guildId,userId,username)}return guildId=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:guildId,xp:t.xp}}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;
|
|
@@ -22,7 +22,7 @@ static getCollection(collection){if(!xp_1.xp.database)throw new xplogs_1.XpFatal
|
|
|
22
22
|
* @link https://simplyxp.js.org/docs/handlers/database#createOne Documentation
|
|
23
23
|
* @returns {Promise<UserResult | LevelRoleResult>} The created document.
|
|
24
24
|
* @throws {XpFatal} Throws an error if there is no database connection.
|
|
25
|
-
*/static async createOne(query){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"createOne()",message:"No database connection"});let e;switch(xp_1.xp.dbType){case"mongodb":
|
|
25
|
+
*/static async createOne(query){if(!xp_1.xp.database)throw new xplogs_1.XpFatal({function:"createOne()",message:"No database connection"});let e;switch(xp_1.xp.dbType){case"mongodb":xp_1.xp.database.db().collection(query.collection).insertOne(query.data).catch(error=>handleError(error,"createOne()")),e=db.findOne(query);break;case"sqlite":e="simply-xps"===query.collection?xp_1.xp.database.prepare('INSERT INTO "simply-xps" (user, guild, name, xp, level) VALUES (?, ?, ?, ?, ?)').run(query.data.user,query.data.guild,query.data?.name,query.data.xp,query.data.level):xp_1.xp.database.prepare('INSERT INTO "simply-xp-levelroles" (guild, level, role) VALUES (?, ?, ?)').run(query.data.guild,query.data.level,query.data.roles)}return e}
|
|
26
26
|
/**
|
|
27
27
|
* Deletes one document from the database.
|
|
28
28
|
*
|
package/lib/src/leaderboard.d.ts
CHANGED
package/lib/src/migrate.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @returns {Promise<boolean>} - Returns true if migration is successful
|
|
8
8
|
* @throws {XpLog.err} - If migration fails.
|
|
9
9
|
*/
|
|
10
|
-
static async discord_xp(deleteOld=!1){var e=await database_1.db.getCollection("levels").find().toArray();xplogs_1.XpLog.debug("migrate.discord_xp()",`FOUND ${e.length} DOCUMENTS`);try{for(const
|
|
10
|
+
static async discord_xp(deleteOld=!1){var e=await database_1.db.getCollection("levels").find().toArray();xplogs_1.XpLog.debug("migrate.discord_xp()",`FOUND ${e.length} DOCUMENTS`);try{for(const r of e)await database_1.db.findOne({collection:"simply-xps",data:{guild:r.guildID,user:r.userID}})||(await database_1.db.createOne({collection:"simply-xps",data:{guild:r.guildID,user:r.userID,xp:r.xp,level:(0,xp_1.convertFrom)(r.xp,"xp")}}),deleteOld&&await database_1.db.getCollection("levels").deleteOne({userID:r.userID,guildID:r.guildID}));return!0}catch(e){return xplogs_1.XpLog.err("migrate.discord_xp()",e),!1}}
|
|
11
11
|
/**
|
|
12
12
|
* Effortlessly migrate from MongoDB to SQLite. (or vice versa)
|
|
13
13
|
* @async
|
|
@@ -16,4 +16,4 @@ static async discord_xp(deleteOld=!1){var e=await database_1.db.getCollection("l
|
|
|
16
16
|
* @link `Documentation:` https://simplyxp.js.org/docs/migrate/database
|
|
17
17
|
* @returns {Promise<boolean>} - Returns true if migration is successful
|
|
18
18
|
* @throws {XpFatal} - If parameters are not provided correctly
|
|
19
|
-
*/static async fromDB(dbType,connection){if(!dbType)throw new xplogs_1.XpFatal({function:"migrate.database()",message:"No database type provided"});if(!connection)throw new xplogs_1.XpFatal({function:"migrate.database()",message:"No database connection provided"});if(xp_1.xp.dbType===dbType)return xplogs_1.XpLog.info("migrate.fromDB()","Same database received, that was unnecessary!");switch(dbType){case"mongodb":try{if(!await(0,connect_1.checkPackageVersion)("mongodb"))return xplogs_1.XpLog.err("migrate.fromDB()","MongoDB V4 or higher is required");connection.db().collection("simply-xps").find().toArray().
|
|
19
|
+
*/static async fromDB(dbType,connection){if(!dbType)throw new xplogs_1.XpFatal({function:"migrate.database()",message:"No database type provided"});if(!connection)throw new xplogs_1.XpFatal({function:"migrate.database()",message:"No database connection provided"});if(xp_1.xp.dbType===dbType)return xplogs_1.XpLog.info("migrate.fromDB()","Same database received, that was unnecessary!");let e;switch(dbType){case"mongodb":try{if(!await(0,connect_1.checkPackageVersion)("mongodb"))return xplogs_1.XpLog.err("migrate.fromDB()","MongoDB V4 or higher is required");e=connection.db().collection("simply-xps").find().toArray(),xplogs_1.XpLog.debug("migrate.fromDB()",`FOUND ${e.length} DOCUMENTS`)}catch(e){return xplogs_1.XpLog.err("migrate.fromDB()",e),!1}break;case"sqlite":try{if(!await(0,connect_1.checkPackageVersion)("sqlite"))return xplogs_1.XpLog.err("migrate.fromDB()","better-sqlite3 V7 or higher is required");e=connection.prepare("SELECT * FROM `simply-xps`").all(),xplogs_1.XpLog.debug("migrate.fromDB()",`FOUND ${e.length} ROWS`)}catch(e){return xplogs_1.XpLog.err("migrate.fromDB()",e),!1}}return await Promise.all(e.map(async user=>await database_1.db.findOne({collection:"simply-xps",data:{guild:user.guild,user:user.user}})?database_1.db.updateOne({collection:"simply-xps",data:{guild:user.guild,user:user.user}},{collection:"simply-xps",data:{guild:user.guild,user:user.user,name:user.name,xp:user.xp,level:user.level}}):database_1.db.createOne({collection:"simply-xps",data:{guild:user.guild,user:user.user,xp:user.xp,level:user.level}}))),!0}}exports.migrate=migrate;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simply-xp",
|
|
3
|
-
"version": "2.0.0-dev.
|
|
3
|
+
"version": "2.0.0-dev.4",
|
|
4
4
|
"description": "The easiest way to implement xp system",
|
|
5
5
|
"main": "lib/xp.js",
|
|
6
6
|
"scripts": {
|
|
@@ -40,19 +40,18 @@
|
|
|
40
40
|
"url": "git+https://github.com/Rahuletto/simply-xp.git"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@napi-rs/canvas": "^0.1.
|
|
43
|
+
"@napi-rs/canvas": "^0.1.44"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/better-sqlite3": "^7.6.4",
|
|
47
|
-
"@types/node": "^20.4
|
|
48
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
49
|
-
"@typescript-eslint/parser": "^6.
|
|
50
|
-
"better-sqlite3": "8.5.
|
|
51
|
-
"discord.js": "^14.
|
|
52
|
-
"eslint": "^8.
|
|
53
|
-
"guilded.ts": "^0.20.2",
|
|
47
|
+
"@types/node": "^20.5.4",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^6.4.1",
|
|
49
|
+
"@typescript-eslint/parser": "^6.4.1",
|
|
50
|
+
"better-sqlite3": "8.5.1",
|
|
51
|
+
"discord.js": "^14.13.0",
|
|
52
|
+
"eslint": "^8.47.0",
|
|
54
53
|
"jsdoc-to-markdown": "^8.0.0",
|
|
55
|
-
"mongodb": "^5.
|
|
54
|
+
"mongodb": "^5.8.1",
|
|
56
55
|
"typescript": "^5.1.6",
|
|
57
56
|
"uglify-js": "^3.17.4"
|
|
58
57
|
},
|
|
Binary file
|