Skip to content

Commit b595790

Browse files
authored
Implement custom events (#113)
* Implement custom events * Add a test
1 parent 00e41e8 commit b595790

File tree

17 files changed

+542
-254
lines changed

17 files changed

+542
-254
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<LangVersion>latest</LangVersion>
7+
<LangVersion>preview</LangVersion>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<IsAotCompatible>true</IsAotCompatible>
1010
<NoWarn>1591</NoWarn>

Documentation/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
6-
<LangVersion>latest</LangVersion>
6+
<LangVersion>preview</LangVersion>
77
<Nullable>enable</Nullable>
88
<AssemblyName>MyBot</AssemblyName>
99
<RootNamespace>MyBot</RootNamespace>

NetCord.slnx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@
7171
<File Path="SourceGenerators/Directory.Build.targets" />
7272
<Project Path="SourceGenerators/MethodsForPropertiesGenerator/MethodsForPropertiesGenerator.csproj" />
7373
<Project Path="SourceGenerators/RestClientMethodAliasesGenerator/RestClientMethodAliasesGenerator.csproj" />
74-
<Project Path="SourceGenerators/ShardedGatewayClientEventsGenerator/ShardedGatewayClientEventsGenerator.csproj" Id="cb9012b4-aed0-4ab9-b182-aa167bdb9b96" />
74+
<Project Path="SourceGenerators/ShardedGatewayClientEventsGenerator/ShardedGatewayClientEventsGenerator.csproj" />
7575
<Project Path="SourceGenerators/Shared/Shared.csproj" />
7676
<Project Path="SourceGenerators/UserAgentHeaderGenerator/UserAgentHeaderGenerator.csproj" />
77+
<Project Path="SourceGenerators/WebSocketClientEventsGenerator/WebSocketClientEventsGenerator.csproj" />
7778
</Folder>
7879
<Folder Name="/Tests/">
7980
<File Path="Tests/Directory.Build.props" />

NetCord/Gateway/GatewayClient.cs

Lines changed: 145 additions & 145 deletions
Large diffs are not rendered by default.

NetCord/Gateway/Voice/VoiceClient.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
namespace NetCord.Gateway.Voice;
1313

14-
public class VoiceClient : WebSocketClient
14+
public partial class VoiceClient : WebSocketClient
1515
{
16-
public event Func<VoiceReceiveEventArgs, ValueTask>? VoiceReceive;
17-
public event Func<ValueTask>? Ready;
18-
public event Func<UserConnectEventArgs, ValueTask>? UserConnect;
19-
public event Func<UserDisconnectEventArgs, ValueTask>? UserDisconnect;
16+
public partial event Func<VoiceReceiveEventArgs, ValueTask>? VoiceReceive;
17+
public partial event Func<ValueTask>? Ready;
18+
public partial event Func<UserConnectEventArgs, ValueTask>? UserConnect;
19+
public partial event Func<UserDisconnectEventArgs, ValueTask>? UserDisconnect;
2020

2121
public ulong UserId { get; }
2222

@@ -192,7 +192,7 @@ void GetIpAndPort(out string ip, out ushort port)
192192
var sessionDescription = payload.Data.GetValueOrDefault().ToObject(Serialization.Default.JsonSessionDescription);
193193
_encryption!.SetKey(sessionDescription.SecretKey);
194194
InvokeLog(LogMessage.Info("Ready"));
195-
var readyTask = InvokeEventAsync(Ready);
195+
var readyTask = InvokeEventAsync(_ready);
196196

197197
state.IndicateReady(connectionState);
198198

@@ -237,13 +237,13 @@ void GetIpAndPort(out string ip, out ushort port)
237237
case VoiceOpcode.ClientConnect:
238238
{
239239
var json = payload.Data.GetValueOrDefault().ToObject(Serialization.Default.JsonClientConnect);
240-
await InvokeEventAsync(UserConnect, new UserConnectEventArgs(json.UserIds)).ConfigureAwait(false);
240+
await InvokeEventAsync(_userConnect, new UserConnectEventArgs(json.UserIds)).ConfigureAwait(false);
241241
}
242242
break;
243243
case VoiceOpcode.ClientDisconnect:
244244
{
245245
var json = payload.Data.GetValueOrDefault().ToObject(Serialization.Default.JsonClientDisconnect);
246-
await InvokeEventAsync(UserDisconnect, new(json.UserId), args =>
246+
await InvokeEventAsync(_userDisconnect, new(json.UserId), args =>
247247
{
248248
var userId = args.UserId;
249249
var cache = Cache;
@@ -259,12 +259,12 @@ void GetIpAndPort(out string ip, out ushort port)
259259
}
260260
}
261261

262-
internal ValueTask InvokeVoiceReceiveAsync(VoiceReceiveEventArgs data) => InvokeEventAsync(VoiceReceive, data);
262+
internal ValueTask InvokeVoiceReceiveAsync(VoiceReceiveEventArgs data) => InvokeEventAsync(_voiceReceive, data);
263263

264264
private async void HandleDatagramReceive(UdpReceiveResult obj)
265265
{
266-
var @event = VoiceReceive;
267-
if (@event is not null)
266+
var handlers = _voiceReceive;
267+
if (!handlers.IsEmpty)
268268
{
269269
try
270270
{

0 commit comments

Comments
 (0)