Skip to content

Commit

Permalink
Update old JavaScript code with Reddit API and fixed websocket error …
Browse files Browse the repository at this point in the history
…code

Fixed the old code to include the Reddit API and the fix to the WebSocket connection error description
  • Loading branch information
Gisgar3 committed Oct 1, 2018
1 parent 55ec5e9 commit 954b382
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 18 deletions.
41 changes: 35 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -579,6 +607,7 @@ var ERROR;
ERROR["RATZx0000007"] = "RATZx0000007";
ERROR["RATZx0000008"] = "RATZx0000008";
ERROR["RATZx0000009"] = "RATZx0000009";
ERROR["RATZx0000010"] = "RATZx0000010";
})(ERROR || (ERROR = {}));
;
var TYPE;
Expand Down
53 changes: 41 additions & 12 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -545,6 +573,7 @@ var ERROR;
ERROR["RATZx0000007"] = "RATZx0000007";
ERROR["RATZx0000008"] = "RATZx0000008";
ERROR["RATZx0000009"] = "RATZx0000009";
ERROR["RATZx0000010"] = "RATZx0000010";
})(ERROR || (ERROR = {}));
;
var TYPE;
Expand Down Expand Up @@ -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();
}

0 comments on commit 954b382

Please sign in to comment.