public async Task Bulk([Remainder] string operationString) { // Instantiate the bulk operation and get the query results. Taxa.IBulkOperation bulkOperation = new Taxa.BulkOperation(operationString); ISearchResult queryResult = await Db.GetSearchResultsAsync(SearchContext, bulkOperation.Query); if (queryResult.TotalResults() <= 0) { await ReplyInfoAsync("No species matching this query could be found."); } else { // Perform the requested operation. switch (bulkOperation.OperationName) { case "addto": { // Move the species into the given zone. if (bulkOperation.Arguments.Count() != 1) { throw new Exception(string.Format("This operation requires **{0}** argument(s), but was given **{1}**.", 1, bulkOperation.Arguments.Count())); } string zoneName = bulkOperation.Arguments.First(); IZone zone = await Db.GetZoneAsync(zoneName); if (await BotUtils.ReplyValidateZoneAsync(Context, zone, zoneName)) { IPaginatedMessage message = new PaginatedMessage(string.Format("**{0}** species will be added to **{1}**. Is this OK?", queryResult.TotalResults(), zone.GetFullName())) { Restricted = true }; message.AddReaction(PaginatedMessageReactionType.Yes, async(args) => { foreach (ISpecies species in await queryResult.GetResultsAsync()) { await Db.AddZonesAsync(species, new IZone[] { zone }); } await ReplySuccessAsync("Operation completed successfully."); }); await ReplyAsync(message); } } break; default: await ReplyErrorAsync($"Unknown operation {bulkOperation.OperationName.ToBold()}."); break; } } }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { // Filter all species that aren't in the first n results. if (int.TryParse(Value, out int limit)) { IEnumerable <ISpecies> searchResults = (await result.GetResultsAsync()) .Take(limit); await result.FilterByAsync(async (species) => await Task.FromResult(!searchResults.Any(sp => sp.Id == species.Id)), Invert); } }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { if (int.TryParse(Value, out int count) && count > 0) { IEnumerable <ISpecies> results = await result.GetResultsAsync(); // Take N random IDs from the results. IEnumerable <long> randomIds = results .Where(species => species.Id.HasValue) .OrderBy(species => NumberUtilities.GetRandomInteger(int.MaxValue)) .Take(count) .Select(species => (long)species.Id) .ToArray(); // Filter all but those results. await result.FilterByAsync(async (species) => await Task.FromResult(!randomIds.Any(id => id == species.Id)), Invert); } }