-
Notifications
You must be signed in to change notification settings - Fork 1
/
translateBot.js
145 lines (123 loc) · 4.04 KB
/
translateBot.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
var mp = require("minecraft-protocol");
var translations = require("./translations.json");
var bot = mp.createClient({
host: "kbpw.de",
port: 25565,
username: "yeetBot",
version: "1.15.2"
});
//test
bot.on("login", () => {
console.log("logged in");
});
bot.on("chat", (msgData, packetInfo) => {
// console.log(JSON.parse(msgData.message));
// console.log("");
// try {
// console.log(JSON.parse(msgData.message).with[1].extra[0].extra);
// } catch (e) {}
// console.log("");
console.log(processMsg(JSON.parse(msgData.message)));
});
function processMsg(data) {
if (
typeof data.text == "undefined" &&
typeof data.translate !== "undefined" &&
!(typeof data == 'string')
) {
var msg = "";
var translate = translations[data.translate] || data.translate;
var withdata = [];
if (typeof data.with !== "undefined")
data.with.forEach(data => {
// console.log(data)
withdata.push(processMsg(data));
});
if (typeof data.text == "undefined") data.text = "";
if (typeof data.color == "undefined") data.color = "reset";
if (typeof data.bold == "undefined") data.bold = false;
if (typeof data.obfuscated == "undefined") data.obfuscated = false;
if (typeof data.underlined == "undefined") data.underlined = false;
if (typeof data.italic == "undefined") data.italic = false;
if (typeof data.strikethrough == "undefined") data.strikethrough = false;
var color = getColor(data.color);
var bold = data.bold == true ? "§l" : "";
var obfuscated = data.obfuscated == true ? "§k" : "";
var underlined = data.underlined == true ? "§n" : "";
var italic = data.italic == true ? "§o" : "";
var strikethrough = data.strikethrough == true ? "§m" : "";
for(var i = 0;i<(translate.match(/%s/g) || []).length+1;i++){
var replace = typeof withdata[i] !== 'undefined'?withdata[i]:"";
translate = translate.replace('%s',replace+color +
bold +
obfuscated +
underlined +
italic +
strikethrough)
}
var msg =
color +
bold +
obfuscated +
underlined +
italic +
strikethrough + translate
if (typeof data.extra !== "undefined")
data.extra.forEach(data => {
msg = msg + processMsg(data);
});
// console.log(msg)
return msg;
} else if (typeof data == 'string') {
return data;
} else {
if (typeof data.text == "undefined") data.text = "";
if (typeof data.color == "undefined") data.color = "reset";
if (typeof data.bold == "undefined") data.bold = false;
if (typeof data.obfuscated == "undefined") data.obfuscated = false;
if (typeof data.underlined == "undefined") data.underlined = false;
if (typeof data.italic == "undefined") data.italic = false;
if (typeof data.strikethrough == "undefined") data.strikethrough = false;
var color = getColor(data.color);
var bold = data.bold == true ? "§l" : "";
var obfuscated = data.obfuscated == true ? "§k" : "";
var underlined = data.underlined == true ? "§n" : "";
var italic = data.italic == true ? "§o" : "";
var strikethrough = data.strikethrough == true ? "§m" : "";
var msg =
color +
bold +
obfuscated +
underlined +
italic +
strikethrough +
data.text;
if (typeof data.extra !== "undefined")
data.extra.forEach(data => {
msg = msg + processMsg(data);
});
return msg;
}
}
const colors = {
"reset": "§r",
"white": "§f",
"black": "§0",
"red": "§c",
"dark_red": "§4",
"green": "§a",
"dark_green": "§2",
"light_purple": "§d",
"dark_purple": "§5",
"blue": "§9",
"dark_blue": "§1",
"aqua": "§b",
"dark_aqua": "§3",
"gold": "§6",
"yellow": "§e",
"gray": "§7",
"dark_gray": "§8"
}
function getColor(cl) {
return colors[cl] == undefined ? "" : colors[cl];
}