nv-kit 1.0.3 → 1.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-kit",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Beginner-friendly Discord.js command handler with slash/prefix commands, cooldowns, permissions, and optional MongoDB.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,59 @@
1
+ const fs = require("fs")
2
+ const path = require("path")
3
+
4
+ const getAllFiles = (dir) => {
5
+ const entries = fs.readdirSync(dir, { withFileTypes: true })
6
+ let files = []
7
+
8
+ for (const ent of entries) {
9
+ const full = path.join(dir, ent.name)
10
+ if (ent.isDirectory()) files = files.concat(getAllFiles(full))
11
+ else if (ent.isFile() && full.endsWith(".js")) files.push(full)
12
+ }
13
+
14
+ return files
15
+ }
16
+
17
+ class ButtonHandler {
18
+ constructor(instance, buttonsDir, client) {
19
+ this._instance = instance
20
+ this._client = client
21
+ this._buttons = new Map()
22
+
23
+ const abs = path.isAbsolute(buttonsDir)
24
+ ? buttonsDir
25
+ : path.join(process.cwd(), buttonsDir)
26
+
27
+ if (!fs.existsSync(abs)) {
28
+ console.warn(`[nv-kit] buttonsDir not found: ${abs}`)
29
+ return
30
+ }
31
+
32
+ const files = getAllFiles(abs)
33
+
34
+ for (const file of files) {
35
+ delete require.cache[require.resolve(file)]
36
+ const btn = require(file)
37
+
38
+ if (!btn || typeof btn !== "object") continue
39
+ if (!btn.id || typeof btn.id !== "string") {
40
+ console.warn(`[nv-kit] Button missing "id" in: ${file}`)
41
+ continue
42
+ }
43
+ if (typeof btn.callback !== "function") {
44
+ console.warn(`[nv-kit] Button missing "callback" in: ${file}`)
45
+ continue
46
+ }
47
+
48
+ this._buttons.set(btn.id, btn)
49
+ }
50
+
51
+ console.log(`[nv-kit] Loaded ${this._buttons.size} button handlers.`)
52
+ }
53
+
54
+ getButton(id) {
55
+ return this._buttons.get(id)
56
+ }
57
+ }
58
+
59
+ module.exports = ButtonHandler
@@ -1,3 +1,16 @@
1
- module.exports = (interaction) => {
2
- interaction.reply('testing')
3
- }
1
+ module.exports = async (instance, interaction, client) => {
2
+ const btn = instance.buttonHandler?.getButton(interaction.customId)
3
+ if (!btn) return
4
+
5
+ try {
6
+ await btn.callback({ interaction, client, instance })
7
+ } catch (e) {
8
+ console.error(`[nv-kit] Button handler error (${interaction.customId})`, e)
9
+
10
+ if (!interaction.replied && !interaction.deferred) {
11
+ await interaction
12
+ .reply({ content: "Button error.", ephemeral: true })
13
+ .catch(() => {})
14
+ }
15
+ }
16
+ }
package/src/index.js CHANGED
@@ -4,6 +4,7 @@ const CommandHandler = require('./command-handler/CommandHandler')
4
4
  const Cooldowns = require('./util/Cooldowns')
5
5
  const EventHandler = require('./event-handler/EventHandler')
6
6
  const FeatureHandler = require('./util/FeatureHandler')
7
+ const ButtonHandler = require("./button-handler/ButtonHandler")
7
8
 
8
9
  class Main {
9
10
  constructor(obj) {
@@ -14,6 +15,7 @@ class Main {
14
15
  client,
15
16
  mongoUri,
16
17
  commandsDir,
18
+ buttonsDir,
17
19
  featuresDir,
18
20
  testServers = [],
19
21
  botOwners = [],
@@ -45,6 +47,10 @@ class Main {
45
47
  this._commandHandler = new CommandHandler(this, commandsDir, client)
46
48
  }
47
49
 
50
+ if (buttonsDir) {
51
+ this._buttonHandler = new ButtonHandler(this, buttonsDir, client)
52
+ }
53
+
48
54
  if (featuresDir) {
49
55
  new FeatureHandler(this, featuresDir, client)
50
56
  }
@@ -72,6 +78,10 @@ class Main {
72
78
  return this._commandHandler
73
79
  }
74
80
 
81
+ get buttonHandler() {
82
+ return this._buttonHandler
83
+ }
84
+
75
85
  get eventHandler() {
76
86
  return this._eventHandler
77
87
  }