diff --git a/NadekoBot.Core/Modules/Searches/Common/CryptoData.cs b/NadekoBot.Core/Modules/Searches/Common/CryptoData.cs new file mode 100644 index 00000000..8f499835 --- /dev/null +++ b/NadekoBot.Core/Modules/Searches/Common/CryptoData.cs @@ -0,0 +1,21 @@ +using Newtonsoft.Json; + +namespace NadekoBot.Core.Modules.Searches.Common +{ + public class CryptoData + { + public string Id { get; set; } + public string Name { get; set; } + public string Symbol { get; set; } + public int Rank { get; set; } + public string Price_Usd { get; set; } + public string Price_Btc { get; set; } + public decimal? Market_Cap_Usd { get; set; } + [JsonProperty("24h_volume_usd")] + public double? _24h_Volume_Usd { get; set; } + public string Percent_Change_1h { get; set; } + public string Percent_Change_24h { get; set; } + public string Percent_Change_7d { get; set; } + public string LastUpdated { get; set; } + } +} diff --git a/NadekoBot.Core/Modules/Searches/Searches.cs b/NadekoBot.Core/Modules/Searches/Searches.cs index 696cb449..00bae886 100644 --- a/NadekoBot.Core/Modules/Searches/Searches.cs +++ b/NadekoBot.Core/Modules/Searches/Searches.cs @@ -36,6 +36,30 @@ namespace NadekoBot.Modules.Searches _google = google; } + [NadekoCommand, Usage, Description, Aliases] + [RequireContext(ContextType.Guild)] + public async Task Crypto(string name) + { + name = name?.ToLowerInvariant(); + + if (string.IsNullOrWhiteSpace(name)) + return; + + var crypto = (await _service.CryptoData().ConfigureAwait(false)) + ?.FirstOrDefault(x => x.Id.ToLowerInvariant() == name || x.Name.ToLowerInvariant() == name); + + if (crypto == null) + return; //todo error message + + await Context.Channel.EmbedAsync(new EmbedBuilder() + .WithOkColor() + .WithTitle($"{crypto.Name} ({crypto.Id})") + .AddField("Market Cap", $"${crypto.Market_Cap_Usd:n0}", true) + .AddField("Price", $"${crypto.Price_Usd}", true) + .AddField("Volume (24h)", $"${crypto._24h_Volume_Usd:n0}", true) + .AddField("Change (7d/24h)", $"{crypto.Percent_Change_7d}% / {crypto.Percent_Change_24h}%", true)); + } + //for anonymasen :^) [NadekoCommand, Usage, Description, Aliases] public async Task Rip([Remainder]IGuildUser usr) diff --git a/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs b/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs index 74e501d7..b3ec3467 100644 --- a/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs +++ b/NadekoBot.Core/Modules/Searches/Services/SearchesService.cs @@ -23,6 +23,7 @@ using Image = ImageSharp.Image; using SixLabors.Primitives; using SixLabors.Fonts; using NadekoBot.Core.Services.Impl; +using NadekoBot.Core.Modules.Searches.Common; namespace NadekoBot.Modules.Searches.Services { @@ -37,7 +38,6 @@ namespace NadekoBot.Modules.Searches.Services private readonly IImageCache _imgs; private readonly IDataCache _cache; private readonly FontProvider _fonts; - private readonly HttpClient http; public ConcurrentDictionary TranslatedChannels { get; } = new ConcurrentDictionary(); public ConcurrentDictionary UserLanguages { get; } = new ConcurrentDictionary(); @@ -52,12 +52,21 @@ namespace NadekoBot.Modules.Searches.Services public List MagicItems { get; } = new List(); private readonly ConcurrentDictionary _imageCacher = new ConcurrentDictionary(); - + public ConcurrentDictionary AutoHentaiTimers { get; } = new ConcurrentDictionary(); public ConcurrentDictionary AutoBoobTimers { get; } = new ConcurrentDictionary(); public ConcurrentDictionary AutoButtTimers { get; } = new ConcurrentDictionary(); private readonly ConcurrentDictionary> _blacklistedTags = new ConcurrentDictionary>(); + private readonly Timer _t; + + public async Task CryptoData() + { + var data = await _cache.Redis.GetDatabase() + .StringGetAsync("crypto_data").ConfigureAwait(false); + + return JsonConvert.DeserializeObject(data); + } public SearchesService(DiscordSocketClient client, IGoogleApiService google, DbService db, NadekoBot bot, IDataCache cache, @@ -72,7 +81,6 @@ namespace NadekoBot.Modules.Searches.Services _imgs = cache.LocalImages; _cache = cache; _fonts = fonts; - http = new HttpClient(); _blacklistedTags = new ConcurrentDictionary>( bot.AllGuildConfigs.ToDictionary( @@ -113,17 +121,28 @@ namespace NadekoBot.Modules.Searches.Services return Task.CompletedTask; }; - //pokemon commands - if (File.Exists(PokemonListFile)) + if (client.ShardId == 0) { - Pokemons = JsonConvert.DeserializeObject>(File.ReadAllText(PokemonListFile)); + _t = new Timer(async _ => + { + var r = _cache.Redis.GetDatabase(); + try + { + var data = (string)(await r.StringGetAsync("crypto_data").ConfigureAwait(false)); + if (data == null) + { + data = await Http.GetStringAsync("https://api.coinmarketcap.com/v1/ticker/") + .ConfigureAwait(false); + + await r.StringSetAsync("crypto_data", data, TimeSpan.FromHours(6)).ConfigureAwait(false); + } + } + catch (Exception ex) + { + _log.Warn(ex); + } + }, null, TimeSpan.Zero, TimeSpan.FromHours(1)); } - else - _log.Warn(PokemonListFile + " is missing. Pokemon abilities not loaded."); - if (File.Exists(PokemonAbilitiesFile)) - PokemonAbilities = JsonConvert.DeserializeObject>(File.ReadAllText(PokemonAbilitiesFile)); - else - _log.Warn(PokemonAbilitiesFile + " is missing. Pokemon abilities not loaded."); //joke commands if (File.Exists("data/wowjokes.json")) @@ -146,7 +165,7 @@ namespace NadekoBot.Modules.Searches.Services var (succ, data) = await _cache.TryGetImageDataAsync(imgUrl); if (!succ) { - using (var temp = await http.GetAsync(imgUrl, HttpCompletionOption.ResponseHeadersRead)) + using (var temp = await Http.GetAsync(imgUrl, HttpCompletionOption.ResponseHeadersRead)) { if (temp.Content.Headers.ContentType.MediaType != "image/png" && temp.Content.Headers.ContentType.MediaType != "image/jpeg" diff --git a/NadekoBot.sln b/NadekoBot.sln index 79ae8766..2be3656b 100644 --- a/NadekoBot.sln +++ b/NadekoBot.sln @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution ProjectSection(SolutionItems) = preProject global.json = global.json NadekoBot.iss = NadekoBot.iss + Performance1.psess = Performance1.psess EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NadekoBot", "src\NadekoBot\NadekoBot.csproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}"