Skip to content

how it work

AsynchronousMatrix edited this page Apr 14, 2024 · 1 revision

How it work

A simple (I hope) run-down on what the hell i'm even doing to get this working;

DiscordClient.luau

DiscordClient.luau is the main class which the user will be interacting with, the goal of this class is to provide a luau object that represents the Client/Bot on Discord

When first creating a DiscordClient, you'll need to pass in DiscordSettings, which is just a basic object that wants the token your bot will use.

⚠️ This DiscordSettings class by default expects all intents to be enabled, so if you're having trouble w connecting to the discord websocket, enable intents!

Upon creation of a DiscordClient, nothing fancy happens.. the fancy stuff happens when we connect to the Discord API using DiscordClient:connectAsync

👀 For every function I end with Async, I return a Future, allowing us to asynchronously handle a blocking call

at which, the client will start setting up the correct amount of shards, which is what we'll discuss next! 😄

DiscordShard.luau

The DiscordShard.luau implements support for discord bots that may require Sharding, beyond that, it is responsible for the majority of events that come from the Discord Websocket, each shard has an onEvent signal that the DiscordClient listens too, the data recieved from this signal will be the result of a DISPATCH event.

DiscordShard.luau is also responsible for maintaining the connection to the discord API, if a shard for some reason needs to reconnect, whether it be the connection magically zombifying itself, or discord actually requesting the connection to reconnect - the shard is responsible for this logic.

The shard however isn't responsible for the Websocket connection.. for that, we need to look at DiscordWebsocket ...

DiscordWebsocket.luau

Manages the connection to a Lune Socket, this is responsible for connecting to the Discord API, receiving messages as well as sending messages.

Additionally, this class also can close the connection from our side.

Mainly just wraps around the Lune socket though so it's easier to manipulate.

Okay, final check!

Discord Client instantiates a variable amount of Discord Shards, each Discord Shard makes a separate websocket connection to the Discord API, when an event is received, if it's a DISPATCH event - the Discord Shard will relay it back to the client, if it's something else though - Discord Shard will handle it internally

Clone this wiki locally