Advertisement
Guest User

b0t.js

a guest
Jan 25th, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1. const WebSocket = require("ws");
  2. const translate = require("translate");
  3. const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
  4. const fs = require("fs");
  5. const util = require("util");
  6. let isdi = require("./istro-dictionary.json");
  7.  
  8. let account = {
  9. email: env.email,
  10. token: env.token
  11. };
  12.  
  13. let chat = {};
  14. let messageQueue = [];
  15.  
  16. function deepStringify(object, isArray) {
  17. let output = "";
  18. for (let i in object) {
  19. let t = object[i];
  20. if (typeof t === "object") {
  21. if (t.length !== undefined) {
  22. output += (isArray ? "" : `"${i}":`) + `[${deepStringify(t, true)}],`;
  23. } else {
  24. output += (isArray ? "" : `"${i}":`) + `{${deepStringify(t)}},`;
  25. }
  26. } else if (typeof t === "string") {
  27. output += (isArray ? "" : `"${i}":`) + `"${t}",`;
  28. } else {
  29. output += (isArray ? "" : `"${i}":`) + `${t},`;
  30. }
  31. }
  32. return output.slice(0, output.length - 1);
  33. }
  34.  
  35. function connect() {
  36. let ws = new WebSocket('ws://198.199.109.223:88');
  37.  
  38. function sendMessage(text, channel, max) {
  39. for (let i = 0; i <= Math.round(text.length / 300); i ++) {
  40. if (i >= (max ? max : 3))
  41. return;
  42. messageQueue.push({text: text.slice(i * 300, i * 300 + 300), channel: channel});
  43. }
  44. }
  45.  
  46. ws.on("error", e => console.error(e));
  47.  
  48. ws.on("open", () => {
  49. console.log("Connected");
  50. ws.send(JSON.stringify(["authSignIn", account]));
  51. ws.send(JSON.stringify(["registerBot"]));
  52. });
  53.  
  54. ws.on('message', msg => {
  55. let data = JSON.parse(msg);
  56. if (data[0] === "message") {
  57. let text = data[1].text;
  58. let name = data[1].name;
  59. let channel = data[1].channel;
  60.  
  61. if (!chat[channel])
  62. chat[channel] = [];
  63. chat[channel].push(data[1]);
  64. let lines = chat[channel];
  65.  
  66. let args = text.split(" ");
  67. let command = args[0].toLowerCase();
  68.  
  69. function msgPointer(ify) {
  70. let output = text.slice(ify.length + 1);
  71. if(text.toLowerCase().startsWith(ify + "^")) {
  72. let num = 0;
  73. if (parseInt(command.slice(ify.length + 1), 10) > 0) {
  74. num = parseInt(command.slice(ify.length + 1), 10) + 1;
  75. } else if (command[ify.length + 1] === "^") {
  76. while (command[ify.length + 1 + num] === "^") {
  77. num ++;
  78. }
  79. num += 2;
  80. } else {
  81. num = 2;
  82. }
  83. if (num <= lines.length) {
  84. name = lines[lines.length - num].name;
  85. output = lines[lines.length - num].text;
  86. }
  87. }
  88. return output;
  89. }
  90.  
  91. //commands
  92. if (command === ".ping")
  93. sendMessage("pong!", channel);
  94.  
  95. else if (command === ".help")
  96. sendMessage("List of commands: .ping, .help, .define, owoify, therxify, voidify, xiaoify, ecclify, snullify, smartify, zalgoify", channel);
  97.  
  98. else if (command === ".isdi") {
  99. let phrase = false;
  100. if (args[2])
  101. phrase = isdi[args[2].toLowerCase()];
  102. if (args[1] === "define") {
  103. if (!args[2])
  104. sendMessage("Usage: .isdi define [phrase] <rank>", channel);
  105. if (phrase) {
  106. let defList = Object.values(phrase).sort((a, b) => { return b.votes.length - a.votes.length; });
  107. if (parseInt(args[3]) > 0) {
  108. let num = parseInt(args[3]);
  109. if (num <= defList.length)
  110. sendMessage(num + "/" + defList.length + " definition by " + defList[num - 1].poster + " (" + defList[num - 1].votes.length + " votes): " + defList[num - 1].definition, channel);
  111. else
  112. sendMessage("Error: number exceeds definition list length!", channel);
  113. } else
  114. sendMessage("Top definition by " + defList[0].poster + " (" + defList[0].votes.length + " votes): " + defList[0].definition, channel);
  115. } else
  116. sendMessage("Can not find definition :(", channel);
  117. }
  118. else if (args[1] === "add") {
  119. if (!args[2] || !args[3])
  120. sendMessage("Usage: .isdi add [phrase] [definition]", channel);
  121. else {
  122. if (!phrase)
  123. isdi[args[2].toLowerCase()] = {};
  124. if (isdi[args[2].toLowerCase()][name.toLowerCase()])
  125. sendMessage("You already made a definition for this phrase, use '.isdi remove [phrase]' to remove your definition.", channel);
  126. else {
  127. let def = args.splice(3, args.length).join(" ");
  128. isdi[args[2]][name.toLowerCase()] = {definition: def, poster: name.toLowerCase(), votes: []};
  129. fs.writeFileSync("istro-dictionary.json", `{${deepStringify(isdi)}}`);
  130. sendMessage(`Created new definition for ${args[2]}: ${def}`, channel);
  131. }
  132. }
  133. }
  134. else if (args[1] === "vote") {
  135. if (!args[2] || !args[3])
  136. sendMessage("Usage: .isdi vote [phrase] [poster]", channel);
  137. else if (!phrase)
  138. sendMessage("Can not find phrase :(", channel);
  139. else if (!phrase[args[3].toLowerCase()])
  140. sendMessage("Can not find poster :(", channel);
  141. else {
  142. for (let i of phrase[args[3].toLowerCase()].votes) {
  143. if (i === name.toLowerCase())
  144. return sendMessage("You already voted for this phrase!", channel);
  145. }
  146. isdi[args[2].toLowerCase()][args[3].toLowerCase()].votes.push(name.toLowerCase());
  147. fs.writeFileSync("istro-dictionary.json", `{${deepStringify(isdi)}}`);
  148. sendMessage("You voted for this post!", channel);
  149. }
  150. } else
  151. sendMessage("Usage: .isdi [action (define:list:vote:add:remove)]", channel);
  152. }
  153.  
  154. else if (command === ".define") {
  155. let xhr = new XMLHttpRequest();
  156. xhr.open("GET", "https://www.urbandictionary.com/define.php?term=" + args.splice(1).join("+"), true);
  157. xhr.onreadystatechange = async function() {
  158. if (xhr.readyState === 4) {
  159. let response = xhr.responseText;
  160. if (response.includes("Sorry, we couldn't find"))
  161. return sendMessage("Can not find definition :(", channel);
  162. response = response.slice(response.indexOf("class=\"meaning") + 16, response.length);
  163. response = response.slice(0, response.indexOf("</div>")).replace(/<br\/>/g, " ").replace(/&apos;/g, "'").replace(/&quot;/g, '"').split("<a").join("</a>").split("</a>").join(");\">").split(");\">");
  164. for (let i = 0; i < response.length; i ++) {
  165. if (response[i].startsWith(" class=\"autolink\""))
  166. response.splice(i, 1);
  167. }
  168. sendMessage("Definition from Urban Dictionary: " + response.join(""), channel);
  169. }
  170. };
  171. xhr.send(null);
  172. }
  173.  
  174. else if (text.toLowerCase().startsWith("snullify")) {
  175. let output = msgPointer("snullify").split(" ");
  176. for (let i = 0; i < output.length; i ++) {
  177. if (" hello hey hi ".includes(" " + output[i] + " ")) {
  178. output[i] += ", r-retard ";
  179. }
  180. if (Math.random() < 0.2 || i === 0) {
  181. output[i] = output[i][0] + "-" + output[i];
  182. }
  183. }
  184. output = output.join(" ");
  185. if (!".,?!:;".includes(output[output.length - 1]))
  186. output += "...";
  187. sendMessage(name + ": " + output, channel)
  188. }
  189.  
  190. else if (text.toLowerCase().startsWith("ecclify")) {
  191. let output = msgPointer("ecclify");
  192. let random = Math.random();
  193. if (random <= 0.25) {
  194. sendMessage(name + ": " + output + ", also im gay lol", channel, 1);
  195. } else if (random <= 0.5) {
  196. sendMessage(name + ": " + output + ", eat my ass", channel, 1);
  197. } else if (random <= 0.75) {
  198. sendMessage(name + ": " + output + " libtard", channel, 1);
  199. } else {
  200. for (let i = 3; i < 5 + Math.random() * 10; i ++) {
  201. output += ".";
  202. }
  203. sendMessage(name + ": " + output, channel, 1);
  204. }
  205. }
  206.  
  207. else if (text.toLowerCase().startsWith("zalgoify")) {
  208. let output = msgPointer("zalgoify").split(" ");
  209. let zalgoUp = ["\u030d", "\u030e", "\u0304", "\u0305", "\u033f", "\u0311", "\u0306", "\u0310", "\u0352", "\u0357", "\u0351", "\u0307","\u0308", "\u030a", "\u0342", "\u0343","\u0344", "\u034a", "\u034b", "\u034c", "\u0303", "\u0302", "\u030c", "\u0350", "\u0300", "\u0301", "\u030b", "\u030f", "\u0312", "\u0313", "\u0314", "\u033d", "\u0309", "\u0363", "\u0364", "\u0365", "\u0366", "\u0367", "\u0368", "\u0369", "\u036a", "\u036b", "\u036c", "\u036d", "\u036e", "\u036f", "\u033e", "\u035b", "\u0346", "\u031a"];
  210. let zalgoMid = ["\u0315", "\u031b", "\u0340", "\u0341", "\u0358", "\u0321", "\u0322", "\u0327", "\u0328", "\u0334", "\u0335", "\u0336", "\u034f", "\u035c", "\u035d", "\u035e", "\u035f", "\u0360", "\u0362", "\u0338", "\u0337", "\u0361", "\u0489"];
  211. let zalgoDown = ["\u0316", "\u0317", "\u0318", "\u0319", "\u031c", "\u031d", "\u031e", "\u031f", "\u0320", "\u0324", "\u0325", "\u0326", "\u0329", "\u032a", "\u032b", "\u032c", "\u032d", "\u032e", "\u032f", "\u0330", "\u0331", "\u0332", "\u0333", "\u0339", "\u033a", "\u033b", "\u033c", "\u0345", "\u0347", "\u0348", "\u0349", "\u034d", "\u034e", "\u0353", "\u0354", "\u0355", "\u0356", "\u0359", "\u035a", "\u0323"];
  212. for (let i = 0; i < output.length; i ++) {
  213. if (output[i] === " ")
  214. continue;
  215. for (let j = 0; j < Math.random() * 10; j ++) {
  216. output[i] += zalgoUp[Math.floor(Math.random() * zalgoUp.length)];
  217. }
  218. for (let j = 0; j < Math.random() * 10; j ++) {
  219. output[i] += zalgoDown[Math.floor(Math.random() * zalgoDown.length)];
  220. }
  221. output[i] += zalgoMid[Math.floor(Math.random() * zalgoMid.length)];
  222. }
  223. sendMessage(name + ": " + output.join(""), channel, 1);
  224. }
  225.  
  226. else if (text.toLowerCase().startsWith("smartify")) {
  227. let output = msgPointer("smartify").split(" ");
  228. let stopWords = " i am the ";
  229. for (let i = 0; i < output.length; i ++) {
  230. if (stopWords.includes(output[i]) && i !== output.length - 1)
  231. continue;
  232. let xhr = new XMLHttpRequest();
  233. xhr.open("GET", "https://www.thesaurus.com/browse/" + output[i].toLowerCase(), true);
  234. xhr.onreadystatechange = async function() {
  235. if (xhr.readyState === 4) {
  236. let response = xhr.responseText;
  237. response = response.slice(response.indexOf("h2"), response.length);
  238. response = response.slice(0, response.indexOf("</ul>")).replace(/li/, "").split("<a");
  239. response = response.splice(1, response.length);
  240. for (let j = 0; j < response.length; j ++) {
  241. response[j] = response[j].slice(response[j].indexOf("\">") + 2, response[j].indexOf("</a"));
  242. }
  243. response.sort((a, b) => { return b.length - a.length; });
  244. if (response[0])
  245. output[i] = await response[0];
  246. if (i === output.length - 1) {
  247. sendMessage(name + ": " + output.join(" "), channel, 1);
  248. }
  249. }
  250. };
  251. xhr.send(null);
  252. }
  253. }
  254.  
  255. else if (text.toLowerCase().startsWith("owoify")) {
  256. let output = msgPointer("owoify").split(" ");
  257. for (let i in output) {
  258. for (let j = 0; j < output[i].length; j ++) {
  259. if (j > 0 && "aeiou".includes(output[i][j]) && output[i][j - 1].toLowerCase() !== "w") {
  260. output[i] = output[i].slice(0, j) + "w" + output[i].slice(j);
  261. break;
  262. }
  263. }
  264. let random = Math.random();
  265. if (random < 0.1) {
  266. output[i] += " OwO ";
  267. } else if (random < 0.2) {
  268. output[i] += " ~nuzzles~ ";
  269. }
  270. }
  271. sendMessage(name + ": " + output.join(" "), channel, 1);
  272. }
  273.  
  274. else if (text.toLowerCase().startsWith("therxify")) {
  275. let output = msgPointer("therxify").split(" ");
  276. let keyboard = [
  277. "qwertyuiop",
  278. "asdfghjkl;",
  279. "zxcvbnnm,."
  280. ];
  281. let splitSize = output.length > 5 ? Math.ceil(output.length / 3) : 6; //3 chunks max
  282. output = [output.slice(0, splitSize).join(" "), output.slice(splitSize, splitSize * 2).join(" "), output.slice(splitSize * 2, splitSize * 3 + 1).join(" ")];
  283. for (let i = 0; i < output.length; i ++) {
  284. for (let j = 0; j < output[i].length; j ++) {
  285. if (Math.random() < 0.15) {
  286. let letter = output[i][j];
  287. let index = -1;
  288. if (keyboard[0].includes(letter)) {
  289. index = 0;
  290. } else if (keyboard[1].includes(letter)) {
  291. index = 1;
  292. } else if (keyboard[2].includes(letter)) {
  293. index = 2;
  294. }
  295. if (index !== -1) {
  296. let position = [index + Math.floor(Math.random() * 3 - 1), keyboard[index].indexOf(letter) + Math.floor(Math.random() * 3 - 1)];
  297. if (keyboard[position[0]] !== undefined && keyboard[position[0]][position[1]] !== undefined) {
  298. output[i] = output[i].slice(0, j) + keyboard[position[0]][position[1]] + output[i].slice(j + 1);
  299. }
  300. }
  301. }
  302. }
  303. if ((output[i + 1] === "" || i === 2) && output[i] !== "")
  304. output[i] += " LOL";
  305. if (output[i] !== "")
  306. sendMessage(name + ": " + output[i], channel);
  307. }
  308. }
  309.  
  310. else if (text.toLowerCase().startsWith("voidify")) {
  311. let output = msgPointer("voidify").split(" ");
  312. for (let i in output) {
  313. console.log(output[i]);
  314. if ((output.length > 5 && Math.random() < 0.1) || " i he she him her our their my me your its ".includes(output[i].toLowerCase()))
  315. output.splice(i, 1);
  316. }
  317. output = output.join(" ");
  318. output = output.slice(0, 1).toUpperCase() + output.slice(1);
  319. if (Math.random() < 0.1)
  320. output = output.toUpperCase(); //angry void
  321. if (!".,?!:;".includes(output[output.length - 1]))
  322. output += ".";
  323. sendMessage(name + ": " + output, channel);
  324. }
  325.  
  326. else if (text.toLowerCase().startsWith("xiaoify")) {
  327. let output = msgPointer("xiaoify");
  328. (async () => {
  329. output = await translate(output, {to: "zh", engine: "yandex", key: "trnsl.1.1.20200101T020837Z.b6e0dc21ae4464a4.3199765e39be8ec23b1f75aa08f6e3ea9f2137f5"});
  330. if (Math.random() < 0.9)
  331. output = await translate(output, {to: "en", from: "zh", engine: "yandex", key: "trnsl.1.1.20200101T020837Z.b6e0dc21ae4464a4.3199765e39be8ec23b1f75aa08f6e3ea9f2137f5"});
  332. sendMessage(name + ": " + output, channel, 1);
  333. })();
  334. }
  335.  
  336. }
  337. });
  338.  
  339. setInterval(() => {
  340. if (messageQueue[0]) {
  341. ws.send(JSON.stringify(["message", {text: messageQueue[0].text, channel: messageQueue[0].channel}]));
  342. messageQueue.splice(0, 1);
  343. }
  344. }, 500);
  345.  
  346. ws.on("close", () => {
  347. console.log("Reconnecting");
  348. setTimeout(connect, 500);
  349. });
  350. }
  351.  
  352. connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement