diff --git a/NadekoBot.Core/Modules/Searches/Searches.cs b/NadekoBot.Core/Modules/Searches/Searches.cs index 00bae886..01bea478 100644 --- a/NadekoBot.Core/Modules/Searches/Searches.cs +++ b/NadekoBot.Core/Modules/Searches/Searches.cs @@ -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 :^) diff --git a/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs b/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs index a72a4865..1a03290b 100644 --- a/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs +++ b/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs @@ -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 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 pageFunc, int totalElements, + int itemsPerPage, bool addPaginatedFooter = true) => + channel.SendPaginatedConfirmAsync(client, currentPage, + (x) => Task.FromResult(pageFunc(x)), totalElements, itemsPerPage, addPaginatedFooter); /// /// danny kamisama /// diff --git a/src/NadekoBot/_strings/ResponseStrings.en-US.json b/src/NadekoBot/_strings/ResponseStrings.en-US.json index e8e862fb..7a439a5b 100644 --- a/src/NadekoBot/_strings/ResponseStrings.en-US.json +++ b/src/NadekoBot/_strings/ResponseStrings.en-US.json @@ -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}?" } \ No newline at end of file diff --git a/src/NadekoBot/data/command_strings.json b/src/NadekoBot/data/command_strings.json index 3e244beb..6c0c2529 100644 --- a/src/NadekoBot/data/command_strings.json +++ b/src/NadekoBot/data/command_strings.json @@ -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" + ] } }