nv-kit 1.0.3 → 1.0.5
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 +1 -1
- package/src/button-handler/ButtonHandler.js +59 -0
- package/src/event-handler/EventHandler.js +1 -1
- package/src/event-handler/events/interactionCreate/index.js +20 -0
- package/src/event-handler/events/interactionCreate/isButton/index.js +16 -0
- package/src/event-handler/events/interactionCreate/isCommand/slash-commands.js +4 -4
- package/src/event-handler/events/messageCreate/isHuman/legacy-commands.js +1 -1
- package/src/index.js +11 -1
- package/src/event-handler/events/interactionCreate/isButton/test.js +0 -3
package/package.json
CHANGED
|
@@ -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
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = async (instance, interaction) => {
|
|
2
|
+
try {
|
|
3
|
+
if (interaction.isButton()) {
|
|
4
|
+
const handleButton = require("./isButton/test")
|
|
5
|
+
return await handleButton(instance, interaction)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (interaction.isChatInputCommand() || interaction.isAutocomplete()) {
|
|
9
|
+
const handleSlash = require("./isCommand/slash-commands")
|
|
10
|
+
return await handleSlash(instance, interaction)
|
|
11
|
+
}
|
|
12
|
+
} catch (err) {
|
|
13
|
+
console.error("[nv-kit] interactionCreate error:", err)
|
|
14
|
+
if (!interaction?.replied && !interaction?.deferred) {
|
|
15
|
+
await interaction
|
|
16
|
+
?.reply?.({ content: "Something went wrong.", ephemeral: true })
|
|
17
|
+
.catch(() => {})
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -28,12 +28,12 @@ const handleAutocomplete = async (interaction, commands) => {
|
|
|
28
28
|
)
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
module.exports = async (
|
|
31
|
+
module.exports = async (instance, interaction) => {
|
|
32
32
|
const { commandHandler } = instance
|
|
33
33
|
const { commands, customCommands } = commandHandler
|
|
34
34
|
|
|
35
35
|
if (interaction.type === InteractionType.ApplicationCommandAutocomplete) {
|
|
36
|
-
|
|
36
|
+
await handleAutocomplete(interaction, commands)
|
|
37
37
|
return
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -70,8 +70,8 @@ module.exports = async (interaction, instance) => {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
if (deferReply) {
|
|
73
|
-
|
|
73
|
+
await interaction.editReply(response).catch(() => {})
|
|
74
74
|
} else {
|
|
75
|
-
|
|
75
|
+
await interaction.reply(response).catch(() => {})
|
|
76
76
|
}
|
|
77
77
|
}
|
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
|
}
|
|
@@ -82,7 +92,7 @@ class Main {
|
|
|
82
92
|
|
|
83
93
|
async connectToMongo(mongoUri) {
|
|
84
94
|
await mongoose.connect(mongoUri, {
|
|
85
|
-
|
|
95
|
+
|
|
86
96
|
})
|
|
87
97
|
}
|
|
88
98
|
}
|