-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.js
211 lines (187 loc) · 6.77 KB
/
commands.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const readline = require("readline");
const prefix = ".";
const baritonePrefix = "#";
const colors = require("colors");
const { writer } = require("repl");
module.exports = function(bot) {
require("./baritone")(bot);
bot.commands = {};
bot.commandPrefix = prefix;
let cLog = console.log;
let cError = console.error;
let interface = readline.createInterface(process.stdin, process.stdout);
global.console.log = function (data, type) {
if (!type) type = "Main";
let date = new Date();
var time = date.getHours().toString();
if (date.getMinutes().toString().length == 1) time += `:0${date.getMinutes()}`;
else time += `:${date.getMinutes()}`;
interface.pause();
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`[${time}] [${type}] ${data}` + "\n" + interface.line);
process.stdout.cursorTo(interface.cursor);
interface.resume();
};
global.console.error = function (data, type) {
if (!type) type = "Main";
let date = new Date();
var time = date.getHours().toString();
if (date.getMinutes().toString().length == 1) time += `:0${date.getMinutes()}`;
else time += `:${date.getMinutes()}`;
interface.pause();
process.stderr.clearLine();
process.stderr.cursorTo(0);
process.stderr.write(`[${time}] [${type}] ${data}` + "\n" + interface.line);
process.stderr.cursorTo(interface.cursor);
interface.resume();
}
bot.registerCommand = function (command, description, usage, cb) {
bot.commands[command] = {
description: description,
usage: usage,
callback: cb
};
};
function inp () {
interface.question("", data => {
process.stdout.moveCursor(0, -1);
process.stdout.clearLine();
if (data.startsWith(baritonePrefix)) {
// coming soon
}
else if (data.startsWith(prefix)) {
let args = data.slice(prefix.length).trim().split(" ");
let command = args.shift().toLowerCase();
if (bot.commands[command]) {
try {
bot.commands[command].callback(args);
}
catch (e) {
console.error(colors.red("An internal error has occurred whilst executing this command"));
console.error(e);
}
return inp();
}
else console.log(`Unknown command. Type "${prefix}help" for a list of available commands.`);
}
else bot.chat(data);
inp();
});
};
bot.on("end", () => {
global.console.log = cLog;
global.console.error = cError;
interface.close();
});
bot.on("login", inp);
bot.registerCommand("help", "Shows the help menu.", "help", () => {
for (let i in bot.commands) {
console.log(`${prefix}${bot.commands[i].usage} - ${bot.commands[i].description}`);
}
});
bot.registerCommand("say", "Sends a chat message", "say <message...>", args => {
if (args.length == 0) return console.log(colors.red(`Usage: ${prefix}say <message...>`));
bot.chat(args.join(" "));
});
bot.registerCommand("coords", "Gets the coordinates of an entity.", "coords", () => {
let coords = {
x: Math.floor(bot.entity.position.x),
y: Math.floor(bot.entity.position.y),
z: Math.floor(bot.entity.position.z)
}
console.log(`Current coords: x:${coords.x} y:${coords.y} z:${coords.z}`);
});
bot.registerCommand("forward", "Makes an entity go forward.", "forward <ms>", args => {
if (Number.isNaN(Number(args[0]))) return console.error("Expected number");
console.log("Started moving forward");
bot.setControlState("forward", true);
setTimeout(() => {
console.log("Finished moving forward");
bot.setControlState("forward", false);
}, parseInt(args[0]))
});
bot.registerCommand("back", "Makes an entity go back.", "back <ms>", args => {
if (Number.isNaN(Number(args[0]))) return console.error("Expected number");
console.log("Started moving back");
bot.setControlState("back", true);
setTimeout(() => {
console.log("Finished moving back");
bot.setControlState("back", false);
}, parseInt(args[0]))
});
bot.registerCommand("left", "Makes an entity go left.", "left <ms>", args => {
if (Number.isNaN(Number(args[0]))) return console.error("Expected number");
console.log("Started moving left");
bot.setControlState("left", true);
setTimeout(() => {
console.log("Finished moving left");
bot.setControlState("left", false);
}, parseInt(args[0]))
});
bot.registerCommand("right", "Makes an entity go right.", "right <ms>", args => {
if (Number.isNaN(Number(args[0]))) return console.error("Expected number");
console.log("Started moving right");
bot.setControlState("right", true);
setTimeout(() => {
console.log("Finished moving right");
bot.setControlState("right", false);
}, parseInt(args[0]))
});
bot.registerCommand("jump", "Makes an entity jump.", "jump <ms>", args => {
if (Number.isNaN(Number(args[0]))) return console.error("Expected number");
console.log("Started jumping");
bot.setControlState("jump", true);
setTimeout(() => {
console.log("Finished jumping");
bot.setControlState("jump", false);
}, parseInt(args[0]))
});
bot.registerCommand("togglesprint", "Toggles sprinting.", "togglesprint", () => {
if (bot.getControlState("sprint")) {
bot.setControlState("sprint", false);
console.log("Sprinting disabled.");
}
else {
bot.setControlState("sprint", true);
console.log("Sprinting enabled.");
}
});
bot.registerCommand("eval", "Evaluates a JavaScript code.", "eval <code...>", args => {
try {
let x = eval(args.join(" "));
console.log(writer(x), "Evaluation thread");
}
catch (e) {
console.error(e, "Evaluation thread");
}
});
bot.registerCommand("list", "Gets the player list.", "list", () => {
console.log(`There are ${Object.entries(bot.players).length} players online`);
var players = [];
for (let i in bot.players) players.push(bot.players[i].username);
console.log(players.join(", "));
});
bot.registerCommand("ping", "Gets you or other players' ping.", "ping [player]", args => {
if (!args[0]) console.log(`Your ping is ${bot.player.ping} ms`);
else {
for (let i in bot.players) {
if (args[0].toLowerCase() == bot.players[i].username.toLowerCase()) {
return console.log(`${bot.players[i].username}'s ping is ${bot.players[i].ping} ms`);
}
}
console.log("Player not found.");
}
});
bot.registerCommand("bestping", "Gets the player's best ping in this server.", "bestping", () => {
let bestPing = Object.entries(bot.players).sort((a, b) => a[1].ping - b[1].ping)[0][1];
console.log(`${bestPing.username} has the best ping in this server, ${bestPing.ping} ms`);
});
bot.registerCommand("worstping", "Gets the player's worst ping in this server.", "worstping", () => {
let worstPing = Object.entries(bot.players).sort((a, b) => b[1].ping - a[1].ping)[0][1];
console.log(`${worstPing.username} has the worst ping in this server, ${worstPing.ping} ms`);
});
bot.registerCommand("disconnect", "Disconnects to the server.", "disconnect", () => {
bot.end();
});
}