Work on .crypto command

This commit is contained in:
Master Kwoth 2017-11-06 13:34:51 +01:00
parent 607decfbcc
commit 0330ac24c4
4 changed files with 78 additions and 13 deletions

View File

@ -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; }
}
}

View File

@ -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)

View File

@ -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<ulong, bool> TranslatedChannels { get; } = new ConcurrentDictionary<ulong, bool>();
public ConcurrentDictionary<UserChannelPair, string> UserLanguages { get; } = new ConcurrentDictionary<UserChannelPair, string>();
@ -58,6 +58,15 @@ namespace NadekoBot.Modules.Searches.Services
public ConcurrentDictionary<ulong, Timer> AutoButtTimers { get; } = new ConcurrentDictionary<ulong, Timer>();
private readonly ConcurrentDictionary<ulong, HashSet<string>> _blacklistedTags = new ConcurrentDictionary<ulong, HashSet<string>>();
private readonly Timer _t;
public async Task<CryptoData[]> CryptoData()
{
var data = await _cache.Redis.GetDatabase()
.StringGetAsync("crypto_data").ConfigureAwait(false);
return JsonConvert.DeserializeObject<CryptoData[]>(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<ulong, HashSet<string>>(
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<Dictionary<string, SearchPokemon>>(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<Dictionary<string, SearchPokemonAbility>>(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"

View File

@ -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}"