-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
53 lines (45 loc) · 1.21 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const mineflayer = require('mineflayer')
const readline = require('readline')
const fs = require('fs')
const path = require('path')
const bot = mineflayer.createBot({
host: 'localhost',
port: 25565,
username: 'Bot',
version: '1.18.2'
})
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const commands = {}
function readFiles(dir) {
const files = fs.readdirSync(dir)
for (const file of files) {
const filePath = path.join(dir, file)
const stats = fs.statSync(filePath)
if (stats.isFile()) {
const command = require(filePath)
commands[command.command] = command
} else if (stats.isDirectory()) {
readFiles(filePath)
}
}
}
readFiles(path.join(__dirname, 'commands'))
bot.on('chat', (username, message) => {
console.log(`<${username}> ${message}`)
})
rl.on('line', (line) => {
if (line.startsWith('.')) {
const [command, ...args] = line.slice(1).split(' ')
if (commands[command]) {
commands[command].execute(bot, command, args)
} else {
console.log(`Unknown command: ${command}`)
}
} else {
bot.chat(line)
}
})
module.exports.commands = commands