clean up code
This commit is contained in:
parent
43b6886d04
commit
a3471b3df6
6 changed files with 73 additions and 65 deletions
|
@ -1,11 +1,12 @@
|
|||
const fs = require("fs");
|
||||
const {data, clients} = require("./data");
|
||||
|
||||
|
||||
class CommandHandler {
|
||||
constructor (dir = "./packets") {
|
||||
constructor (dir = "./packets", data={}) {
|
||||
this.packets = {};
|
||||
this.data = data;
|
||||
this.clients = clients;
|
||||
Object.entries(data).forEach(([key, value]) => {
|
||||
this[key] = value;
|
||||
});
|
||||
this.loadPackets(dir);
|
||||
}
|
||||
|
||||
|
|
35
botpackets/broadcast.js
Normal file
35
botpackets/broadcast.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
module.exports = {
|
||||
id: 0,
|
||||
execute: function (data, client, handler) {
|
||||
let toSend = "";
|
||||
const {clients, webhookClient, kittycorpBots} = handler;
|
||||
for (let [key, c] of Object.entries(kittycorpBots)) {
|
||||
if (key != client.server) {
|
||||
c.send(JSON.stringify({ op: 0, ...data, server: client.server, serverName: client.serverName }));
|
||||
}
|
||||
}
|
||||
if (Array.isArray(data.m)) {
|
||||
data.m.forEach(obj => {
|
||||
toSend += obj.text;
|
||||
});
|
||||
} else { toSend = data.m; }
|
||||
if (data.type == 0) {
|
||||
webhookClient.send({
|
||||
content: toSend.replace("@", "@ "), allowed_mentions: { parse: [] },
|
||||
username: data.sender + ` (${client.serverName})`,
|
||||
avatarURL: `https://cataas.com/cat/2LC9Ne6SIMXnIdXZ`
|
||||
});
|
||||
clients.forEach((u, c) => {
|
||||
c.send(JSON.stringify({ op: 1, sn: client.serverName, sender: data.sender, m: toSend, from: "mc" }));
|
||||
});
|
||||
} else {
|
||||
webhookClient.send({
|
||||
content: toSend.replace("@", "@ "), allowed_mentions: { parse: [] },
|
||||
username: "KittyCorp™️" + ` (${client.serverName})`,
|
||||
avatarURL: `https://cataas.com/cat/iD7RU5SKBkeQijJn`
|
||||
});
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
7
botpackets/deleteplayer.js
Normal file
7
botpackets/deleteplayer.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
module.exports = {
|
||||
id: 2,
|
||||
execute: function (data, client, handler) {
|
||||
let {players} = handler;
|
||||
delete players[data.username];
|
||||
}
|
||||
}
|
7
botpackets/getplayer.js
Normal file
7
botpackets/getplayer.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
module.exports = {
|
||||
id: 3,
|
||||
execute: function (data, client, handler) {
|
||||
let {players} = handler;
|
||||
client.send(JSON.stringify({ op: "player", player: players[data.player] ?? null, requestId: data.requestId }));
|
||||
}
|
||||
}
|
12
botpackets/players.js
Normal file
12
botpackets/players.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
module.exports = {
|
||||
id: 1,
|
||||
execute: function (data, client, handler) {
|
||||
let {players} = handler;
|
||||
data.players.forEach(player => {
|
||||
players[player.username] = {
|
||||
...player,
|
||||
server: client.server
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
68
index.js
68
index.js
|
@ -24,7 +24,8 @@ const dclient = new Client({
|
|||
|
||||
const webhookClient = new WebhookClient({ id: config.webhookId, token: config.webhookToken });
|
||||
const app = express();
|
||||
const packetHandler = new PacketHandler();
|
||||
const packetHandler = new PacketHandler("./packets", {data, clients});
|
||||
|
||||
|
||||
process.on("uncaughtException", console.log);
|
||||
|
||||
|
@ -49,7 +50,7 @@ let server = new WebSocket.WebSocketServer({ noServer: true, clientTracking: fal
|
|||
|
||||
let kittycorpBots = {};
|
||||
let players = {};
|
||||
|
||||
const botPacketHandler = new PacketHandler("./botpackets", {clients, kittycorpBots, webhookClient, players});
|
||||
|
||||
dclient.on("messageCreate", msg => {
|
||||
if (msg.author.bot) { return };
|
||||
|
@ -66,69 +67,16 @@ dclient.on("messageCreate", msg => {
|
|||
|
||||
|
||||
/**@param client {WebSocket.WebSocket} */
|
||||
function initListeners(client) {
|
||||
function initListeners(client, req) {
|
||||
client.on("message", data => {
|
||||
let toSend = "";
|
||||
try {
|
||||
data = JSON.parse(data);
|
||||
switch (data.op) {
|
||||
case 0: {
|
||||
// broadcast!
|
||||
for (let [key, c] of Object.entries(kittycorpBots)) {
|
||||
if (key != client.server) {
|
||||
c.send(JSON.stringify({ op: 0, ...data, server: client.server, serverName: client.serverName }));
|
||||
}
|
||||
}
|
||||
if (Array.isArray(data.m)) {
|
||||
data.m.forEach(obj => {
|
||||
toSend += obj.text;
|
||||
});
|
||||
} else { toSend = data.m; }
|
||||
if (data.type == 0) {
|
||||
webhookClient.send({
|
||||
content: toSend.replace("@", "@ "), allowed_mentions: { parse: [] },
|
||||
username: data.sender + ` (${client.serverName})`,
|
||||
avatarURL: `https://cataas.com/cat/2LC9Ne6SIMXnIdXZ`
|
||||
});
|
||||
clients.forEach((u, c) => {
|
||||
c.send(JSON.stringify({ op: 1, sn: client.serverName, sender: data.sender, m: toSend, from: "mc" }));
|
||||
});
|
||||
} else {
|
||||
|
||||
webhookClient.send({
|
||||
content: toSend.replace("@", "@ "), allowed_mentions: { parse: [] },
|
||||
username: "KittyCorp™️" + ` (${client.serverName})`,
|
||||
avatarURL: `https://cataas.com/cat/iD7RU5SKBkeQijJn`
|
||||
});
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
case 1: { // players
|
||||
data.op.players.forEach(player => {
|
||||
players[player.username] = {
|
||||
...player,
|
||||
server: client.server
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 2: {
|
||||
delete players[data.username];
|
||||
}
|
||||
|
||||
case 3: {
|
||||
client.send(JSON.stringify({ op: "player", player: players[data.player] ?? null, requestId: data.requestId }));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (typeof data.op != "number") return;
|
||||
botPacketHandler.execute(data.op, data, client);
|
||||
} catch (e) {
|
||||
console.log("kittycorp bot sent invalid packet", e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
server.on("kConnect", (client, req) => {
|
||||
|
@ -146,7 +94,6 @@ function handle(client, req) {
|
|||
packet = JSON.parse(packet);
|
||||
if (typeof packet.op != "number") return;
|
||||
let p = packetHandler.findPacket(packet.op);
|
||||
console.log(p);
|
||||
if (p) {
|
||||
if (p.auth && !clients.get(client)) return console.log("not authed.");
|
||||
packetHandler.execute(packet.op, packet, client, req);
|
||||
|
@ -160,8 +107,7 @@ function handle(client, req) {
|
|||
server.on("pConnect", handle);
|
||||
|
||||
httpServer.on("upgrade", (req, socket, head) => {
|
||||
|
||||
|
||||
|
||||
if (req.url == "/kittycorpBots") {
|
||||
if (config.token && !req.headers.authorization || req.headers.authorization != config.token) {
|
||||
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
||||
|
|
Loading…
Reference in a new issue