Exemplo n.º 1
0
        public async Task <HttpResponseMessage> Create([FromBody] PredictionDto prediction)
        {
            var request = new CreatePredictionRequest
            {
                Prediction = prediction
            };
            await _mediator.ExecuteAsync(request).ConfigureAwait(false);

            return(Request.CreateResponse(HttpStatusCode.Created));
        }
        public void create(userEntry commandUser, string argumentString)
        {
            string[] argArray = argumentString.Split('/');
            if (argArray.Length >= 5)
            {
                m_BotBrain.sendDefaultChannelMessage(string.Format(m_BotBrain.localizer.getString("predictionCreateFail"), m_BotBrain.localizer.getString("predictionCreateFailOutcomeCount")));
            }
            else if (argArray.Length == 4)
            {
                string failReasons = "";

                int  duration;
                bool durationValid = false;
                if (Int32.TryParse(argArray[0], out duration) && duration >= 1 && duration <= 1800)
                {
                    durationValid = true;
                }
                else
                {
                    failReasons += m_BotBrain.localizer.getString("predictionCreateFailArgCount");
                }

                string title      = argArray[1];
                bool   titleValid = false;

                if (!string.IsNullOrEmpty(title) && title.Length > 1 && title.Length <= 45)
                {
                    titleValid = true;
                }
                else
                {
                    failReasons += m_BotBrain.localizer.getString("predictionCreateFailTitleLength");
                }

                string outcome1      = argArray[2];
                bool   outcome1Valid = false;

                if (isValidOutcome(outcome1))
                {
                    outcome1Valid = true;
                }
                else
                {
                    failReasons += string.Format(m_BotBrain.localizer.getString("predictionCreateFailOutcomeInvalid"), 1);
                }

                string outcome2      = argArray[3];
                bool   outcome2Valid = false;

                if (isValidOutcome(outcome2))
                {
                    outcome2Valid = true;
                }
                else
                {
                    failReasons += string.Format(m_BotBrain.localizer.getString("predictionCreateFailOutcomeInvalid"), 2);
                }

                if (durationValid && titleValid && outcome1Valid && outcome2Valid)
                {
                    try
                    {
                        TwitchLib.Api.Helix.Models.Predictions.CreatePrediction.Outcome outcome1Object = new TwitchLib.Api.Helix.Models.Predictions.CreatePrediction.Outcome()
                        {
                            Title = outcome1
                        };
                        TwitchLib.Api.Helix.Models.Predictions.CreatePrediction.Outcome outcome2Object = new TwitchLib.Api.Helix.Models.Predictions.CreatePrediction.Outcome()
                        {
                            Title = outcome2
                        };
                        TwitchLib.Api.Helix.Models.Predictions.CreatePrediction.Outcome[] outcomeList = { outcome1Object, outcome2Object };

                        CreatePredictionRequest newPredictionRequest = new CreatePredictionRequest()
                        {
                            BroadcasterId           = m_BotBrain.ownerID,
                            Title                   = title,
                            PredictionWindowSeconds = duration,
                            Outcomes                = outcomeList
                        };

                        Task <CreatePredictionResponse> createTask = Task.Run(() => m_BotBrain.twitchAPI.Helix.Predictions.CreatePredictionAsync(newPredictionRequest));
                        createTask.Wait();

                        m_BotBrain.sendDefaultChannelMessage(m_BotBrain.localizer.getString("predictionCreateSuccess"));
                    }
                    catch (Exception e)
                    {
                        m_BotBrain.sendDefaultChannelMessage(string.Format(m_BotBrain.localizer.getString("predictionCreateFail"), m_BotBrain.localizer.getString("predictionCreateFailUnknown")));
                    }
                }
                else
                {
                    m_BotBrain.sendDefaultChannelMessage(string.Format(m_BotBrain.localizer.getString("predictionCreateFail"), failReasons));
                }
            }
            else
            {
                m_BotBrain.sendDefaultChannelMessage(string.Format(m_BotBrain.localizer.getString("predictionCreateFail"), m_BotBrain.localizer.getString("predictionCreateFailArgCount")));
            }
        }
Exemplo n.º 3
0
 public Task <CreatePredictionResponse> CreatePredictionAsync(CreatePredictionRequest request, string accessToken = null)
 {
     return(TwitchPostGenericAsync <CreatePredictionResponse>("/predictions", ApiVersion.Helix, JsonConvert.SerializeObject(request), accessToken: accessToken));
 }