Skip to content

Commit

Permalink
fix(client-support/netcord): Use data from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
angelobreuer committed Feb 25, 2024
1 parent 15917cc commit c62b22a
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions src/Lavalink4NET.NetCord/DiscordClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,26 @@ public void Dispose()
_client.VoiceServerUpdate -= HandleVoiceServerUpdateAsync;
}

public async ValueTask<ImmutableArray<ulong>> GetChannelUsersAsync(ulong guildId, ulong voiceChannelId, bool includeBots = false, CancellationToken cancellationToken = default)
public ValueTask<ImmutableArray<ulong>> GetChannelUsersAsync(ulong guildId, ulong voiceChannelId, bool includeBots = false, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var restGuild = await _client.Rest
.GetGuildAsync(guildId)
.WaitAsync(cancellationToken)
.ConfigureAwait(false);

if (restGuild is not Guild guild)
if (!_client.Cache.Guilds.TryGetValue(guildId, out var guild))
{
return [];
return new ValueTask<ImmutableArray<ulong>>([]);
}

var currentUser = await _client.Rest
.GetCurrentUserAsync()
.WaitAsync(cancellationToken)
.ConfigureAwait(false);

var voiceStates = guild.VoiceStates
.Where(x => x.Value.ChannelId == voiceChannelId)
.Where(x => x.Value.UserId != currentUser.Id);
.Where(x => x.Value.UserId != _client.Id);

if (!includeBots)
{
voiceStates = voiceStates.Where(x => x.Value.User is not { IsBot: true, });
}

return voiceStates.Select(x => x.Value.UserId).ToImmutableArray();
var userIds = voiceStates.Select(x => x.Value.UserId).ToImmutableArray();
return new ValueTask<ImmutableArray<ulong>>(userIds);
}

public async ValueTask SendVoiceUpdateAsync(ulong guildId, ulong? voiceChannelId, bool selfDeaf = false, bool selfMute = false, CancellationToken cancellationToken = default)
Expand All @@ -86,12 +77,7 @@ await _client.ReadyAsync

var shardCount = _client.Shard?.Count ?? 0;

var currentUser = await _client.Rest
.GetCurrentUserAsync()
.WaitAsync(cancellationToken)
.ConfigureAwait(false);

return new ClientInformation("NetCord", currentUser.Id, shardCount);
return new ClientInformation("NetCord", _client.Id, shardCount);
}

private ValueTask HandleVoiceServerUpdateAsync(VoiceServerUpdateEventArgs eventArgs)
Expand Down Expand Up @@ -120,18 +106,14 @@ private async ValueTask HandleVoiceStateUpdateAsync(global::NetCord.Gateway.Voic
? new Clients.VoiceState(VoiceChannelId: previousVoiceStateData.ChannelId, SessionId: previousVoiceStateData.SessionId)
: default;

var currentUser = await _client.Rest
.GetCurrentUserAsync()
.ConfigureAwait(false);

var updatedVoiceState = new Clients.VoiceState(
VoiceChannelId: eventArgs.ChannelId,
SessionId: eventArgs.SessionId);

var voiceStateUpdatedEventArgs = new VoiceStateUpdatedEventArgs(
eventArgs.GuildId,
eventArgs.UserId,
eventArgs.UserId == currentUser.Id,
eventArgs.UserId == _client.Id,
updatedVoiceState,
previousVoiceState);

Expand Down

0 comments on commit c62b22a

Please sign in to comment.