Example #1
0
        public async void PopulateGuildsAndChannels()
        {
            using var operation = ProgressManager.CreateOperation();

            try
            {
                var tokenValue = TokenValue?.Trim('"', ' ');
                if (string.IsNullOrWhiteSpace(tokenValue))
                {
                    return;
                }

                var token = new AuthToken(
                    IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
                    tokenValue
                    );

                _settingsService.LastToken = token;

                var discord = new DiscordClient(token);

                var guildChannelMap = new Dictionary <Guild, IReadOnlyList <Channel> >();
                await foreach (var guild in discord.GetUserGuildsAsync())
                {
                    guildChannelMap[guild] = await discord.GetGuildChannelsAsync(guild.Id);
                }

                GuildChannelMap = guildChannelMap;
                SelectedGuild   = guildChannelMap.Keys.FirstOrDefault();
            }
            catch (DiscordChatExporterException ex) when(!ex.IsCritical)
            {
                Notifications.Enqueue(ex.Message.TrimEnd('.'));
            }
        }
        internal void ValidateThrowException()
        {
            var missingParams = new List <string>();

            if (string.IsNullOrEmpty(ConsumerKey.Trim()))
            {
                missingParams.Add(nameof(ConsumerKey));
            }

            if (string.IsNullOrEmpty(ConsumerSecret.Trim()))
            {
                missingParams.Add(nameof(ConsumerSecret));
            }

            if (string.IsNullOrEmpty(TokenValue.Trim()))
            {
                missingParams.Add(nameof(TokenValue));
            }

            if (string.IsNullOrEmpty(TokenSecret.Trim()))
            {
                missingParams.Add(nameof(TokenSecret));
            }

            if (missingParams.Any())
            {
                throw new BricklinkMissingCredentialsException(missingParams);
            }
        }
        public async void PopulateGuildsAndChannels()
        {
            try
            {
                // Set busy state and indeterminate progress
                IsEnabled = false;
                Progress  = -1;

                // Sanitize token
                TokenValue = TokenValue.Trim('"');

                // Create token
                var token = new AuthToken(
                    IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
                    TokenValue);

                // Save token
                _settingsService.LastToken = token;

                // Clear guild to channel map
                _guildChannelsMap.Clear();

                // Get DM channels
                {
                    var guild    = Guild.DirectMessages;
                    var channels = await _dataService.GetDirectMessageChannelsAsync(token);

                    // Order channels
                    channels = channels.OrderBy(c => c.Name).ToArray();

                    _guildChannelsMap[guild] = channels;
                }

                // Get guild channels
                {
                    var guilds = await _dataService.GetUserGuildsAsync(token);

                    foreach (var guild in guilds)
                    {
                        var channels = await _dataService.GetGuildChannelsAsync(token, guild.Id);

                        // Filter and order channels
                        channels = channels.Where(c => c.Type == ChannelType.GuildTextChat).OrderBy(c => c.Name).ToArray();

                        _guildChannelsMap[guild] = channels;
                    }
                }

                // Update available guilds
                AvailableGuilds = _guildChannelsMap.Keys.ToArray();

                // Select the first guild
                SelectedGuild = AvailableGuilds.FirstOrDefault();
            }
            catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Unauthorized)
            {
                Notifications.Enqueue("Unauthorized – make sure the token is valid");
            }
            catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Forbidden)
            {
                Notifications.Enqueue("Forbidden – account may be locked by 2FA");
            }
            finally
            {
                // Reset busy state and progress
                Progress  = 0;
                IsEnabled = true;
            }
        }
        public async void PopulateGuildsAndChannels()
        {
            // Create progress operation
            var operation = ProgressManager.CreateOperation();

            try
            {
                // Sanitize token
                TokenValue = TokenValue.Trim('"');

                // Create token
                var token = new AuthToken(
                    IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
                    TokenValue);

                // Save token
                _settingsService.LastToken = token;

                // Prepare available guild list
                var availableGuilds = new List <GuildViewModel>();

                // Get direct messages
                {
                    // Get fake guild
                    var guild = Guild.DirectMessages;

                    // Get channels
                    var channels = await _dataService.GetDirectMessageChannelsAsync(token);

                    // Create channel view models
                    var channelViewModels = new List <ChannelViewModel>();
                    foreach (var channel in channels)
                    {
                        // Get fake category
                        var category = channel.Type == ChannelType.DirectTextChat ? "Private" : "Group";

                        // Create channel view model
                        var channelViewModel = _viewModelFactory.CreateChannelViewModel(channel, category);

                        // Add to list
                        channelViewModels.Add(channelViewModel);
                    }

                    // Create guild view model
                    var guildViewModel = _viewModelFactory.CreateGuildViewModel(guild,
                                                                                channelViewModels.OrderBy(c => c.Category)
                                                                                .ThenBy(c => c.Model.Name)
                                                                                .ToArray());

                    // Add to list
                    availableGuilds.Add(guildViewModel);
                }

                // Get guilds
                var guilds = await _dataService.GetUserGuildsAsync(token);

                foreach (var guild in guilds)
                {
                    // Get channels
                    var channels = await _dataService.GetGuildChannelsAsync(token, guild.Id);

                    // Get category channels
                    var categoryChannels = channels.Where(c => c.Type == ChannelType.Category).ToArray();

                    // Get text channels
                    var textChannels = channels.Where(c => c.Type == ChannelType.GuildTextChat).ToArray();

                    // Create channel view models
                    var channelViewModels = new List <ChannelViewModel>();
                    foreach (var channel in textChannels)
                    {
                        // Get category
                        var category = categoryChannels.FirstOrDefault(c => c.Id == channel.ParentId)?.Name;

                        // Create channel view model
                        var channelViewModel = _viewModelFactory.CreateChannelViewModel(channel, category);

                        // Add to list
                        channelViewModels.Add(channelViewModel);
                    }

                    // Create guild view model
                    var guildViewModel = _viewModelFactory.CreateGuildViewModel(guild,
                                                                                channelViewModels.OrderBy(c => c.Category)
                                                                                .ThenBy(c => c.Model.Name)
                                                                                .ToArray());

                    // Add to list
                    availableGuilds.Add(guildViewModel);
                }

                // Update available guild list
                AvailableGuilds = availableGuilds;

                // Pre-select first guild
                SelectedGuild = AvailableGuilds.FirstOrDefault();
            }
            catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Unauthorized)
            {
                Notifications.Enqueue("Unauthorized – make sure the token is valid");
            }
            catch (HttpErrorStatusCodeException ex) when(ex.StatusCode == HttpStatusCode.Forbidden)
            {
                Notifications.Enqueue("Forbidden – account may be locked by 2FA");
            }
            finally
            {
                // Dispose progress operation
                operation.Dispose();
            }
        }