Gateway Interface
Communication with Discord's WebSocket API happens through the Gateway. To interact with the Gateway, an instance of Discore.WebSocket.IDiscordGateway is needed. This can be retrieved from any Shard via the Gateway property.
Gateway Events
The IDiscordGateway interface contains C# events for most events documented by the Discord API. The full list of supported events can be viewed here. The names of each C# event follow the pattern "On" + the name of the Gateway event.
For example, to listen for the Message Create event:
gateway.OnMessageCreate += Gateway_OnMessageCreate;
...
void Gateway_OnMessageCreate(object? sender, MessageCreateEventArgs e)
{
// Handle the message
}
Note: the sender argument for each event is always the IDiscordGateway instance (despite it being nullable).
Gateway Methods
The IDiscordGateway interface exposes methods for making requests to the Gateway, such as:
- Sending self-user presence updates.
- Requesting guild member chunk payloads.
Available methods can be viewed here.
For example, the bot's user status can be set by doing:
// Makes the bot appear to be playing Tunic.
await gateway.UpdatePresenceAsync(new PresenceOptions()
.AddActivity(new ActivityOptions("Tunic")));
Gateway methods will wait until the underlying connection is fully connected. This means that these methods are perfectly safe to use even during an automatic shard reconnection, but the returned Task will take a little longer to complete. However, if the shard is stopped in the middle of a method call, an OperationCanceledException will be thrown.