示例#1
0
        private async Task GetYouTubeAuthAsync()
        {
            try
            {
                _youTubeClientInstance.HasCredentials = await _youTubeClientInstance.GetAuthAsync(_botConfig.YouTubeClientId, _botConfig.YouTubeClientSecret);

                if (_youTubeClientInstance.HasCredentials)
                {
                    Playlist           playlist            = null;
                    string             playlistName        = _botConfig.YouTubeBroadcasterPlaylistName;
                    string             defaultPlaylistName = "Twitch Song Requests";
                    SongRequestSetting songRequestSetting  = await _songRequestSetting.GetSongRequestSettingAsync(_broadcasterInstance.DatabaseId);

                    if (string.IsNullOrEmpty(playlistName))
                    {
                        playlistName = defaultPlaylistName;
                    }

                    // Check if YouTube song request playlist still exists
                    if (!string.IsNullOrEmpty(_botConfig.YouTubeBroadcasterPlaylistId))
                    {
                        playlist = await _youTubeClientInstance.GetBroadcasterPlaylistByIdAsync(_botConfig.YouTubeBroadcasterPlaylistId);
                    }

                    if (playlist?.Id == null)
                    {
                        playlist = await _youTubeClientInstance.GetBroadcasterPlaylistByIdAsync(songRequestSetting.RequestPlaylistId);

                        if (playlist?.Id == null)
                        {
                            playlist = await _youTubeClientInstance.GetBroadcasterPlaylistByKeywordAsync(playlistName);

                            if (playlist?.Id == null)
                            {
                                playlist = await _youTubeClientInstance.CreatePlaylistAsync(playlistName,
                                                                                            "Songs requested via Twitch viewers on https://twitch.tv/" + _botConfig.Broadcaster
                                                                                            + " . Playlist automatically created courtesy of https://github.com/SimpleSandman/TwitchBot");
                            }
                        }
                    }

                    _botConfig.YouTubeBroadcasterPlaylistId = playlist.Id;
                    _appConfig.AppSettings.Settings.Remove("youTubeBroadcasterPlaylistId");
                    _appConfig.AppSettings.Settings.Add("youTubeBroadcasterPlaylistId", playlist.Id);

                    _botConfig.YouTubeBroadcasterPlaylistName = playlist.Snippet.Title;
                    _appConfig.AppSettings.Settings.Remove("youTubeBroadcasterPlaylistName");
                    _appConfig.AppSettings.Settings.Add("youTubeBroadcasterPlaylistName", playlist.Snippet.Title);

                    // Find personal playlist if requested
                    playlist     = null;
                    playlistName = _botConfig.YouTubePersonalPlaylistName;

                    // Check if personal YouTube playlist still exists
                    if (!string.IsNullOrEmpty(_botConfig.YouTubePersonalPlaylistId))
                    {
                        playlist = await _youTubeClientInstance.GetPlaylistByIdAsync(_botConfig.YouTubePersonalPlaylistId);
                    }

                    if (playlist?.Id == null && songRequestSetting.PersonalPlaylistId != null)
                    {
                        playlist = await _youTubeClientInstance.GetPlaylistByIdAsync(songRequestSetting.PersonalPlaylistId);
                    }

                    if (playlist?.Id != null && playlist?.Snippet != null)
                    {
                        _botConfig.YouTubePersonalPlaylistId = playlist.Id;
                        _appConfig.AppSettings.Settings.Remove("youTubePersonalPlaylistId");
                        _appConfig.AppSettings.Settings.Add("youTubePersonalPlaylistId", playlist.Id);

                        _botConfig.YouTubePersonalPlaylistName = playlist.Snippet.Title;
                        _appConfig.AppSettings.Settings.Remove("youTubePersonalPlaylistName");
                        _appConfig.AppSettings.Settings.Add("youTubePersonalPlaylistName", playlist.Snippet.Title);
                    }

                    _appConfig.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("TwitchBotConfiguration");

                    // Save song request info into database
                    if (songRequestSetting?.Id != 0 &&
                        (_botConfig.YouTubeBroadcasterPlaylistId != songRequestSetting.RequestPlaylistId ||
                         _botConfig.YouTubePersonalPlaylistId != (songRequestSetting.PersonalPlaylistId ?? "") ||
                         _broadcasterInstance.DatabaseId != songRequestSetting.BroadcasterId))
                    {
                        await _songRequestSetting.UpdateSongRequestSettingAsync
                        (
                            _botConfig.YouTubeBroadcasterPlaylistId,
                            _botConfig.YouTubePersonalPlaylistId,
                            _broadcasterInstance.DatabaseId,
                            songRequestSetting.DjMode
                        );
                    }
                    else if (songRequestSetting?.Id == 0)
                    {
                        await _songRequestSetting.CreateSongRequestSettingAsync
                        (
                            _botConfig.YouTubeBroadcasterPlaylistId,
                            _botConfig.YouTubePersonalPlaylistId,
                            _broadcasterInstance.DatabaseId
                        );
                    }

                    // Write to a text file to allow users to show the currently playing song as a song ticker
                    // ToDo: Add config variables
                    string filename = "Twitch Chat Bot Song Request.txt";
                    string docPath  = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

                    using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, filename)))
                    {
                        await outputFile.WriteAsync(""); // clear old song request or create new file with empty string
                    }
                }
            }
            catch (Exception ex)
            {
                _youTubeClientInstance.HasCredentials = false; // do not allow any YouTube features for this bot until error has been resolved
                await _errHndlrInstance.LogError(ex, "TwitchBotApplication", "GetYouTubeAuth()", false);
            }
        }