I've recently been messing around with Node.js and Express, and decided to make a super customizable analytics program for Node.js. I'm sure such a thing already existed, but I figured it would be something fun to do to learn about websockets and nodejs libraries. Anyways, I've gotten a MySQL database working, with a table for each "apikey/user" and one command in the library. Currently I was using it with a discord bot, so I created a newMessage() command that sends the message, the author's info, and the timestamp to the backend, which then puts it into the DB.
Anyways, I'm gonna make a dashboard where the user can create custom analytics types that they can use with the library.
Oh yeah, and I'm calling it JsDoctr for some reason, and using heroku for hosting.
Github: https://github.com/Unicorn808/JsDoctr
Some code:
Used in a Discord bot
Code:
The Library
Code:
Sorry about the wall of text
Let me know if I'm doing anything super wrong!
Anyways, I'm gonna make a dashboard where the user can create custom analytics types that they can use with the library.
Oh yeah, and I'm calling it JsDoctr for some reason, and using heroku for hosting.
Github: https://github.com/Unicorn808/JsDoctr
Some code:
Used in a Discord bot
Code:
const jsDoctr = require('./index.js');
const Discord = require("discord.js");
const bot = new Discord.Client();
const token = 'superSekretToken';
const apikey = '05b51f3dafe2463390ae90d08b77a545';
jsDoctr.connect();
bot.on("message", msg => {
if (msg.content.startsWith('!')) {
let messageAuthor = {
username: msg.author.username + '#' + msg.author.discriminator,
id: msg.author.id
};
jsDoctr.newMessage(jsDoctr.DiscordBot, msg.content, msg.createdTimestamp, messageAuthor, apikey);
}
});
The Library
Code:
const WebSocket = require('ws');
let wsOpen = false;
let ws;
const heroku = 'jsdoctr.herokuapp.com';
const local = 'localhost:8080';
module.exports = {
/**
* @return {string}
*/
DiscordBot: function () {
return 'DiscBot';
},
newMessage: function (type, content, time, author, apikey) {
let message = {
type: type,
stuff: {
content: content,
time: time,
author: author
},
key: apikey
};
if (wsOpen === true) {
ws.send(JSON.stringify(message), function ack(err) {
if (err) {
if (err.startsWith('Error: WebSocket is not open:')) {
console.log('Disconnected from server, reconnecting...');
ws = new WebSocket('ws://' + local);
} else if (err) {
console.log('Unknown Error:', err);
}
}
});
} else {
ws.on('open', function open() {
ws.send(JSON.stringify(message));
});
}
},
connect: function () {
ws = new WebSocket('ws://' + local);
ws.on('message', function incoming(data) {
console.log('Server: ' + data);
});
ws.on('open', function open() {
wsOpen = true;
});
}
};
Sorry about the wall of text
Let me know if I'm doing anything super wrong!