using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; namespace TwitchEventSub.Types.EventSubSubscription { public enum SubscribableTypes { channel_update, //aka Channel Update, version 1. A broadcaster updates their channel properties e.g., category, title, mature flag, broadcast, or language. channel_follow, //aka Channel Follow, version 1. A specified channel receives a follow. channel_subscribe, //aka Channel Subscribe, version 1. A notification when a specified channel receives a subscriber. This does not include resubscribes. channel_subscription_end, //aka Channel Subscription End, version 1. A notification when a subscription to the specified channel ends. channel_subscription_gift, //aka Channel Subscription Gift, version 1. A notification when a viewer gives a gift subscription to one or more users in the specified channel. channel_subscription_message, //aka Channel Subscription Message, version 1. A notification when a user sends a resubscription chat message in a specific channel. channel_cheer, //aka Channel Cheer, version 1. A user cheers on the specified channel. channel_raid, //aka Channel Raid, version 1. A broadcaster raids another broadcaster’s channel. channel_ban, //aka Channel Ban, version 1. A viewer is banned from the specified channel. channel_unban, //aka Channel Unban, version 1. A viewer is unbanned from the specified channel. channel_moderator_add, //aka Channel Moderator Add, version 1. Moderator privileges were added to a user on a specified channel. channel_moderator_remove, //aka Channel Moderator Remove, version 1. Moderator privileges were removed from a user on a specified channel. channel_channel_points_custom_reward_add, //aka Channel Points Custom Reward Add, version 1. A custom channel points reward has been created for the specified channel. channel_channel_points_custom_reward_update, //aka Channel Points Custom Reward Update, version 1. A custom channel points reward has been updated for the specified channel. channel_channel_points_custom_reward_remove, //aka Channel Points Custom Reward Remove, version 1. A custom channel points reward has been removed from the specified channel. channel_channel_points_custom_reward_redemption_add, //aka Channel Points Custom Reward Redemption Add, version 1. A viewer has redeemed a custom channel points reward on the specified channel. channel_channel_points_custom_reward_redemption_update, //aka Channel Points Custom Reward Redemption Update, version 1. A redemption of a channel points custom reward has been updated for the specified channel. channel_poll_begin, //aka Channel Poll Begin, version 1. A poll started on a specified channel. channel_poll_progress, //aka Channel Poll Progress, version 1. Users respond to a poll on a specified channel. channel_poll_end, //aka Channel Poll End, version 1. A poll ended on a specified channel. channel_prediction_begin, //aka Channel Prediction Begin, version 1. A Prediction started on a specified channel. channel_prediction_progress, //aka Channel Prediction Progress, version 1. Users participated in a Prediction on a specified channel. channel_prediction_lock, //aka Channel Prediction Lock, version 1. A Prediction was locked on a specified channel. channel_prediction_end, //aka Channel Prediction End, version 1. A Prediction ended on a specified channel. drop_entitlement_grant, //aka Drop Entitlement Grant, version 1. An entitlement for a Drop is granted to a user. extension_bits_transaction_create, //aka Extension Bits Transaction Create, version 1. A Bits transaction occurred for a specified Twitch Extension. channel_goal_begin, //aka Goal Begin, version 1. Get notified when a broadcaster begins a goal. channel_goal_progress, //aka Goal Progress, version 1. Get notified when progress (either positive or negative) is made towards a broadcaster’s goal. channel_goal_end, //aka Goal End, version 1. Get notified when a broadcaster ends a goal. channel_hype_train_begin, //aka Hype Train Begin, version 1. A Hype Train begins on the specified channel. channel_hype_train_progress, //aka Hype Train Progress, version 1. A Hype Train makes progress on the specified channel. channel_hype_train_end, //aka Hype Train End, version 1. A Hype Train ends on the specified channel. stream_online, //aka Stream Online, version 1. The specified broadcaster starts a stream. stream_offline, //aka Stream Offline, version 1. The specified broadcaster stops a stream. user_authorization_grant, //aka User Authorization Grant, version 1. A user’s authorization has been granted to your client id. user_authorization_revoke, //aka User Authorization Revoke, version 1. A user’s authorization has been revoked for your client id. user_update //aka User Update, version 1. A user has updated their account. } internal class SubscribableTypesTranslation { public static string Enum2String(SubscribableTypes enumedType) { return table.Keys.FirstOrDefault(k => table[k] == enumedType); } public static SubscribableTypes String2Enum(string stringedType) { return table[stringedType]; } public static Twitchogram RefineTwitchogram(string raw) { var urT = JsonConvert.DeserializeObject(raw); var refinedSubscription = new Subscription() { id = urT.subscription.id, type = urT.subscription.type, version = urT.subscription.version, status = urT.subscription.status, cost = urT.subscription.cost, created_at = urT.subscription.created_at }; var refinedTuple = TypedPieces(urT.subscription.type); refinedSubscription.condition = refinedTuple.Item1; var refinedTwitchogram = new Twitchogram() { challenge = urT.challenge, subscription = refinedSubscription, Event = refinedTuple.Item2 }; JsonConvert.PopulateObject(raw, refinedTwitchogram); return refinedTwitchogram; } public static TwitchResponse RefineTwitchResponse(string raw) { var refinedResponse = JsonConvert.DeserializeObject(raw); foreach(var d in refinedResponse.data) { d.condition = TypedCondition(d.type); } JsonConvert.PopulateObject(raw, refinedResponse); return refinedResponse; } public static Event TypedEvent(string stringType) { return TypedPieces(stringType).Item2; } public static Conditions.Condition TypedCondition(string stringType) { return TypedPieces(stringType).Item1; } public static Tuple TypedPieces(string stringType) { switch (String2Enum(stringType)) { case SubscribableTypes.channel_update: return new Tuple( new Conditions.ChannelUpdate(), new Types.Offline.ChannelUpdate()); case SubscribableTypes.channel_follow: return new Tuple( new Conditions.ChannelFollow(), new Types.Offline.ChannelFollow()); case SubscribableTypes.channel_subscribe: return new Tuple( new Conditions.ChannelSubscribe(), new Types.ChannelSubscription.ChannelSubscribe()); case SubscribableTypes.channel_subscription_end: return new Tuple( new Conditions.ChannelSubscriptionEnd(), new Types.ChannelSubscription.ChannelSubscriptionEnd()); case SubscribableTypes.channel_subscription_gift: return new Tuple( new Conditions.ChannelSubscriptionGift(), new Types.ChannelSubscription.ChannelSubscriptionGift()); case SubscribableTypes.channel_subscription_message: return new Tuple( new Conditions.ChannelSubscriptionMessage(), new Types.ChannelSubscription.ChannelSubscriptionMessage()); case SubscribableTypes.channel_cheer: return new Tuple( new Conditions.ChannelCheer(), new Types.ChannelCheer()); case SubscribableTypes.channel_raid: return new Tuple( new Conditions.ChannelRaid(), new Types.ChannelRaid()); case SubscribableTypes.channel_ban: return new Tuple( new Conditions.ChannelBan(), new Types.Moderation.ChannelBan()); case SubscribableTypes.channel_unban: return new Tuple( new Conditions.ChannelUnban(), new Types.Moderation.ChannelUnban()); case SubscribableTypes.channel_moderator_add: return new Tuple( new Conditions.ChannelModeratorAdd(), new Types.Moderation.ChannelModeratorAdd()); case SubscribableTypes.channel_moderator_remove: return new Tuple( new Conditions.ChannelModeratorRemove(), new Types.Moderation.ChannelModeratorRemove()); case SubscribableTypes.channel_channel_points_custom_reward_add: return new Tuple( new Conditions.ChannelPointsCustomRewardAdd(), new Types.ChannelPoints.ChannelPointsCustomRewardAdd()); case SubscribableTypes.channel_channel_points_custom_reward_update: return new Tuple( new Conditions.ChannelPointsCustomRewardUpdate(), new Types.ChannelPoints.ChannelPointsCustomRewardUpdate()); case SubscribableTypes.channel_channel_points_custom_reward_remove: return new Tuple( new Conditions.ChannelPointsCustomRewardRemove(), new Types.ChannelPoints.ChannelPointsCustomRewardRemove()); case SubscribableTypes.channel_channel_points_custom_reward_redemption_add: return new Tuple( new Conditions.ChannelPointsCustomRewardRedemptionAdd(), new Types.ChannelPoints.ChannelPointsCustomRewardRedemptionAdd()); case SubscribableTypes.channel_channel_points_custom_reward_redemption_update: return new Tuple( new Conditions.ChannelPointsCustomRewardRedemptionUpdate(), new Types.ChannelPoints.ChannelPointsCustomRewardRedemptionUpdate()); case SubscribableTypes.channel_poll_begin: return new Tuple( new Conditions.ChannelPollBegin(), new Types.Poll.ChannelPollBegin()); case SubscribableTypes.channel_poll_progress: return new Tuple( new Conditions.ChannelPollProgress(), new Types.Poll.ChannelPollProgress()); case SubscribableTypes.channel_poll_end: return new Tuple( new Conditions.ChannelPollEnd(), new Types.Poll.ChannelPollEnd()); case SubscribableTypes.channel_prediction_begin: return new Tuple( new Conditions.ChannelPollBegin(), new Types.Poll.ChannelPollBegin()); case SubscribableTypes.channel_prediction_progress: return new Tuple( new Conditions.ChannelPollProgress(), new Types.Poll.ChannelPollProgress()); case SubscribableTypes.channel_prediction_lock: return new Tuple( new Conditions.ChannelPredictionLock(), new Types.Prediction.ChannelPredictionLock()); case SubscribableTypes.channel_prediction_end: return new Tuple( new Conditions.ChannelPredictionEnd(), new Types.Prediction.ChannelPredictionEnd()); case SubscribableTypes.drop_entitlement_grant: return new Tuple( new Conditions.DropEntitlementGrant(), new Types.DropEntitlementGrant()); case SubscribableTypes.extension_bits_transaction_create: return new Tuple( new Conditions.ExtensionBitsTransactionCreate(), new Types.ExtensionBitsTransactionCreate()); case SubscribableTypes.channel_goal_begin: case SubscribableTypes.channel_goal_progress: case SubscribableTypes.channel_goal_end: return new Tuple( new Conditions.Goals(), new Types.Goals()); case SubscribableTypes.channel_hype_train_begin: return new Tuple( new Conditions.HypeTrainBegin(), new Types.HypeTrain.HypeTrainBegin()); case SubscribableTypes.channel_hype_train_progress: return new Tuple( new Conditions.HypeTrainProgress(), new Types.HypeTrain.HypeTrainProgress()); case SubscribableTypes.channel_hype_train_end: return new Tuple( new Conditions.HypeTrainEnd(), new Types.HypeTrain.HypeTrainEnd()); case SubscribableTypes.stream_online: return new Tuple( new Conditions.StreamOnline(), new Types.StreamOnline()); case SubscribableTypes.stream_offline: return new Tuple( new Conditions.StreamOffline(), new Types.StreamOffline()); case SubscribableTypes.user_authorization_grant: return new Tuple( new Conditions.UserAuthorizationGrant(), new Types.Offline.UserAuthorizationGrant()); case SubscribableTypes.user_authorization_revoke: return new Tuple( new Conditions.UserAuthorizationRevoke(), new Types.Offline.UserAuthorizationRevoke()); case SubscribableTypes.user_update: return new Tuple( new Conditions.UserUpdate(), new Types.Offline.UserUpdate()); default: throw new ArgumentOutOfRangeException($"I've never heard of {stringType}. That's not real. I don't believe in that."); } } private static readonly Dictionary table = new Dictionary() { {"channel.update", SubscribableTypes.channel_update}, {"channel.follow", SubscribableTypes.channel_follow}, {"channel.subscribe", SubscribableTypes.channel_subscribe}, {"channel.subscription.end", SubscribableTypes.channel_subscription_end}, {"channel.subscription.gift", SubscribableTypes.channel_subscription_gift}, {"channel.subscription.message", SubscribableTypes.channel_subscription_message}, {"channel.cheer", SubscribableTypes.channel_cheer}, {"channel.raid", SubscribableTypes.channel_raid}, {"channel.ban", SubscribableTypes.channel_ban}, {"channel.unban", SubscribableTypes.channel_unban}, {"channel.moderator.add", SubscribableTypes.channel_moderator_add}, {"channel.moderator.remove", SubscribableTypes.channel_moderator_remove}, {"channel.channel.points.custom.reward.add", SubscribableTypes.channel_channel_points_custom_reward_add}, {"channel.channel.points.custom.reward.update", SubscribableTypes.channel_channel_points_custom_reward_update}, {"channel.channel.points.custom.reward.remove", SubscribableTypes.channel_channel_points_custom_reward_remove}, {"channel.channel.points.custom.reward.redemption.add", SubscribableTypes.channel_channel_points_custom_reward_redemption_add}, {"channel.channel.points.custom.reward.redemption.update", SubscribableTypes.channel_channel_points_custom_reward_redemption_update}, {"channel.poll.begin", SubscribableTypes.channel_poll_begin}, {"channel.poll.progress", SubscribableTypes.channel_poll_progress}, {"channel.poll.end", SubscribableTypes.channel_poll_end}, {"channel.prediction.begin", SubscribableTypes.channel_prediction_begin}, {"channel.prediction.progress", SubscribableTypes.channel_prediction_progress}, {"channel.prediction.lock", SubscribableTypes.channel_prediction_lock}, {"channel.prediction.end", SubscribableTypes.channel_prediction_end}, {"drop.entitlement.grant", SubscribableTypes.drop_entitlement_grant}, {"extension.bits.transaction.create", SubscribableTypes.extension_bits_transaction_create}, {"channel.goal.begin", SubscribableTypes.channel_goal_begin}, {"channel.goal.progress", SubscribableTypes.channel_goal_progress}, {"channel.goal.end", SubscribableTypes.channel_goal_end}, {"channel.hype.train.begin", SubscribableTypes.channel_hype_train_begin}, {"channel.hype.train.progress", SubscribableTypes.channel_hype_train_progress}, {"channel.hype.train.end", SubscribableTypes.channel_hype_train_end}, {"stream.online", SubscribableTypes.stream_online}, {"stream.offline", SubscribableTypes.stream_offline}, {"user.authorization.grant", SubscribableTypes.user_authorization_grant}, {"user.authorization.revoke", SubscribableTypes.user_authorization_revoke}, {"user.update", SubscribableTypes.user_update} }; } }