示例#1
0
    public async Task <IActionResult> OnPost(string teamId, int leagueId, string channel, EventSubscription[] subscriptions)
    {
        var league = await _leagueClient.GetClassicLeague(leagueId, tolerate404 : true);

        if (league == null)
        {
            TempData["msg"] = "⚠️ League does not exist.\n";
        }

        var slackClient = await CreateSlackClient(teamId);

        var channelsRes = await slackClient.ConversationsListPublicChannels(500);

        var channelsFound = channelsRes.Channels.Any(c => channel == $"#{c.Name}" || channel == c.Id);

        if (!channelsFound)
        {
            var channelsText = string.Join(',', channelsRes.Channels.Select(c => c.Name));
            TempData["msg"] += $"WARN. Could not find updated channel in via Slack API lookup. Channels: {channelsText}";
        }

        await _teamRepo.UpdateLeagueId(teamId, leagueId);

        await _teamRepo.UpdateChannel(teamId, channel);

        await _teamRepo.UpdateSubscriptions(teamId, subscriptions);

        TempData["msg"] += "Updated!";
        return(RedirectToPage("Edit"));
    }
示例#2
0
    public async Task <EventHandledResponse> Handle(BlockActionInteraction blockActionEvent)
    {
        var actionsBlock = blockActionEvent.Actions.FirstOrDefault(x => x.action_id.Equals("fpl_league_id_action"));

        if (actionsBlock == null)
        {
            return(new EventHandledResponse("IGNORE. THIS IS NOT FOR ME"));
        }

        var leagueId = actionsBlock.value;

        if (!int.TryParse(leagueId, out var newLeagueID))
        {
            return(new EventHandledResponse("VALIDATION_ERRORS"));
        }

        try
        {
            await _leagueClient.GetClassicLeague(newLeagueID);
        }
        catch (Exception)
        {
            return(new EventHandledResponse("VALIDATION_ERRORS"));
        }

        await _teamRepo.UpdateLeagueId(blockActionEvent.Team.Id, newLeagueID);



        return(new EventHandledResponse("League ID updated"));
    }
示例#3
0
        public override async Task <EventHandledResponse> Handle(EventMetaData eventMetadata, AppMentionEvent message)
        {
            var newLeagueId = ParseArguments(message);

            if (string.IsNullOrEmpty(newLeagueId))
            {
                var help = $"No leagueId provided. Usage: `@fplbot updateleagueid 123`";
                await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, help);

                return(new EventHandledResponse(help));
            }

            var couldParse = int.TryParse(newLeagueId, out var theLeagueId);

            if (!couldParse)
            {
                var res = $"Could not update league to id '{newLeagueId}'. Make sure it's a valid number.";
                await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, res);

                return(new EventHandledResponse(res));
            }

            var failure = $"Could not find league {newLeagueId} :/ Could you find it at https://fantasy.premierleague.com/leagues/{newLeagueId}/standings/c ?";

            try
            {
                var league = await _leagueClient.GetClassicLeague(theLeagueId);

                if (league?.Properties != null)
                {
                    await _slackTeamRepository.UpdateLeagueId(eventMetadata.Team_Id, theLeagueId);

                    var success = $"Thanks! You're now following the '{league.Properties.Name}' league (leagueId: {theLeagueId})";
                    await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, success);

                    return(new EventHandledResponse(success));
                }
                await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, failure);

                return(new EventHandledResponse(failure));
            }
            catch (HttpRequestException e)
            {
                _logger.LogError(e.Message, e);
                await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, failure);

                return(new EventHandledResponse(failure));
            }
        }