diff --git a/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs b/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs index 9fee9d79c..2410ec9b7 100644 --- a/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs +++ b/src/NadekoBot.Generators/Command/CommandAttributesGenerator.cs @@ -160,7 +160,8 @@ public class CmdAttribute : System.Attribute var methodModels = methods .Select(x => MethodDeclarationToMethodModel(compilation, x!)) - .Where(static x => x is not null); + .Where(static x => x is not null) + .Cast(); var groups = methodModels .GroupBy(static x => $"{x.Namespace}.{string.Join(".", x.Classes)}"); @@ -180,7 +181,7 @@ public class CmdAttribute : System.Attribute var model = new FileModel( methods: elems, ns: elems[0].Namespace, - classHierarchy: elems[0].Classes + classHierarchy: elems![0].Classes ); models.Add(model); diff --git a/src/NadekoBot/Modules/Games/Trivia/TriviaQuestionPool.cs b/src/NadekoBot/Modules/Games/Trivia/TriviaQuestionPool.cs index 7ff0a4fe2..79d9ec763 100644 --- a/src/NadekoBot/Modules/Games/Trivia/TriviaQuestionPool.cs +++ b/src/NadekoBot/Modules/Games/Trivia/TriviaQuestionPool.cs @@ -19,14 +19,14 @@ public class TriviaQuestionPool { var pokes = await _cache.GetPokemonMapAsync(); - if (pokes is null or { Length: 0 }) + if (pokes is null or { Count: 0 }) return default; var num = _rng.Next(1, _maxPokemonId + 1); return new(new() { Question = "Who's That Pokémon?", - Answer = pokes[num].Name.ToTitleCase(), + Answer = pokes[num].ToTitleCase(), Category = "Pokemon", ImageUrl = $@"https://nadeko.bot/images/pokemon/shadows/{num}.png", AnswerImageUrl = $@"https://nadeko.bot/images/pokemon/real/{num}.png" diff --git a/src/NadekoBot/Services/ILocalDataCache.cs b/src/NadekoBot/Services/ILocalDataCache.cs index 2e3d3edba..f37182d01 100644 --- a/src/NadekoBot/Services/ILocalDataCache.cs +++ b/src/NadekoBot/Services/ILocalDataCache.cs @@ -9,5 +9,5 @@ public interface ILocalDataCache Task> GetPokemonsAsync(); Task> GetPokemonAbilitiesAsync(); Task GetTriviaQuestionsAsync(); - Task GetPokemonMapAsync(); + Task> GetPokemonMapAsync(); } \ No newline at end of file diff --git a/src/NadekoBot/Services/Impl/LocalDataCache.cs b/src/NadekoBot/Services/Impl/LocalDataCache.cs index 4714c7703..274965952 100644 --- a/src/NadekoBot/Services/Impl/LocalDataCache.cs +++ b/src/NadekoBot/Services/Impl/LocalDataCache.cs @@ -67,11 +67,37 @@ public sealed class LocalDataCache : ILocalDataCache, INService => await GetOrCreateCachedDataAsync(_pokemonAbilitiesKey, POKEMON_ABILITIES_FILE); - private static TypedKey _pokeMapKey - = new("pokemon:ab_map"); + private static TypedKey> _pokeMapKey + = new("pokemon:ab_map2"); // 2 because ab_map was storing arrays - public async Task GetPokemonMapAsync() - => await GetOrCreateCachedDataAsync(_pokeMapKey, POKEMON_MAP_PATH); + public async Task?> GetPokemonMapAsync() + => await _cache.GetOrAddAsync(_pokeMapKey, + async () => + { + var fileName = POKEMON_MAP_PATH; + if (!File.Exists(fileName)) + { + Log.Warning($"{fileName} is missing. Relevant data can't be loaded"); + return default; + } + + try + { + await using var stream = File.OpenRead(fileName); + var arr = await JsonSerializer.DeserializeAsync(stream, _opts); + + return (IReadOnlyDictionary?)arr?.ToDictionary(x => x.Id, x => x.Name); + } + catch (Exception ex) + { + Log.Error(ex, + "Error reading {FileName} file: {ErrorMessage}", + fileName, + ex.Message); + + return default; + } + }); private static TypedKey _triviaKey