示例#1
0
        public async Task <CommandResult> Badges(CommandContext context)
        {
            (Optional <PkmnSpecies> optionalSpecies, Optional <User> optionalUser) =
                await context.ParseArgs <Optional <PkmnSpecies>, Optional <User> >();

            Console.WriteLine($"species present: {optionalSpecies.IsPresent}");
            Console.WriteLine($"user present: {optionalUser.IsPresent}");
            bool isSelf = !optionalUser.IsPresent;
            User user   = isSelf ? context.Message.User : optionalUser.Value;

            if (optionalSpecies.IsPresent)
            {
                PkmnSpecies species   = optionalSpecies.Value;
                long        numBadges = await _badgeRepo.CountByUserAndSpecies(user.Id, species);

                return(new CommandResult
                {
                    Response = numBadges == 0
                        ? isSelf
                            ? $"You have no {species} badges."
                            : $"{user.Name} has no {species} badges."
                        : isSelf
                            ? $"You have {numBadges}x {species} badges."
                            : $"{user.Name} has {numBadges}x {species} badges."
                });
            }
            else
            {
                ImmutableSortedDictionary <PkmnSpecies, int> numBadgesPerSpecies =
                    await _badgeRepo.CountByUserPerSpecies(user.Id);

                if (!numBadgesPerSpecies.Any())
                {
                    return(new CommandResult
                    {
                        Response = isSelf ? "You have no badges." : $"{user.Name} has no badges."
                    });
                }
                IEnumerable <string> badgesFormatted = numBadgesPerSpecies.Select(kvp => $"{kvp.Value}x {kvp.Key}");
                return(new CommandResult
                {
                    Response = isSelf
                        ? $"Your badges: {string.Join(", ", badgesFormatted)}"
                        : $"{user.Name}'s badges: {string.Join(", ", badgesFormatted)}",
                    ResponseTarget = ResponseTarget.WhisperIfLong
                });
            }
        }