private async void PubSub_OnSubscribedReceived(object sender, PubSubSubscriptionsEventModel packet) { UserViewModel user = ChannelSession.Services.User.GetActiveUserByPlatformID(StreamingPlatformTypeEnum.Twitch, packet.user_id); if (user == null) { user = await UserViewModel.Create(packet); } if (packet.IsSubscription || packet.cumulative_months == 1) { await this.AddSub(new TwitchSubEventModel(user, packet)); } else { int months = Math.Max(packet.streak_months, packet.cumulative_months); string planTier = TwitchEventService.GetSubTierNameFromText(packet.sub_plan); CommandParametersModel parameters = new CommandParametersModel(user); if (ChannelSession.Services.Events.CanPerformEvent(EventTypeEnum.TwitchChannelResubscribed, parameters)) { string message = (packet.sub_message.ContainsKey("message") && packet.sub_message["message"] != null) ? packet.sub_message["message"].ToString() : string.Empty; parameters.Arguments = new List <string>(message.Split(new char[] { ' ' })); parameters.SpecialIdentifiers["message"] = message; parameters.SpecialIdentifiers["usersubmonths"] = months.ToString(); parameters.SpecialIdentifiers["usersubplanname"] = !string.IsNullOrEmpty(packet.sub_plan_name) ? packet.sub_plan_name : TwitchEventService.GetSubTierNameFromText(packet.sub_plan); parameters.SpecialIdentifiers["usersubplan"] = planTier; parameters.SpecialIdentifiers["usersubstreak"] = packet.streak_months.ToString(); ChannelSession.Settings.LatestSpecialIdentifiersData[SpecialIdentifierStringBuilder.LatestSubscriberUserData] = user.ID; ChannelSession.Settings.LatestSpecialIdentifiersData[SpecialIdentifierStringBuilder.LatestSubscriberSubMonthsData] = months; user.SubscribeDate = DateTimeOffset.Now.SubtractMonths(months - 1); user.Data.TwitchSubscriberTier = TwitchEventService.GetSubTierNumberFromText(packet.sub_plan); user.Data.TotalMonthsSubbed++; foreach (CurrencyModel currency in ChannelSession.Settings.Currency.Values) { currency.AddAmount(user.Data, currency.OnSubscribeBonus); } foreach (StreamPassModel streamPass in ChannelSession.Settings.StreamPass.Values) { if (parameters.User.HasPermissionsTo(streamPass.Permission)) { streamPass.AddAmount(user.Data, streamPass.SubscribeBonus); } } if (string.IsNullOrEmpty(await ChannelSession.Services.Moderation.ShouldTextBeModerated(user, message))) { await ChannelSession.Services.Events.PerformEvent(EventTypeEnum.TwitchChannelResubscribed, parameters); } } GlobalEvents.ResubscribeOccurred(new Tuple <UserViewModel, int>(user, months)); await ChannelSession.Services.Alerts.AddAlert(new AlertChatMessageViewModel(StreamingPlatformTypeEnum.Twitch, user, string.Format("{0} Re-Subscribed For {1} Months at {2}", user.FullDisplayName, months, planTier), ChannelSession.Settings.AlertSubColor)); } }
public UserViewModel(PubSubSubscriptionsEventModel packet) { this.SetUserData(twitchID: packet.user_id); this.TwitchID = packet.user_id; this.TwitchUsername = packet.user_name; this.TwitchDisplayName = (!string.IsNullOrEmpty(packet.display_name)) ? packet.display_name : this.TwitchUsername; this.SetTwitchRoles(); }
public TwitchSubEventModel(UserViewModel user, PubSubSubscriptionsEventModel packet) { this.User = user; this.PlanTier = TwitchEventService.GetSubTierNameFromText(packet.sub_plan); this.PlanName = !string.IsNullOrEmpty(packet.sub_plan_name) ? packet.sub_plan_name : TwitchEventService.GetSubTierNameFromText(packet.sub_plan); if (packet.sub_message.ContainsKey("message")) { this.Message = packet.sub_message["message"].ToString(); } }
public static async Task <UserViewModel> Create(PubSubSubscriptionsEventModel packet) { UserViewModel user = await UserViewModel.Create(StreamingPlatformTypeEnum.Twitch, packet.user_id); user.TwitchID = packet.user_id; user.TwitchUsername = packet.user_name; user.TwitchDisplayName = (!string.IsNullOrEmpty(packet.display_name)) ? packet.display_name : user.TwitchUsername; user.SetTwitchRoles(); ChannelSession.Settings.SetUserData(user.Data); return(user); }
private async void PubSub_OnSubscribedReceived(object sender, PubSubSubscriptionsEventModel packet) { UserViewModel user = ChannelSession.Services.User.GetUserByTwitchID(packet.user_id); if (user == null) { user = new UserViewModel(packet); } if (packet.IsSubscription || packet.cumulative_months == 1) { await this.AddSub(new TwitchSubEventModel(user, packet)); } else { int months = Math.Max(packet.streak_months, packet.cumulative_months); string planTier = TwitchEventService.GetSubTierNameFromText(packet.sub_plan); EventTrigger trigger = new EventTrigger(EventTypeEnum.TwitchChannelResubscribed, user); if (ChannelSession.Services.Events.CanPerformEvent(trigger)) { trigger.SpecialIdentifiers["message"] = (packet.sub_message.ContainsKey("message")) ? packet.sub_message["message"].ToString() : string.Empty; trigger.SpecialIdentifiers["usersubmonths"] = months.ToString(); trigger.SpecialIdentifiers["usersubplanname"] = !string.IsNullOrEmpty(packet.sub_plan_name) ? packet.sub_plan_name : TwitchEventService.GetSubTierNameFromText(packet.sub_plan); trigger.SpecialIdentifiers["usersubplan"] = planTier; trigger.SpecialIdentifiers["usersubstreak"] = packet.streak_months.ToString(); ChannelSession.Settings.LatestSpecialIdentifiersData[SpecialIdentifierStringBuilder.LatestSubscriberUserData] = user.ID; ChannelSession.Settings.LatestSpecialIdentifiersData[SpecialIdentifierStringBuilder.LatestSubscriberSubMonthsData] = months; user.Data.TwitchSubscribeDate = DateTimeOffset.Now.SubtractMonths(months - 1); foreach (CurrencyModel currency in ChannelSession.Settings.Currency.Values) { currency.AddAmount(user.Data, currency.OnSubscribeBonus); } user.Data.TotalMonthsSubbed++; await ChannelSession.Services.Events.PerformEvent(trigger); } GlobalEvents.ResubscribeOccurred(new Tuple <UserViewModel, int>(user, months)); await ChannelSession.Services.Alerts.AddAlert(new AlertChatMessageViewModel(StreamingPlatformTypeEnum.Twitch, user, string.Format("{0} Re-Subscribed For {1} Months at {2}", user.Username, months, planTier), ChannelSession.Settings.AlertSubColor)); } }
/// <summary> /// Processes the received text packet. /// </summary> /// <param name="packetText">The receive text packet</param> /// <returns>An awaitable task</returns> protected override Task ProcessReceivedPacket(string packetText) { Logger.Log(LogLevel.Debug, "Twitch PubSub Packet Received: " + packetText); PubSubPacketModel packet = JSONSerializerHelper.DeserializeFromString <PubSubPacketModel>(packetText); if (packet != null) { switch (packet.type) { case "RECONNECT": this.OnReconnectReceived?.Invoke(this, new EventArgs()); break; case "RESPONSE": this.OnResponseReceived?.Invoke(this, JSONSerializerHelper.DeserializeFromString <PubSubResponsePacketModel>(packetText)); break; case "MESSAGE": PubSubMessagePacketModel messagePacket = JSONSerializerHelper.DeserializeFromString <PubSubMessagePacketModel>(packetText); this.OnMessageReceived?.Invoke(this, messagePacket); try { PubSubMessagePacketDataModel messageData = messagePacket.messageData; if (messageData != null) { if (messagePacket.topicType == PubSubTopicsEnum.UserWhispers) { this.OnWhisperReceived?.Invoke(this, messageData.data_object.ToObject <PubSubWhisperEventModel>()); } else if (messagePacket.topicType == PubSubTopicsEnum.ChannelBitsEventsV1) { this.OnBitsV1Received?.Invoke(this, messageData.data_object.ToObject <PubSubBitsEventV1Model>()); } else if (messagePacket.topicType == PubSubTopicsEnum.ChannelBitsEventsV2) { this.OnBitsV2Received?.Invoke(this, messageData.data_object.ToObject <PubSubBitsEventV2Model>()); } else if (messagePacket.topicType == PubSubTopicsEnum.ChannelBitsBadgeUnlocks) { this.OnBitsBadgeReceived?.Invoke(this, messageData.data_object.ToObject <PubSubBitBadgeEventModel>()); } else if (messagePacket.topicType == PubSubTopicsEnum.ChannelSubscriptionsV1) { PubSubSubscriptionsEventModel subscription = JSONSerializerHelper.DeserializeFromString <PubSubSubscriptionsEventModel>(messagePacket.message); if (subscription.IsGiftedSubscription || subscription.IsAnonymousGiftedSubscription) { this.OnSubscriptionsGiftedReceived?.Invoke(this, JSONSerializerHelper.DeserializeFromString <PubSubSubscriptionsGiftEventModel>(messagePacket.message)); } else { this.OnSubscribedReceived?.Invoke(this, subscription); } } else if (messagePacket.topicType == PubSubTopicsEnum.ChannelPointsRedeemed) { this.OnChannelPointsRedeemed?.Invoke(this, messageData.data_object.ToObject <PubSubChannelPointsRedemptionEventModel>()); } } } catch (Exception ex) { Logger.Log(ex); } break; case "PONG": this.OnPongReceived?.Invoke(this, new EventArgs()); break; } } return(Task.FromResult(0)); }