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.

Wednesday, October 14, 2020

Creating a Discord monitor in Unity

Recently I stopped making excuses, mainly that I don't have the need artistic skills, and started learning Unity. Why Unity? 🤷  It seemed to be what the cool kids were using. As an added bonus, picking Unity meant adding a new language to my belt, C#. Learning Unity was strait forward and I quickly got the basic down. Now I need a good beginner project to take my Unity skills to the next level.

After much humming and hawing, I came up with idea of making a Discord monitor. The basic of it is I would use Unity to communicate with a Discord bot to show 3D avatars representing the Discord Users online in a given server. The perfect beginner project, strait forward and simple to implement with good opportunities to expand.

After some time, maybe a week, I got this:


Unity spawns little blue avatars showing online Discord users in my server. Their server display name is shown above their heads.

The ultimate end goal for this project is to have a fully fleshed out environment that the avatars can wonder around and interact with. Representing actions and interactions that Discord users take in the server. Also I'll probably sprinkle in some server management tools in the monitor.

In the more immediate now, I'm going to work on avatar pathing. The main goal being that when users join a voice channel, there avatar will walk to a room in the level representing that voice channel.

The code repo for the monitor can be found here and the Discord bot can be found here. They use WebSockets to send JSON messages to each other. With the Discord bot being the WebSocket server. In latter post I will go into detail of how this all works.