Add index.js
This commit is contained in:
commit
1264c41e6c
1 changed files with 102 additions and 0 deletions
102
index.js
Normal file
102
index.js
Normal file
|
@ -0,0 +1,102 @@
|
|||
// declarations
|
||||
const sleep = require ('system-sleep');
|
||||
const core = require ("./core");
|
||||
const config = require ("./conf");
|
||||
const mc = require ('minecraft-protocol');
|
||||
|
||||
// random name function
|
||||
function random (length) {
|
||||
let result = '';
|
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const charactersLength = characters.length;
|
||||
let counter = 0;
|
||||
while (counter < length) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
counter += 1;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
// bot creation
|
||||
const client = mc.createClient
|
||||
(
|
||||
{
|
||||
host: "prljav.cc",
|
||||
port: 25565,
|
||||
username: random (10),
|
||||
version: "1.19"
|
||||
}
|
||||
);
|
||||
|
||||
// add core
|
||||
client.on ("login", () => {
|
||||
core.injectTo(client)
|
||||
setInterval(() => {
|
||||
client.cmdCore.refillCmdCore()
|
||||
}, 60000);
|
||||
})
|
||||
|
||||
// create functions
|
||||
function tellraw (players, jsonMessage) {
|
||||
client.cmdCore.run(`minecraft:tellraw ${players} ${jsonMessage}`);
|
||||
};
|
||||
|
||||
function commandError (players, errorTitle, otherthing) {
|
||||
tellraw (players, JSON.stringify
|
||||
(
|
||||
[
|
||||
{
|
||||
"text": errorTitle + "\n",
|
||||
"color": "red"
|
||||
},
|
||||
{
|
||||
"text": otherthing,
|
||||
"color": "gray"
|
||||
},
|
||||
{
|
||||
"text": "<--[HERE]",
|
||||
"color": "red",
|
||||
"italic": true
|
||||
}
|
||||
]
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
// command listener
|
||||
const chatListener = function ({senderName, plainMessage, unsignedContent, formattedMessage, verified}) {
|
||||
if (! `${plainMessage}`.startsWith (config.prefix)) return;
|
||||
|
||||
const args = `${plainMessage}`.slice (config.prefix.length).trim ().split (/ +/);
|
||||
const cmd = args.shift ().toLowerCase ();
|
||||
|
||||
const cmds = {
|
||||
help: async () => {
|
||||
tellraw (senderName, JSON.stringify
|
||||
(
|
||||
[
|
||||
{
|
||||
"text": "List of commands (" + Object.keys (cmds).length + "):\n" + Object.keys (cmds).join ("\n")
|
||||
}
|
||||
]
|
||||
)
|
||||
)
|
||||
},
|
||||
echo: async (args) => {
|
||||
if (! args.join(" ")) {
|
||||
commandError ("Expected text", cmd);
|
||||
} else {
|
||||
client.chat (args.join(" "));
|
||||
};
|
||||
},
|
||||
refill: async () => {
|
||||
client.cmdCore.refillCmdCore()
|
||||
}
|
||||
};if (cmds[cmd]) cmds[cmd](args); else commandError (senderName, "Unknown command", cmd) // command executor thingy
|
||||
}
|
||||
|
||||
client.on ("playerChat", chatListener);
|
||||
|
||||
client.on ("end", () => {
|
||||
client.off ("playerChat", chatListener);
|
||||
}); // idfk what this does
|
Loading…
Reference in a new issue