.crypto command added, and it's kinda smart as a bonus.

This commit is contained in:
Master Kwoth 2017-11-07 06:51:36 +01:00
parent 3d903e028a
commit 3518ebc145
4 changed files with 66 additions and 12 deletions

View File

@ -22,6 +22,7 @@ using NadekoBot.Modules.Searches.Common;
using NadekoBot.Modules.Searches.Services;
using NadekoBot.Common.Replacements;
using Discord.WebSocket;
using NadekoBot.Core.Modules.Searches.Common;
namespace NadekoBot.Modules.Searches
{
@ -44,20 +45,56 @@ namespace NadekoBot.Modules.Searches
if (string.IsNullOrWhiteSpace(name))
return;
var cryptos = (await _service.CryptoData().ConfigureAwait(false));
var crypto = cryptos
?.FirstOrDefault(x => x.Id.ToLowerInvariant() == name || x.Name.ToLowerInvariant() == name
|| x.Symbol.ToLowerInvariant() == name);
var crypto = (await _service.CryptoData().ConfigureAwait(false))
?.FirstOrDefault(x => x.Id.ToLowerInvariant() == name || x.Name.ToLowerInvariant() == name);
(CryptoData Elem, int Distance)? nearest = null;
if (crypto == null)
{
nearest = cryptos.Select(x => (x, Distance: x.Name.ToLowerInvariant().LevenshteinDistance(name)))
.OrderBy(x => x.Distance)
.Where(x => x.Distance <= 2)
.FirstOrDefault();
crypto = nearest?.Elem;
}
if (crypto == null)
return; //todo error message
{
await ReplyErrorLocalized("crypto_not_found").ConfigureAwait(false);
return;
}
if (nearest != null)
{
//wrap this into some class, ther'es the same code in execsql too
var msg = await Context.Channel.EmbedAsync(new EmbedBuilder()
.WithOkColor()
.WithTitle(GetText("crypto_not_found"))
.WithDescription(GetText("did_you_mean", Format.Bold($"{crypto.Name} ({crypto.Symbol})")))
.WithFooter("Y/n")).ConfigureAwait(false);
var input = await GetUserInputAsync(Context.User.Id, Context.Channel.Id);
input = input?.ToLowerInvariant().ToString();
if (input != "yes" && input != "y")
{
var __ = msg.DeleteAsync();
return;
}
var _ = msg.DeleteAsync();
}
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));
.WithTitle($"{crypto.Name} ({crypto.Symbol})")
.WithThumbnailUrl($"https://files.coinmarketcap.com/static/img/coins/32x32/{crypto.Id}.png")
.AddField(GetText("market_cap"), $"${crypto.Market_Cap_Usd:n0}", true)
.AddField(GetText("price"), $"${crypto.Price_Usd}", true)
.AddField(GetText("volume_24h"), $"${crypto._24h_Volume_Usd:n0}", true)
.AddField(GetText("change_7d_24h"), $"{crypto.Percent_Change_7d}% / {crypto.Percent_Change_24h}%", true));
}
//for anonymasen :^)

View File

@ -54,9 +54,12 @@ namespace NadekoBot.Extensions
private static readonly IEmote arrow_left = new Emoji("⬅");
private static readonly IEmote arrow_right = new Emoji("➡");
//todo update this
public static Task SendPaginatedConfirmAsync(this IMessageChannel channel, DiscordSocketClient client, int currentPage, Func<int, EmbedBuilder> pageFunc, int totalElements, int itemsPerPage, bool addPaginatedFooter = true) =>
channel.SendPaginatedConfirmAsync(client, currentPage, (x) => Task.FromResult(pageFunc(x)), totalElements, itemsPerPage, addPaginatedFooter);
public static Task SendPaginatedConfirmAsync(this IMessageChannel channel, DiscordSocketClient client,
int currentPage, Func<int, EmbedBuilder> pageFunc, int totalElements,
int itemsPerPage, bool addPaginatedFooter = true) =>
channel.SendPaginatedConfirmAsync(client, currentPage,
(x) => Task.FromResult(pageFunc(x)), totalElements, itemsPerPage, addPaginatedFooter);
/// <summary>
/// danny kamisama
/// </summary>

View File

@ -911,5 +911,11 @@
"gambling_timely": "You've claimed your {0}. You can claim again in {1}h",
"gambling_timely_set": "Users will be able to claim {0} every {1}h",
"gambling_timely_set_none": "Users will not be able to claim any timely currency.",
"gambling_timely_reset": "All users will be able to claim timely currency again."
"gambling_timely_reset": "All users will be able to claim timely currency again.",
"searches_price": "Price",
"searches_market_cap": "Market Cap",
"searches_volume_24h": "Volume (24h)",
"searches_change_7d_24h": "Change (7d/24h)",
"searches_crypto_not_found": "Cryptocurrency with that name was not found.",
"searches_did_you_mean": "Did you mean {0}?"
}

View File

@ -3090,5 +3090,13 @@
"usage": [
"{0}novel the nine cauldrons"
]
},
"crypto": {
"cmd": "crypto c",
"desc": "Shows basic stats about a cryptocurrency from coinmarketcap.com. You can use either a name or an abbreviation of the currency.",
"usage": [
"{0}c btc",
"{0}c bitcoin"
]
}
}