djs-builder 0.6.31 → 0.6.33

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
@@ -541,6 +541,164 @@ module.exports = {
541
541
 
542
542
  ---
543
543
 
544
+ <details>
545
+
546
+ <summary> Level System 🏆 </summary>
547
+
548
+ ---
549
+
550
+ ## 🏆 Level System – XP, Levels & Leaderboard
551
+
552
+ The **Level System** module lets you track user experience points (XP) in **text** 💬 and **voice** 🎙️, handle **level-ups** ⬆️, and display **leaderboards** 🏅.
553
+ Perfect for gamifying your Discord server! 🎮✨
554
+
555
+ ---
556
+
557
+ ### 📦 Module Exports
558
+
559
+ ```js
560
+ const { addXP, UserLevel, leaderboard } = require("djs-builder");
561
+ ```
562
+
563
+ - `addXP(userId, guildId, options)` → Adds XP for a user and handles level-ups 🎲.
564
+ - `UserLevel(userId, guildId)` → Fetch a user's XP and level 👤.
565
+ - `leaderboard(guildId, type, limit)` → Get top users 🏅.
566
+
567
+ ---
568
+
569
+ ### 🎲 addXP – Add Experience Points
570
+
571
+ Adds XP to a user and automatically handles **level-ups**.
572
+
573
+ ```js
574
+ const result = await addXP("USER_ID", "GUILD_ID", {
575
+ type: "text", // "text" 💬 | "voice" 🎙️
576
+ minXP: 5, // Minimum random XP 🟢
577
+ maxXP: 15, // Maximum random XP 🔵
578
+ amount_add: 10, // Optional: fixed XP 💎
579
+ level_add: 1, // Optional: direct level boost ⬆️
580
+ });
581
+
582
+ console.log(result);
583
+ /* Example output:
584
+ {
585
+ newLevel: 3,
586
+ oldLevel: 2,
587
+ totalXP: 250,
588
+ leveledUp: true
589
+ }
590
+ */
591
+ ```
592
+
593
+ ---
594
+
595
+ ### 🧑‍🤝‍🧑 UserLevel – Fetch User Data
596
+
597
+ Fetch a user's **text XP, voice XP, total XP, and current level**.
598
+
599
+ ```js
600
+ const data = await UserLevel("USER_ID", "GUILD_ID");
601
+ console.log(data);
602
+ /* Example output:
603
+ {
604
+ text: 120 💬,
605
+ voice: 50 🎙️,
606
+ totalXP: 170 ⭐,
607
+ level: 2 ⬆️
608
+ }
609
+ */
610
+ ```
611
+
612
+ > Returns default values if the user is not found.
613
+
614
+ ---
615
+
616
+ ### 🏅 Leaderboard – Top Users
617
+
618
+ Get a sorted list of users based on XP or level.
619
+
620
+ ```js
621
+ const topUsers = await leaderboard("GUILD_ID", "totalXP", 5);
622
+ console.log(topUsers);
623
+ /* Example output:
624
+ [
625
+ { userId: "123", totalXP: 500, level: 5 },
626
+ { userId: "456", totalXP: 400, level: 4 },
627
+ ...
628
+ ]
629
+ */
630
+ ```
631
+
632
+ **Parameters:**
633
+
634
+ - `guildId` → Server ID 🏠
635
+ - `type` → `"totalXP"`, `"text"` 💬, `"voice"` 🎙️, or `"level"` ⬆️. Default = `"totalXP"`
636
+ - `limit` → Number of top users to return 🔢. Default = 10
637
+
638
+ ---
639
+
640
+ ### ⚡ Practical Example: messageCreate Event
641
+
642
+ ```js
643
+ const { addXP, UserLevel, leaderboard } = require("djs-builder");
644
+
645
+ module.exports = {
646
+ name: "messageCreate",
647
+ run: async (msg, client) => {
648
+ if (msg.author.bot) return;
649
+
650
+ // 🎲 Add XP on every message
651
+ const result = await addXP(msg.author.id, msg.guild.id, {
652
+ type: "text",
653
+ minXP: 5,
654
+ maxXP: 15,
655
+ });
656
+
657
+ // 🎉 Level-up notification
658
+ if (result.leveledUp) {
659
+ msg.channel.send(
660
+ `🎊 ${msg.author} new level **${result.newLevel}** ⬆️`
661
+ );
662
+ }
663
+
664
+ // 📊 Check your rank
665
+ if (msg.content === "!rank") {
666
+ const data = await UserLevel(msg.author.id, msg.guild.id);
667
+ msg.reply(`📈 **level ** ${data.level} ⬆️ – **XP:** ${data.totalXP} ⭐`);
668
+ }
669
+
670
+ // 🏅 Display top users
671
+ if (msg.content === "!top") {
672
+ const lb = await leaderboard(msg.guild.id, "totalXP", 5);
673
+ msg.reply(
674
+ lb
675
+ .map(
676
+ (u, i) =>
677
+ `#${i + 1} <@${u.userId}> – Lv.${u.level} ⬆️ (${u.totalXP} ⭐)`
678
+ )
679
+ .join("\n")
680
+ );
681
+ }
682
+ },
683
+ };
684
+ ```
685
+
686
+ ---
687
+
688
+ ### 💡 Notes & Tips
689
+
690
+ - 💬 **Text XP** – Add XP for messages automatically.
691
+ - 🎙️ **Voice XP** – Add XP for voice activity.
692
+ - ⬆️ **Level Up** – Trigger notifications when leveling up.
693
+ - 🏅 **Leaderboard** – Display the top users in server using embeds for better look.
694
+ - 🎮 **Gamify** your server easily with XP rewards, mini-games, and custom commands.
695
+
696
+ ---
697
+
698
+ </details>
699
+
700
+ ---
701
+
544
702
  ## ⚡ Commands & Events
545
703
 
546
704
  ---
@@ -0,0 +1,76 @@
1
+ const { Schema, model } = require("mongoose");
2
+
3
+ const levelSchema = new Schema({
4
+ userId: String,
5
+ guildId: String,
6
+ text: { type: Number, default: 0 },
7
+ voice: { type: Number, default: 0 },
8
+ totalXP: { type: Number, default: 0 },
9
+ level: { type: Number, default: 0 },
10
+ });
11
+
12
+ const Level = model("Level", levelSchema);
13
+
14
+ function randomXP(min, max) {
15
+ return Math.floor(Math.random() * (max - min + 1)) + min;
16
+ }
17
+
18
+ async function addXP(userId, guildId, options = {}) {
19
+ let data = await Level.findOne({ userId, guildId });
20
+ if (!data) {
21
+ data = new Level({ userId, guildId });
22
+ }
23
+
24
+ const oldLevel = data.level || 0;
25
+ let leveledUp = false;
26
+
27
+ ////////////////////////////////! 🎲 XP
28
+ const xpToAdd =
29
+ options.amount_add ?? randomXP(options.minXP || 5, options.maxXP || 12);
30
+
31
+ data[options.type || "text"] += xpToAdd;
32
+ data.totalXP += xpToAdd;
33
+
34
+ ////////////////////////////////! 🎯 Level boost
35
+ if (options.level_add) {
36
+ data.level += options.level_add;
37
+ leveledUp = true;
38
+ }
39
+
40
+ ////////////////////////////////! check level ✅
41
+ const needed = (data.level + 1) * 100;
42
+ if (data.totalXP >= needed) {
43
+ data.level++;
44
+ leveledUp = true;
45
+ }
46
+
47
+ await data.save();
48
+
49
+ return {
50
+ newLevel: data.level,
51
+ oldLevel,
52
+ totalXP: data.totalXP,
53
+ leveledUp,
54
+ };
55
+ }
56
+
57
+ ////////////////////////////////! user data 🧑‍🤝‍🧑
58
+ async function UserLevel(userId, guildId) {
59
+ return (
60
+ (await Level.findOne({ userId, guildId })) || {
61
+ text: 0,
62
+ voice: 0,
63
+ totalXP: 0,
64
+ level: 0,
65
+ }
66
+ );
67
+ }
68
+
69
+ ////////////////////////////////! 🏆 اtop
70
+ async function leaderboard(guildId, type = "totalXP", limit = 10) {
71
+ return await Level.find({ guildId })
72
+ .sort({ [type]: -1 })
73
+ .limit(limit);
74
+ }
75
+
76
+ module.exports = { addXP, UserLevel, leaderboard };
package/handler/helper.js CHANGED
@@ -89,7 +89,7 @@ async function terminalInfo(client, options, data) {
89
89
  [rowColors[4]("Prefix Commands"), rowColors[4](data.prefix)],
90
90
  [rowColors[5]("Slash Commands"), rowColors[5](data.slash)],
91
91
  [rowColors[6]("Total Commands"), rowColors[6](data.slash + data.prefix)],
92
- [rowColors[7]("Events"), rowColors[7](data.event)],
92
+ [rowColors[7]("Events"), rowColors[7](data.events)],
93
93
  [rowColors[8]("Started At"), rowColors[8](new Date().toLocaleString())]
94
94
  );
95
95
 
@@ -9,7 +9,7 @@ const {
9
9
  let termenal = {
10
10
  prefix: 0,
11
11
  slash: 0,
12
- event: 0,
12
+ events: 0,
13
13
  };
14
14
 
15
15
  async function starter(client, options) {
@@ -144,7 +144,7 @@ async function starter(client, options) {
144
144
  });
145
145
  }
146
146
 
147
- termenal.event++;
147
+ termenal.events++;
148
148
  }
149
149
  }
150
150
 
@@ -351,6 +351,7 @@ client.on("messageCreate", async (message) => {
351
351
  }
352
352
 
353
353
  const { Wait, CreateBar, CreateRow, GetUser } = require("../function/function");
354
+ const { addXP, UserLevel, leaderboard } = require("../function/level");
354
355
  const { log } = require("../function/log");
355
356
 
356
- module.exports = { starter, log, Wait, CreateBar, CreateRow, GetUser };
357
+ module.exports = { starter, log, Wait, CreateBar, CreateRow, GetUser, addXP, UserLevel, leaderboard };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "djs-builder",
3
- "version": "0.6.31",
4
- "note": "🎉 Package Update! 🥏\n- Add option `disabled` to the selectMenu options \nNew features added:\n- `GetUser`: Easily fetch a user from **ID**, **mention**, or even from a **reply**.\n\n🛠 Fixes:\n- Minor bugs fixed\n- Improved stability and error handling\n- Custom prefix : add custom prefix for etch server\n\n🔗 Learn more on [NPM](https://www.npmjs.com/package/djs-builder)",
3
+ "version": "0.6.33",
4
+ "note": "🎉 Package Update! 🥏\n- Add Level System 🏆 \n\n- 🛠 Fixes:\n- Minor bugs fixed\n- Improved stability and error handling\n- Custom prefix : add custom prefix for etch server\n\n🔗 Learn more on [NPM](https://www.npmjs.com/package/djs-builder)",
5
5
  "description": "🎉 Package Update! 🥏",
6
6
  "main": "handler/starter.js",
7
7
  "dependencies": {