public async Task <SlashCommandResponse> Handle(SlashCommandContext context)
    {
        var existingSub = await _repo.GetGuildSubscription(context.GuildId, context.ChannelId);

        if (existingSub == null || !existingSub.Subscriptions.Any())
        {
            return(Respond($"🤷‍♀️O RLY?", $"Did not find any subscription(s) in this channel to remove!"));
        }

        EventSubscription eventSub = Enum.Parse <EventSubscription>(context.CommandInput.Value);

        bool isLastSub = existingSub.Subscriptions.Count() == 1 && existingSub.Subscriptions.First() == eventSub;

        if (existingSub.LeagueId == null && (isLastSub || eventSub == EventSubscription.All))
        {
            await _repo.DeleteGuildSubscription(context.GuildId, context.ChannelId);

            return(Respond($"✅ Success!", $"Removed subscription to this channel."));
        }
        bool existingIsAll = existingSub.Subscriptions.Count() == 1 && existingSub.Subscriptions.First() == EventSubscription.All;

        if (existingIsAll && eventSub != EventSubscription.All)
        {
            var allTypes = EventSubscriptionHelper.GetAllSubscriptionTypes().ToList();
            allTypes.Remove(EventSubscription.All);
            await _repo.UpdateGuildSubscription(existingSub with {
                Subscriptions = allTypes
            });

            var updatedFromAll = await _repo.GetGuildSubscription(context.GuildId, context.ChannelId);

            return(Respond($"✅ Success!", $"No longer subscribing to all events. Updated list:\n{Formatter.BulletPoints(updatedFromAll.Subscriptions)}"));
        }

        var updated = new List <EventSubscription>(existingSub.Subscriptions);

        if (eventSub == EventSubscription.All)
        {
            updated = new List <EventSubscription>();
        }
        else
        {
            updated.Remove(eventSub);
        }

        await _repo.UpdateGuildSubscription(existingSub with {
            Subscriptions = updated
        });

        var regularUpdate = await _repo.GetGuildSubscription(context.GuildId, context.ChannelId);

        if (regularUpdate.Subscriptions.Any())
        {
            return(Respond($"✅ Success!", $"Unsubscribed from {eventSub}. Updated list:\n{Formatter.BulletPoints(regularUpdate.Subscriptions)}"));
        }
        return(Respond($"✅ Success!", $"No longer subscribing to any events."));
    }
    public override (string, string) GetHelpDescription()
    {
        var sb = new StringBuilder();

        sb.Append("Update what notifications fplbot should post. (");

        sb.Append($"{string.Join(", ", EventSubscriptionHelper.GetAllSubscriptionTypes())})");

        return(
            "subscribe/unsubscribe {comma separated list of events}",
            sb.ToString());
    }
Example #3
0
        public override void Create(EventSubscription element)
        {
            if (element == null)
            {
                throw HttpResponseExceptionHelper.Create("Element you want to create is null (Event Subscription)",
                                                         HttpStatusCode.BadRequest);
            }

            using (var db = new SystemDbContext())
            {
                element.EventSubscriptionID = EventSubscriptionHelper.GetEventSubscriptionId(element);
                db.EventSubscriptions.Add(element);
                db.SaveChanges();
            }
        }
Example #4
0
        public void Unsubscribe(SubscribeModel unsubscribModel)
        {
            var subscription = Get(EventSubscriptionHelper.GetEventSubscriptionId(unsubscribModel));

            if (subscription == null)
            {
                throw HttpResponseExceptionHelper.Create("No subscription to unsubcribe from", HttpStatusCode.BadRequest);
            }

            // Delete Subscription
            Delete(new EventSubscription()
            {
                EventSubscriptionID = EventSubscriptionHelper.GetEventSubscriptionId(unsubscribModel)
            });
        }
    private IEnumerable <EventSubscription> UnsubscribeToEvents(
        IEnumerable <EventSubscription> inputSubscriptions,
        IEnumerable <EventSubscription> currentSubscriptions
        )
    {
        if (inputSubscriptions.Contains(EventSubscription.All))
        {
            return(new List <EventSubscription>());
        }

        if (currentSubscriptions.Contains(EventSubscription.All))
        {
            return(EventSubscriptionHelper.GetAllSubscriptionTypes().Except(inputSubscriptions.Append(EventSubscription.All)));
        }

        return(currentSubscriptions.Except(inputSubscriptions));
    }
Example #6
0
    public async Task <SlashCommandResponse> Handle(SlashCommandContext context)
    {
        var content = "";
        var sub     = await _store.GetGuildSubscription(context.GuildId, context.ChannelId);

        if (sub != null)
        {
            if (sub.LeagueId.HasValue)
            {
                var league = await _client.GetClassicLeague(sub.LeagueId.Value, tolerate404 : true);

                if (league != null)
                {
                    content += $"\n**League:**\nCurrently following the '{league.Properties.Name}' league";
                }
            }
            else
            {
                content += $"\n ⚠️ Not following any FPL leagues";
            }

            var allTypes = EventSubscriptionHelper.GetAllSubscriptionTypes();
            if (sub.Subscriptions.Any())
            {
                content += $"\n\n**Subscriptions:**\n{string.Join("\n", sub.Subscriptions.Select(s => $" ✅ {s}"))}";

                if (!sub.Subscriptions.Contains(EventSubscription.All))
                {
                    var allTypesExceptSubs = allTypes.Except(sub.Subscriptions).Except(new [] { EventSubscription.All });
                    content += $"\n\n**Not subscribing:**\n{string.Join("\n", allTypesExceptSubs.Select(s => $" ❌ {s}"))}";
                }
            }
            else
            {
                content += "\n\n**Subscriptions:**\n ⚠️ No subscriptions";
                content += $"\n\n**Events you may subscribe to:**\n{string.Join("\n", allTypes.Select(s => $" ▪️ {s}"))}";
            }
        }
        else
        {
            content = "⚠️ Not subscribing to any events. Add one to get notifications!";
        }

        return(Respond(content));
    }
Example #7
0
        public void Subscribe(SubscribeModel subscribeModel)
        {
            // Check if there is a subscription with the same parameter
            var subscription = Get(EventSubscriptionHelper.GetEventSubscriptionId(subscribeModel));

            if (subscription != null)
            {
                throw HttpResponseExceptionHelper.Create(
                          "There is already a subscription that exist between the user : "******" And the event " + subscribeModel.EventID, HttpStatusCode.BadRequest);
            }

            // create subscription
            Create(new EventSubscription()
            {
                EventID = subscribeModel.EventID,
                UserID  = subscribeModel.UserID
            });
        }
 private string FormatAllSubsAvailable()
 {
     return($"You can choose from: {string.Join(", ", EventSubscriptionHelper.GetAllSubscriptionTypes())}");
 }