Skip to content

Commit 3334551

Browse files
committed
using event dispatcher to handle events
1 parent 1496b2f commit 3334551

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

src/Disc.NET/Gateway/GatewayConnection.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using Disc.NET.Handlers;
1+
using Disc.NET.Dispatcher;
22
using Disc.NET.Shared.Configurations;
33
using Disc.NET.Shared.Enums;
44
using Disc.NET.Shared.Extensions;
55
using Disc.NET.Shared.Serializer;
66
using Microsoft.Extensions.Logging;
77
using System.Net.WebSockets;
8-
using System.Reflection.Metadata;
98
using System.Text.Json;
109
using System.Threading.Channels;
1110

@@ -31,14 +30,17 @@ internal class GatewayConnection
3130
private readonly ClientWebSocket _ws = new();
3231
private readonly Channel<JsonDocument> _channel = Channel.CreateUnbounded<JsonDocument>();
3332
private readonly ILogger<GatewayConnection> _logger;
34-
33+
private readonly EventDispatcher _eventDispatcher;
34+
private readonly AppConfiguration _appConfiguration;
3535
/// <summary>
3636
/// Initializes a new instance of the <see cref="GatewayConnection"/> class.
3737
/// </summary>
3838
/// <param name="logger">Logger instance used for structured logging and diagnostics.</param>
39-
public GatewayConnection(ILogger<GatewayConnection> logger)
39+
public GatewayConnection(AppConfiguration appConfiguration,ILogger<GatewayConnection> logger)
4040
{
41+
_appConfiguration = appConfiguration;
4142
_logger = logger;
43+
_eventDispatcher = new EventDispatcher(_appConfiguration);
4244
}
4345

4446
/// <summary>
@@ -55,7 +57,7 @@ public GatewayConnection(ILogger<GatewayConnection> logger)
5557
/// <item>Starts concurrent loops for reading WebSocket messages and handling them asynchronously.</item>
5658
/// </list>
5759
/// </remarks>
58-
public async Task ConnectAsync(AppConfiguration configuration)
60+
public async Task ConnectAsync()
5961
{
6062
_logger.LogInformation("Connecting to Discord Gateway...");
6163

@@ -76,7 +78,7 @@ public async Task ConnectAsync(AppConfiguration configuration)
7678

7779
// Identify and authenticate
7880
_logger.LogDebug("Sending Identify payload...");
79-
await SendIdentifyPayloadAsync(configuration.Token, configuration).ConfigureAwait(false);
81+
await SendIdentifyPayloadAsync(_appConfiguration).ConfigureAwait(false);
8082
_logger.LogDebug("Identify payload sent.");
8183

8284
// Producer: Reads WebSocket messages and writes them to a channel
@@ -138,8 +140,7 @@ public async Task ConnectAsync(AppConfiguration configuration)
138140
_logger.LogDebug("Dispatching event: {Event}", eventName);
139141
_logger.LogDebug("Event context: {Event}", DiscNetSerializer.GetInstance().Serialize(eventContextData));
140142

141-
await HandlerFactory.CreateHandlerChain(configuration).HandleAsync(eventType, eventContextData, configuration)
142-
.ConfigureAwait(false);
143+
await _eventDispatcher.DispatchAsync(new EventHandlerPayload(eventType, eventContextData));
143144
}
144145
}
145146

@@ -215,14 +216,14 @@ await _ws.SendAsync(
215216
/// <remarks>
216217
/// The Identify payload includes OS, device, and browser properties as required by the Discord API.
217218
/// </remarks>
218-
private async Task SendIdentifyPayloadAsync(string token, AppConfiguration configuration)
219+
private async Task SendIdentifyPayloadAsync(AppConfiguration configuration)
219220
{
220221
var identify = JsonSerializer.Serialize(new
221222
{
222223
op = GatewayOpCode.Identify,
223224
d = new
224225
{
225-
token = $"Bot {token}",
226+
token = $"Bot {configuration.Token}",
226227
intents = configuration.Intents.GetIntIntents(),
227228
properties = new
228229
{

0 commit comments

Comments
 (0)