示例#1
0
 public async Task ConnectAsync()
 {
     _webSocket = new ClientWebSocket();
     _webSocket.Options.SetRequestHeader("User-Id", $"{_config.UserId}");
     _webSocket.Options.SetRequestHeader("Num-Shards", $"{_config.Shards}");
     _webSocket.Options.SetRequestHeader("Authorization", _config.Authorization);
     _lavalink.LogInfo($"Connecting to Lavalink node at {_config.Endpoint.Host}:{_config.Endpoint.Port}");
     try
     {
         await _webSocket.ConnectAsync(new Uri($"ws://{_config.Endpoint.Host}:{_config.Endpoint.Port}"),
                                       CancellationToken.None).ContinueWith(AfterConnect);
     }
     catch
     {
         // Ignored
     }
 }
示例#2
0
        private async Task OnUVSU(SocketUser user, SocketVoiceState oldState, SocketVoiceState newState)
        {
            if (user.Id != _baseClient.CurrentUser.Id)
            {
                return;
            }

            switch (oldState)
            {
            case var state when state.VoiceChannel is null && !(newState.VoiceChannel is null):
                _voiceState = newState;
                _lavalink.LogDebug($"Voice state updated. Voice session Id: {_voiceState.VoiceSessionId}");
                break;

            case var state when !(state.VoiceChannel is null) && newState.VoiceChannel is null:
                if (!_players.TryGetValue(state.VoiceChannel.Guild.Id, out var oldPlayer))
                {
                    return;
                }
                await oldPlayer.DisconnectAsync().ConfigureAwait(false);

                _players.TryRemove(state.VoiceChannel.Guild.Id, out _);
                LavaSocket.SendPayload(new DestroyPayload(state.VoiceChannel.Guild.Id));
                break;

            case var state when state.VoiceChannel?.Id != newState.VoiceChannel?.Id:
                if (!_players.TryGetValue(state.VoiceChannel.Guild.Id, out var updatePlayer))
                {
                    return;
                }
                updatePlayer.VoiceChannel = newState.VoiceChannel;
                _players.TryUpdate(state.VoiceChannel.Guild.Id, updatePlayer, updatePlayer);
                _lavalink.LogInfo(
                    $"Moved from {state.VoiceChannel.Name} to {newState.VoiceChannel.Name}.");
                break;
            }
        }