Exemplo n.º 1
0
        private void InitClientService()
        {
            ClientServiceTypes clientServiceType = ClientServiceTypes.Terminal;

            using (BotDBContext dbContext = DatabaseManager.OpenContext())
            {
                Settings setting = dbContext.SettingCollection.FirstOrDefault((set) => set.Key == SettingsConstants.CLIENT_SERVICE_TYPE);

                if (setting != null)
                {
                    clientServiceType = (ClientServiceTypes)setting.ValueInt;
                }
                else
                {
                    TRBotLogger.Logger.Warning($"Database does not contain the {SettingsConstants.CLIENT_SERVICE_TYPE} setting. It will default to {clientServiceType}.");
                }
            }

            TRBotLogger.Logger.Information($"Setting up client service: {clientServiceType}");

            if (clientServiceType == ClientServiceTypes.Terminal)
            {
                ClientService = new TerminalClientService(DataConstants.COMMAND_IDENTIFIER);

                MsgHandler.SetLogToLogger(false);
            }
            else if (clientServiceType == ClientServiceTypes.Twitch)
            {
                TwitchLoginSettings twitchSettings = ConnectionHelper.ValidateTwitchCredentialsPresent(DataConstants.DataFolderPath,
                                                                                                       TwitchConstants.LOGIN_SETTINGS_FILENAME);

                //If either of these fields are empty, the data is invalid
                if (string.IsNullOrEmpty(twitchSettings.ChannelName) || string.IsNullOrEmpty(twitchSettings.Password) ||
                    string.IsNullOrEmpty(twitchSettings.BotName))
                {
                    TRBotLogger.Logger.Error($"Twitch login settings are invalid. Please modify the data in the \"{TwitchConstants.LOGIN_SETTINGS_FILENAME}\" file.");
                    return;
                }

                TwitchClient          client      = new TwitchClient();
                ConnectionCredentials credentials = ConnectionHelper.MakeCredentialsFromTwitchLogin(twitchSettings);
                ClientService = new TwitchClientService(credentials, twitchSettings.ChannelName, DataConstants.COMMAND_IDENTIFIER, DataConstants.COMMAND_IDENTIFIER, true);
            }

            //Initialize service
            ClientService?.Initialize();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validates if the file for Twitch credentials exists and reads it if so.
        /// If it does not exist, it creates it and returns a newly initialized object.
        /// </summary>
        /// <returns>A TwitchLoginSettings object.</returns>
        public static TwitchLoginSettings ValidateTwitchCredentialsPresent(string dataFolderPath, string loginSettingsFilename)
        {
            TwitchLoginSettings loginSettings = null;

            //Read from the file and create it if it doesn't exist
            string text = FileHelpers.ReadFromTextFileOrCreate(dataFolderPath, loginSettingsFilename);

            if (string.IsNullOrEmpty(text) == true)
            {
                //Create default settings and save to the file
                loginSettings = new TwitchLoginSettings();
                string loginText = JsonConvert.SerializeObject(loginSettings, Formatting.Indented);
                FileHelpers.SaveToTextFile(dataFolderPath, loginSettingsFilename, loginText);
            }
            else
            {
                //Read from the file
                loginSettings = JsonConvert.DeserializeObject <TwitchLoginSettings>(text);
            }

            return(loginSettings);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates connection credentials based on twitch login settings.
 /// </summary>
 /// <returns>A ConnectionCredentials object.</returns>
 public static ConnectionCredentials MakeCredentialsFromTwitchLogin(TwitchLoginSettings twitchLoginSettings)
 {
     return(new ConnectionCredentials(twitchLoginSettings.BotName, twitchLoginSettings.Password));
 }