Skip to content

Commit

Permalink
Add an optional VoiceUser to the user connect and disconnect event args
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Mar 29, 2024
1 parent dee2806 commit 523efb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public sealed class VoiceLinkUserEventArgs : AsyncEventArgs
{
public required VoiceLinkConnection Connection { get; init; }
public required DiscordMember Member { get; init; }
public required VoiceLinkUser? VoiceUser { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ private static async ValueTask ClientConnectedAsync(VoiceLinkConnection connecti
await connection.Extension._userConnected.InvokeAsync(connection.Extension, new VoiceLinkUserEventArgs()
{
Connection = connection,
Member = await connection.Guild.GetMemberAsync(voiceClientConnectedPayload.UserId)
Member = await connection.Guild.GetMemberAsync(voiceClientConnectedPayload.UserId),
VoiceUser = null
});
}

Expand All @@ -151,16 +152,21 @@ private static async ValueTask ClientDisconnectAsync(VoiceLinkConnection connect
// We only receive a member's SSRC when they speak. This means they may not be within the speakers dictionary.
// Since we're going to be receiving a lot more RTP packets than speaking payloads, we index by the SSRC.
// This means we need to iterate through the speakers dictionary to find the user instead of just indexing.
if (connection._speakers.FirstOrDefault(x => x.Value.Member.Id == voiceClientDisconnectedPayload.UserId) is (uint ssrc, VoiceLinkUser voiceLinkUser))
if (connection._speakers.FirstOrDefault(x => x.Value.Member.Id == voiceClientDisconnectedPayload.UserId) is (uint, VoiceLinkUser) kvp)
{
connection._speakers.Remove(ssrc);
voiceLinkUser._audioPipe.Writer.Complete();
connection._speakers.Remove(kvp.Key);
kvp.Value._audioPipe.Writer.Complete();
}
else
{
kvp = default;
}

await connection.Extension._userDisconnected.InvokeAsync(connection.Extension, new VoiceLinkUserEventArgs()
{
Connection = connection,
Member = await connection.Guild.GetMemberAsync(voiceClientDisconnectedPayload.UserId)
Member = await connection.Guild.GetMemberAsync(voiceClientDisconnectedPayload.UserId),
VoiceUser = kvp.Value
});
}

Expand Down

0 comments on commit 523efb4

Please sign in to comment.