示例#1
0
        public HypeCommandsSettings EnsureDefaultHypeSettings(string userID)
        {
            var key = HypeCommandsSettings.GetKey(userID);
            HypeCommandsSettings hypeCommandsSettings;

            try
            {
                hypeCommandsSettings = this.redis.Get <HypeCommandsSettings>(key);
            }
            catch (System.Runtime.Serialization.SerializationException)
            {
                this.redis.Delete(key);
                hypeCommandsSettings = null;
            }

            if (hypeCommandsSettings == null)
            {
                hypeCommandsSettings = new HypeCommandsSettings
                {
                    userID       = userID,
                    hypeCommands = new List <Common.Models.HypeCommand>
                    {
                        new Common.Models.HypeCommand
                        {
                            accessLevel      = (int)KomaruBot.Common.Constants.AccessLevel.Public,
                            commandResponses = new List <Common.Models.HypeCommandResponse>
                            {
                                new Common.Models.HypeCommandResponse {
                                    message = "D e e R F o r C e",
                                }
                            },
                            commandText             = "!df",
                            numberOfResponses       = 1,
                            pointsCost              = 0,
                            randomizeResponseOrders = false,
                        },
                        new Common.Models.HypeCommand
                        {
                            accessLevel      = (int)KomaruBot.Common.Constants.AccessLevel.Public,
                            commandResponses = new List <Common.Models.HypeCommandResponse>
                            {
                                new Common.Models.HypeCommandResponse {
                                    message = "D e e R F o r C e",
                                }
                            },
                            commandText             = "!deerforce",
                            numberOfResponses       = 1,
                            pointsCost              = 0,
                            randomizeResponseOrders = false,
                        },
                    },
                };
                this.redis.Set(key, hypeCommandsSettings);
            }

            return(hypeCommandsSettings);
        }
示例#2
0
        public ActionResult Update(
            [FromBody]
            HypeCommandsSettings settings
            )
        {
            try
            {
                if (settings == null)
                {
                    return(StatusCode((int)System.Net.HttpStatusCode.BadRequest, $"{nameof(settings)} is null"));
                }
                if (string.IsNullOrWhiteSpace(settings.userID))
                {
                    return(StatusCode((int)System.Net.HttpStatusCode.BadRequest, $"{nameof(settings.userID)} is null"));
                }

                var userInfo = this.authenticationProvider.Authenticate(this.HttpContext);
                if (userInfo.Result != Constants.AuthenticationResult.Success)
                {
                    return(StatusCode((int)System.Net.HttpStatusCode.Unauthorized, $"Could not authenticate"));
                }
                if (this.appSettings.TwitchClientID != userInfo.GetClientID())
                {
                    return(StatusCode((int)System.Net.HttpStatusCode.Unauthorized, $"{nameof(this.appSettings.TwitchClientID)} does not match"));
                }
                if (settings.userID != userInfo.GetUserID())
                {
                    return(StatusCode((int)System.Net.HttpStatusCode.Unauthorized, $"{nameof(settings.userID)} is not yours"));
                }

                var validationResult = settings.validate();
                if (validationResult != System.ComponentModel.DataAnnotations.ValidationResult.Success)
                {
                    return(StatusCode((int)System.Net.HttpStatusCode.BadRequest, new { message = validationResult.ErrorMessage }));
                }

                this.userHelper.SaveSettings(settings);
                Startup.chatBotManager.UpdateConnection(userInfo.GetUserID(), settings.hypeCommands);

                return(Json(settings));
            }
            catch (Exception exc)
            {
                this._logger.LogError(ExceptionFormatter.FormatException(exc, $"Exception in {this.GetType().Name} - {System.Reflection.MethodBase.GetCurrentMethod().Name}"));
                return(StatusCode((int)System.Net.HttpStatusCode.InternalServerError));
            }
        }
示例#3
0
 public void SaveSettings(HypeCommandsSettings toSave)
 {
     this.redis.Set(toSave.GetKey(), toSave);
 }