Monday, October 19, 2020

Discord Monitor: They Why? and Updates

 Hello. Sorry about the long gap between the last post. Got supper into working on avatar pathing (more at the bottom). In this post, I want to cover my reasoning for the choices I made in this project to give insight into project decision making. 

For Unity, as I mentioned in my first post, I don't really have a concreate reason for choosing Unity aside from that's what everyone seemed to be using. What I forgot to mention in my first post, was that I had messed around with Unity for moding before starting this project, so that did influence my decision. 

Moving onto Discord, we need to pull information from Discord so we can display user state information in Unity. Luckily for us, Discord has a kick ass api and bot support that we can use for this task. Now, we can by all means write code in Unity that use the Discord api. However, Unity is already pretty busy with being a game engine, so it's better if we have this in a separate process. This opens up our choices on what we can use. Since we have all of this freedom, why even use the raw Discord api. It's quite complex with lots of logic and little intricates. It would be nice if we could use a library to handle the whole communicating to Discord. Lucky for use there is just such a thing, Discord.js.

Discord.js is written in JavaScript and runes on node.js. What is node.js? Simply put, it's JavaScript but it runs on a server instead of the traditional web browser. So, Discord.js will take care of the low level Discord communication for us and allow us to focus on pulling the data we need from Discord. From this point on, I'll call this part the server. 

A quick side note before moving onto the last problem to solve. If you look at the Discord bot repo, you will notice that it is not written in plain JS, instead it is written in TypeScript. TypeScript adds type and other langue syntax sprinkles to JS which is then transpiled into JS. So if it can run JS, it can run TypeScript. I highly recommend using TypeScript for your next project or, if you can, switch to it for an existing project.

The Discord Bot in Action

Ok, now that's out of the way, on to the last problem; communication.  Because we are using two process, we need a way for them to talk to each other so that Unity can get the data it needs. There are a million ways we can could go about this. There is, however, some limiting factors. One being easy of use (we don't have all time in the world 😉) and the fact that we are dealing with what I'll call "live data." What that means is that data is not static and is changing at indeterminate points. So if we want Unity to always be update on what is happing, we need a way for the server to push data to Unity. Other wise Unity will have to constantly bug the server for new information (talk about a huge performance hit). Luckily, there a communication method that fits our requirements exactly, WebSockets

WebSockets upgrades an existing http/https connection to allow both the client and server to push data. Awesome. So when ever the server sees that the Discord data has changed, it can push these changes to Unity. Better yet, there are already libraries when can use that we take care of the WebSocket logic for us. For the server it's ws and for Unity System.Net.WebSockets. Nice. We use plain text messages written in JSON since it's the core of JS and we can use Json.net for Unity.

Alright that's all the parts need to make this bad boy work. Phew, that was a long one. Thanks for sticking around and reading all of it. Next, I'll cover the code needed to get these parts working, broken into two parts for the server and Unity.

Updates

I updated the bots readme to cover information about where to supply the bot token. Sorry that I missed that one. For the unity side of things, I got a test avatar to path from a spawn location to another location marking a voice channel.


Now I just need to add idle pathing and get the Discord bot to report when users join a voice channel. But, before all that, I need to add an avatar simulator so I can test things without using the Discord bot. Supper important and I have no idea how I could have missed something that big. But, hey, that's just how things are some time.

Oh, I should also mention that I use milestone branches for the Unity code. So if you go there and don't see a whole lot of updates on the master branch, switch to the current milestone branch and you will see all the changes there.

I'll finish this post by saying how blown away I was by the number of views on the last post and that I'm supper excited to continue working on this project.

In case you didn't know, you can support my work using this Bitcoin address :

3Gz9nXZFPm3NtcZWjn4Bq5jALsTqQBTbWU

Supper wired I know but it's the only payment option that doesn't have a bunch of legal read type and require me to dox my self.

Look forward to seeing you in the next post.

No comments:

Post a Comment