-
Notifications
You must be signed in to change notification settings - Fork 14
/
helperbot.js
138 lines (113 loc) · 3.72 KB
/
helperbot.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
var mf = require('mineflayer'),
fs = require('fs'),
path = require('path'),
optimist = require('optimist'),
prompt = require('prompt'),
requireIndex = require('requireindex');
// Parse command line options:
var default_opts = {
login: false,
masters: [],
};
var aliases = {
'l': 'login',
'p': 'port',
'h': 'help',
};
var argv = optimist
.alias(aliases)
.default(default_opts)
.boolean('l')
.argv;
if (argv.help) {
console.log("Usage: node helperbot.js [SERVER] [USERNAME] [OPTIONS]\n\
Connect a mineflayer bot to SERVER as USERNAME.\n\
By default connects 'helperbot' to localhost.\n\
\n\
-h, --help display this help and exit\n\
-l, --login prompt for credentials and login to minecraft.net\n\
--masters=a,b,c,... set the bot's masters to player a, b, c, ...\n\
--password=PASSWORD connect to minecraft.net with the given PASSWORD\n\
-p, --port=PORT connect to the given port (defaults to 25565)");
process.exit(code=0);
}
// Split masters by commas in order to get all masters
argv.masters = argv.masters.length ? argv.masters.split(',').map(function(master) { return master.toLowerCase(); }) : [];
// First unnamed argument is the host, second is the username
if (argv._.length === 0) {
} else if (argv._.length === 1) {
argv.host = argv._[0];
} else if (argv._.length === 2) {
argv.host = argv._[0];
argv.username = argv._[1];
} else {
argv.host = argv._[0];
argv.username = argv._[1];
console.log("Ignoring unknown options: " + argv.slice(2).join(', '));
}
if (argv.login) {
// If login flag is set, prompt user for any missing credentials
required_fields = [];
if (!argv.username) {
required_fields.push('username');
}
if (!argv.password) {
required_fields.push('password');
}
if (required_fields) {
prompt.override = argv;
// prompt bug doesn't allow for falsy value options, work around this by changing the defaults
prompt.message = '';
prompt.delimiter = '';
prompt.colors = false;
prompt.start({
allowEmpty: true,
});
var schema = { properties: {} };
for (i = 0; i < required_fields.length; i++) {
schema.properties[required_fields[i]] = {
description: required_fields[i].charAt(0).toUpperCase() + required_fields[i].slice(1) + ':',
pattern: /.*/,
required: true,
};
}
if (schema.properties.password) {
schema.properties.password.hidden = true;
}
prompt.get(schema, function get_login(err, result) {
if (err) throw err;
argv.username = result.username || argv.username;
argv.password = result.password || argv.password;
// Connect
init(argv);
});
}
} else {
// Set default credential options
// Default username, no password makes it default to offline mode.
if (argv.username == null) {
argv.username = 'helperbot';
}
// Connect
init(argv);
}
function init(argv) {
var bot = mf.createBot(argv);
bot.masters = argv.masters;
bot.quietMode = true;
require('mineflayer-navigate')(mf)(bot);
require('mineflayer-scaffold')(mf)(bot);
require('mineflayer-blockfinder')(mf)(bot);
bot.on('error', function(error) {
console.error(error.stack);
});
var plugins = requireIndex(path.join(__dirname, 'lib', 'plugins'));
for (plugin in plugins) {
if (plugins[plugin].inject != null) {
plugins[plugin].inject(bot);
} else {
console.log(plugin, 'has no inject function.');
}
}
}