public void CreateOrUpdate(Guid rewardId, string rewardTitle, string rewardDescription)
        {
            using (var context = _chatbotContextFactory.Create())
            {
                var reward = context.ChannelRewards.Find(rewardId);

                if (reward == null)
                {
                    reward = new ChannelReward
                    {
                        ChannelRewardId = rewardId
                    };

                    context.ChannelRewards.Add(reward);
                }

                reward.RewardTitle       = rewardTitle;
                reward.RewardDescription = rewardDescription;

                context.SaveChanges();
            }
        }
Exemple #2
0
        public async Task VerifyChannelRewardsAsync(MainForm mainForm, string soundredemptionid, string ttsredemptionid)
        {
            {
                int endTimer = 5;
                while ((BroadcasterID == null || BroadcasterID == "") && endTimer >= 0)
                {
                    await Task.Delay(1000);

                    endTimer--;
                }
            }

            if (BroadcasterID == null || BroadcasterID == "")
            {
                mainForm.ThreadSafeAddPreviewText("[ERROR] No broadcaster ID to verify Channel Rewards!", LineType.IrcCommand);
                return;
            }

            string response = await GetNewUpdateAsync("channel_points/custom_rewards", "?broadcaster_id=" + BroadcasterID, true);

            if (response == null || response == "")
            {
                mainForm.ThreadSafeAddPreviewText("[ERROR] Incorrect response when verifying Channel Rewards!", LineType.IrcCommand);
                return;
            }
            else
            {
                JObject jReader = JObject.Parse(response);
                if (jReader["data"] != null)
                {
                    ChannelReward soundredemptionReward = null;
                    ChannelReward ttsredemptionReward   = null;

                    var dataNode = jReader["data"];
                    foreach (var customReward in dataNode)
                    {
                        var parseNode = customReward.ToObject <ChannelReward>();
                        if (soundredemptionid != null && parseNode.id.Equals(soundredemptionid, StringComparison.InvariantCultureIgnoreCase))
                        {
                            soundredemptionReward = parseNode;
                        }
                        else if (ttsredemptionid != null && parseNode.id.Equals(ttsredemptionid, StringComparison.InvariantCultureIgnoreCase))
                        {
                            ttsredemptionReward = parseNode;
                        }
                    }

                    if (soundredemptionid != null && soundredemptionid != "")
                    {
                        if (soundredemptionReward == null)
                        {
                            mainForm.ThreadSafeAddPreviewText("[ERROR] Haven't found Channel Reward with Sound Reward ID!", LineType.IrcCommand);
                        }
                        else
                        {
                            if (!soundredemptionReward.is_enabled)
                            {
                                mainForm.ThreadSafeAddPreviewText("[WARNING] Sound reward is not enabled!", LineType.IrcCommand);
                            }
                            else if (soundredemptionReward.is_paused)
                            {
                                mainForm.ThreadSafeAddPreviewText("[WARNING] Sound reward redemption is paused!", LineType.IrcCommand);
                            }
                            else if (!soundredemptionReward.is_user_input_required)
                            {
                                mainForm.ThreadSafeAddPreviewText("[ERROR] Sound reward is incorrectly configured - it needs to require player input!", LineType.IrcCommand);
                            }
                            else if (soundredemptionReward.should_redemptions_skip_request_queue)
                            {
                                mainForm.ThreadSafeAddPreviewText("[WARNING] Sound redemption via points shouldn't skip request queue to allow for returning points, if request fails!", LineType.IrcCommand);
                            }
                        }
                    }

                    if (ttsredemptionid != null && ttsredemptionid != "")
                    {
                        if (ttsredemptionReward == null)
                        {
                            mainForm.ThreadSafeAddPreviewText("[ERROR] Haven't found Channel Reward with TTS Reward ID!", LineType.IrcCommand);
                        }
                        else
                        {
                            if (!ttsredemptionReward.is_enabled)
                            {
                                mainForm.ThreadSafeAddPreviewText("[WARNING] TTS reward is not enabled!", LineType.IrcCommand);
                            }
                            else if (ttsredemptionReward.is_paused)
                            {
                                mainForm.ThreadSafeAddPreviewText("[WARNING] TTS reward redemption is paused!", LineType.IrcCommand);
                            }
                            else if (!ttsredemptionReward.is_user_input_required)
                            {
                                mainForm.ThreadSafeAddPreviewText("[ERROR] TTS reward is incorrectly configured - it needs to require player input!", LineType.IrcCommand);
                            }
                            else if (ttsredemptionReward.should_redemptions_skip_request_queue)
                            {
                                mainForm.ThreadSafeAddPreviewText("[WARNING] TTS redemption via points shouldn't skip request queue to allow for returning points, if request fails!", LineType.IrcCommand);
                            }
                        }
                    }

                    //Clear up redemption queue
                    if (ttsredemptionReward != null || soundredemptionReward != null)
                    {
                        if (soundredemptionReward != null)
                        {
                            string pointsRewardResponse = await GetNewUpdateAsync("channel_points/custom_rewards/redemptions", "?broadcaster_id=" + BroadcasterID + "&reward_id=" + soundredemptionReward.id + "&status=UNFULFILLED", true, true);


                            //Should have thought this through...
                            if (pointsRewardResponse.StartsWith("Error: "))
                            {
                                mainForm.ThreadSafeAddPreviewText("[ERROR] Error verifying Sound award. It has ID, but bot doesn't have access to it. Maybe it was defined by a user instead of bot?", LineType.IrcCommand);
                                mainForm.ThreadSafeAddPreviewText("[ERROR] " + pointsRewardResponse, LineType.IrcCommand);
                            }
                            else
                            {
                                jReader  = JObject.Parse(pointsRewardResponse);
                                dataNode = jReader["data"];

                                mainForm.ThreadSafeAddPreviewText("[Verification Status] Sound reward seems OK." + (dataNode.Count() > 0 ? " Clearing up request queue!" : " Request queue empty."), LineType.IrcCommand);

                                int totalIDsToCancel = dataNode.Count() > 50 ? 50 : dataNode.Count();
                                if (totalIDsToCancel > 0)
                                {
                                    var idsToCancel = dataNode.Take(totalIDsToCancel).Select(x => x["id"].ToString()).ToArray();

                                    UpdateRedemptionStatus(mainForm, soundredemptionReward.id, idsToCancel, RedemptionStates.CANCELED);
                                }
                            }
                        }

                        if (ttsredemptionReward != null)
                        {
                            string pointsRewardResponse = await GetNewUpdateAsync("channel_points/custom_rewards/redemptions", "?broadcaster_id=" + BroadcasterID + "&reward_id=" + ttsredemptionReward.id + "&status=UNFULFILLED", true, true);

                            //Should have thought this through...
                            if (pointsRewardResponse.StartsWith("Error: "))
                            {
                                mainForm.ThreadSafeAddPreviewText("[ERROR] Error verifying TTS award. It has ID, but bot doesn't have access to it. Maybe it was defined by a user instead of bot?", LineType.IrcCommand);
                                mainForm.ThreadSafeAddPreviewText("[ERROR] " + pointsRewardResponse, LineType.IrcCommand);
                            }
                            else
                            {
                                jReader  = JObject.Parse(pointsRewardResponse);
                                dataNode = jReader["data"];

                                mainForm.ThreadSafeAddPreviewText("[Verification Status] TTS reward seems OK." + (dataNode.Count() > 0 ? " Clearing up request queue!" : " Request queue empty."), LineType.IrcCommand);

                                int totalIDsToCancel = dataNode.Count() > 50 ? 50 : dataNode.Count();
                                if (totalIDsToCancel > 0)
                                {
                                    var idsToCancel = dataNode.Take(totalIDsToCancel).Select(x => x["id"].ToString()).ToArray();

                                    UpdateRedemptionStatus(mainForm, ttsredemptionReward.id, idsToCancel, RedemptionStates.CANCELED);
                                }
                            }
                        }
                    }
                }
            }
        }