ArchiSteamFarm/IPC/Controllers/Api/BotController.cs
/// <summary>
///     Updates bot config of given bot.
/// </summary>
Updates bot config of given bot.
Updates bot config of given bot .
[Consumes("application/json")]
	[HttpPost("{botNames:required}")]
	[ProducesResponseType(typeof(GenericResponse<IReadOnlyDictionary<string, bool>>), (int) HttpStatusCode.OK)]
	[ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)]
	public async Task<ActionResult<GenericResponse>> BotPost(string botNames, [FromBody] BotRequest request) {
		if (string.IsNullOrEmpty(botNames)) {
			throw new ArgumentNullException(nameof(botNames));
		}
		ArgumentNullException.ThrowIfNull(request);
		if (Bot.Bots == null) {
			throw new InvalidOperationException(nameof(Bot.Bots));
		}
		(bool valid, string? errorMessage) = request.BotConfig.CheckValidation();
		if (!valid) {
			return BadRequest(new GenericResponse(false, errorMessage));
		}
		request.BotConfig.Saving = true;
		HashSet<string> bots = botNames.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet(Bot.BotsComparer);
		if (bots.Any(static botName => !ASF.IsValidBotName(botName))) {
			return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(botNames))));
		}
		Dictionary<string, bool> result = new(bots.Count, Bot.BotsComparer);
		foreach (string botName in bots) {
			if (Bot.Bots.TryGetValue(botName, out Bot? bot)) {
				if (!request.BotConfig.IsSteamLoginSet && bot.BotConfig.IsSteamLoginSet) {
					request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin;
				}
				if (!request.BotConfig.IsSteamPasswordSet && bot.BotConfig.IsSteamPasswordSet) {
					request.BotConfig.SetDecryptedSteamPassword(await bot.BotConfig.GetDecryptedSteamPassword().ConfigureAwait(false));
				}
				if (!request.BotConfig.IsSteamParentalCodeSet && bot.BotConfig.IsSteamParentalCodeSet) {
					request.BotConfig.SteamParentalCode = bot.BotConfig.SteamParentalCode;
				}
				if (bot.BotConfig.AdditionalProperties?.Count > 0) {
					request.BotConfig.AdditionalProperties ??= new Dictionary<string, JToken>(bot.BotConfig.AdditionalProperties.Count, bot.BotConfig.AdditionalProperties.Comparer);
					foreach ((string key, JToken value) in bot.BotConfig.AdditionalProperties.Where(property => !request.BotConfig.AdditionalProperties.ContainsKey(property.Key))) {
						request.BotConfig.AdditionalProperties.Add(key, value);
					}
					request.BotConfig.AdditionalProperties.TrimExcess();
				}
			}
			string filePath = Bot.GetFilePath(botName, Bot.EFileType.Config);
			if (string.IsNullOrEmpty(filePath)) {
				ASF.ArchiLogger.LogNullError(filePath);
				return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(filePath))));
			}
			result[botName] = await BotConfig.Write(filePath, request.BotConfig).ConfigureAwait(false);
		}
		return Ok(new GenericResponse<IReadOnlyDictionary<string, bool>>(result.Values.All(static value => value), result));
	}
[ Consumes ( " " ) ] [ HttpPost ( " " ) ] [ ProducesResponseType ( typeof ( GenericResponse < IReadOnlyDictionary < string , bool > > ) , ( int ) HttpStatusCode . OK ) ] [ ProducesResponseType ( typeof ( GenericResponse ) , ( int ) HttpStatusCode . BadRequest ) ] public async Task < ActionResult < GenericResponse > > BotPost ( string botNames , [ FromBody ] BotRequest request ) { if ( string . IsNullOrEmpty ( botNames ) ) { throw new ArgumentNullException ( nameof ( botNames ) ) ; } ArgumentNullException . ThrowIfNull ( request ) ; if ( Bot . Bots == null ) { throw new InvalidOperationException ( nameof ( Bot . Bots ) ) ; } ( bool valid , string ? errorMessage ) = request . BotConfig . CheckValidation ( ) ; if ( ! valid ) { return BadRequest ( new GenericResponse ( false , errorMessage ) ) ; } request . BotConfig . Saving = true ; HashSet < string > bots = botNames . Split ( new [ ] { ' ' } , StringSplitOptions . RemoveEmptyEntries ) . ToHashSet ( Bot . BotsComparer ) ; if ( bots . Any ( static botName => ! ASF . IsValidBotName ( botName ) ) ) { return BadRequest ( new GenericResponse ( false , string . Format ( CultureInfo . CurrentCulture , Strings . ErrorIsInvalid , nameof ( botNames ) ) ) ) ; } Dictionary < string , bool > result = new ( bots . Count , Bot . BotsComparer ) ; foreach ( string botName in bots ) { if ( Bot . Bots . TryGetValue ( botName , out Bot ? bot ) ) { if ( ! request . BotConfig . IsSteamLoginSet && bot . BotConfig . IsSteamLoginSet ) { request . BotConfig . SteamLogin = bot . BotConfig . SteamLogin ; } if ( ! request . BotConfig . IsSteamPasswordSet && bot . BotConfig . IsSteamPasswordSet ) { request . BotConfig . SetDecryptedSteamPassword ( await bot . BotConfig . GetDecryptedSteamPassword ( ) . ConfigureAwait ( false ) ) ; } if ( ! request . BotConfig . IsSteamParentalCodeSet && bot . BotConfig . IsSteamParentalCodeSet ) { request . BotConfig . SteamParentalCode = bot . BotConfig . SteamParentalCode ; } if ( bot . BotConfig . AdditionalProperties ? . Count > 0 ) { request . BotConfig . AdditionalProperties ??= new Dictionary < string , JToken > ( bot . BotConfig . AdditionalProperties . Count , bot . BotConfig . AdditionalProperties . Comparer ) ; foreach ( ( string key , JToken value ) in bot . BotConfig . AdditionalProperties . Where ( property => ! request . BotConfig . AdditionalProperties . ContainsKey ( property . Key ) ) ) { request . BotConfig . AdditionalProperties . Add ( key , value ) ; } request . BotConfig . AdditionalProperties . TrimExcess ( ) ; } } string filePath = Bot . GetFilePath ( botName , Bot . EFileType . Config ) ; if ( string . IsNullOrEmpty ( filePath ) ) { ASF . ArchiLogger . LogNullError ( filePath ) ; return BadRequest ( new GenericResponse ( false , string . Format ( CultureInfo . CurrentCulture , Strings . ErrorIsInvalid , nameof ( filePath ) ) ) ) ; } result [ botName ] = await BotConfig . Write ( filePath , request . BotConfig ) . ConfigureAwait ( false ) ; } return Ok ( new GenericResponse < IReadOnlyDictionary < string , bool > > ( result . Values . All ( static value => value ) , result ) ) ; }
Updates bot config of given bot.
Updates bot config of given bot .
botNames
string
BotRequest
[FromBody]