From 954b38282f651bb1c3abdff2cfcdb80758ddd452 Mon Sep 17 00:00:00 2001 From: Gavin Isgar Date: Mon, 1 Oct 2018 16:33:56 -0400 Subject: [PATCH] Update old JavaScript code with Reddit API and fixed websocket error code Fixed the old code to include the Reddit API and the fix to the WebSocket connection error description --- main.js | 41 +++++++++++++++++++++++++++++++++++------ main.ts | 53 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/main.js b/main.js index 2451616..790406c 100644 --- a/main.js +++ b/main.js @@ -431,6 +431,33 @@ bot.on('message', function (message) { } }); } + /* -----REDDIT API/CUSTOM----- + STILL NEEDS AUTH VIA CODE IN TOKENS.JSON + if (message.content.toString() == "/ratz motivationalquote") { + var getNewReddit = { + url: "https://www.reddit.com/api/v1/r/GetMotivated/new", + method: "GET", + headers: { + "User-Agent": "windows:yMuEkegXP0k93A:v3.1.1 (by /u/Gisgar3)" + } + }; + request(getNewReddit, function (error, response, body) { + try { + if (!error && response.StatusCode == 200) { + var stats = JSON.parse(body); + message.channel.send(stats); + } + else { + message.channel.send(sendError(TYPE.BACKEND, ERROR.RATZx0000010)); + appendError(TYPE.BACKEND, ERROR.RATZx0000010, message.author.id, error); + } + } + catch (err) { + message.channel.send(sendError(TYPE.BACKEND, ERROR.RATZx0000010)); + appendError(TYPE.BACKEND, ERROR.RATZx0000010, message.author.id, err); + } + }); + */ // -----VOICE CHANNELS----- if (message.content.startsWith("/ratz play ")) { var result = message.content.slice(11); @@ -478,26 +505,24 @@ bot.on('message', function (message) { else { fs.appendFileSync("./exclude/bannedmessages.ratz", "\r\n(" + message.author.username + " [" + message.author.id + "], " + message.createdTimestamp + ") " + message.content.toString()); message["delete"](); - removeMessage(message.content.toString(), message.author.id); message.channel.send(message.author + ", your message was deleted because it possibly relates to a blocked topic. To appeal the deletion of your message, contact an administrator."); } } if (message.content.includes("NIGGER") || message.content.includes("nigger") || message.content.includes("NIGGA") || message.content.includes("nigga")) { fs.appendFileSync("./exclude/bannedmessages.ratz", "\r\n(" + message.author.username + " [" + message.author.id + "], " + message.createdTimestamp + ") " + message.content.toString()); message["delete"](); - removeMessage(message.content.toString(), message.author.id); message.channel.send(message.author + ", your message was deleted because it possibly relates to a blocked topic. To appeal the deletion of your message, contact an administrator."); } // -------------------------------- - if (message.content.startsWith("/ratz testdetermine")) { + /*if (message.content.startsWith("/ratz testdetermine")) { message.channel.send(teachAI()); - } + }*/ } } } } catch (err) { - appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", err); + appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", err.toString()); } }); bot.on("guildMemberAdd", function (member) { @@ -507,7 +532,7 @@ bot.on("guildMemberRemove", function (member) { bot.channels.get(newcomerChannel).send("**| GOODBYE |** " + member + " has left the server!"); }); bot.on("error", function (error) { - appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", error); + appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", error.toString()); }); bot.on("disconnect", function (userconnection) { try { @@ -550,6 +575,9 @@ rl.on("line", function (input) { console.log("**ERROR**: " + err); } } + if (input.toString() == "/ratz clear") { + console.clear(); + } if (input.toString().startsWith("/ratz writemain ")) { var result = input.slice(16); try { @@ -579,6 +607,7 @@ var ERROR; ERROR["RATZx0000007"] = "RATZx0000007"; ERROR["RATZx0000008"] = "RATZx0000008"; ERROR["RATZx0000009"] = "RATZx0000009"; + ERROR["RATZx0000010"] = "RATZx0000010"; })(ERROR || (ERROR = {})); ; var TYPE; diff --git a/main.ts b/main.ts index 862cb19..c4c6f7e 100644 --- a/main.ts +++ b/main.ts @@ -397,6 +397,33 @@ bot.on('message', function (message) { } }); } + /* -----REDDIT API/CUSTOM----- + STILL NEEDS AUTH VIA CODE IN TOKENS.JSON + if (message.content.toString() == "/ratz motivationalquote") { + var getNewReddit = { + url: "https://www.reddit.com/api/v1/r/GetMotivated/new", + method: "GET", + headers: { + "User-Agent": "windows:yMuEkegXP0k93A:v3.1.1 (by /u/Gisgar3)" + } + }; + request(getNewReddit, function (error, response, body) { + try { + if (!error && response.StatusCode == 200) { + var stats = JSON.parse(body); + message.channel.send(stats); + } + else { + message.channel.send(sendError(TYPE.BACKEND, ERROR.RATZx0000010)); + appendError(TYPE.BACKEND, ERROR.RATZx0000010, message.author.id, error); + } + } + catch (err) { + message.channel.send(sendError(TYPE.BACKEND, ERROR.RATZx0000010)); + appendError(TYPE.BACKEND, ERROR.RATZx0000010, message.author.id, err); + } + }); + */ // -----VOICE CHANNELS----- if (message.content.startsWith("/ratz play ")) { var result = message.content.slice(11); @@ -444,26 +471,24 @@ bot.on('message', function (message) { else { fs.appendFileSync("./exclude/bannedmessages.ratz", "\r\n(" + message.author.username + " [" + message.author.id + "], " + message.createdTimestamp + ") " + message.content.toString()); message["delete"](); - removeMessage(message.content.toString(), message.author.id); message.channel.send(message.author + ", your message was deleted because it possibly relates to a blocked topic. To appeal the deletion of your message, contact an administrator."); } } if (message.content.includes("NIGGER") || message.content.includes("nigger") || message.content.includes("NIGGA") || message.content.includes("nigga")) { fs.appendFileSync("./exclude/bannedmessages.ratz", "\r\n(" + message.author.username + " [" + message.author.id + "], " + message.createdTimestamp + ") " + message.content.toString()); - message["delete"](); - removeMessage(message.content.toString(), message.author.id); + message["delete"](); message.channel.send(message.author + ", your message was deleted because it possibly relates to a blocked topic. To appeal the deletion of your message, contact an administrator."); } // -------------------------------- - if (message.content.startsWith("/ratz testdetermine")) { + /*if (message.content.startsWith("/ratz testdetermine")) { message.channel.send(teachAI()); - } + }*/ } } } } catch (err) { - appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", err); + appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", err.toString()); } }); bot.on("guildMemberAdd", function (member) { @@ -473,7 +498,7 @@ bot.on("guildMemberRemove", function (member) { bot.channels.get(newcomerChannel).send("**| GOODBYE |** " + member + " has left the server!"); }); bot.on("error", function (error) { - appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", error); + appendError(TYPE.BACKEND, ERROR.RATZx0000009, "SYSTEM", error.toString()); }); bot.on("disconnect", function (userconnection) { try { @@ -516,6 +541,9 @@ rl.on("line", function (input) { console.log("**ERROR**: " + err); } } + if (input.toString() == "/ratz clear") { + console.clear(); + } if (input.toString().startsWith("/ratz writemain ")) { var result = input.slice(16); try { @@ -545,6 +573,7 @@ var ERROR; ERROR["RATZx0000007"] = "RATZx0000007"; ERROR["RATZx0000008"] = "RATZx0000008"; ERROR["RATZx0000009"] = "RATZx0000009"; + ERROR["RATZx0000010"] = "RATZx0000010"; })(ERROR || (ERROR = {})); ; var TYPE; @@ -612,16 +641,16 @@ function rollAdvert() { // ---------- async function teachAI() { const model = tf.sequential(); - model.add(tf.layers.dense({units: 1, inputShape: [1]})); + model.add(tf.layers.dense({ units: 1, inputShape: [1] })); model.compile({ loss: 'meanSquaredError', optimizer: 'sgd' }); // Shape the output - const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6,1]); - const ys = tf.tensor2d([0.10, 1.20, 2.30, 3.40, 4.50, 5.60], [6,1]); + const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]); + const ys = tf.tensor2d([0.10, 1.20, 2.30, 3.40, 4.50, 5.60], [6, 1]); // Train the model - await model.fit(xs, ys, {epochs: 500}); + await model.fit(xs, ys, { epochs: 500 }); // Predict the number - var result = model.predict(tf.tensor2d([7], [1,1])).print(); + var result = model.predict(tf.tensor2d([7], [1, 1])).print(); }