Added many more braces for multiline if's, Improved .crypto command quite a bit and applied locale-specific format

This commit is contained in:
Kwoth
2022-02-04 06:00:17 +01:00
parent f77f2f433f
commit eda38e64d1
129 changed files with 635 additions and 233 deletions

View File

@@ -65,6 +65,7 @@ public partial class Searches
var favAnime = GetText(strs.anime_no_fav);
if (favorites.Length > 0 && favorites[0].QuerySelector("p") is null)
{
favAnime = string.Join("\n",
favorites[0]
.QuerySelectorAll("ul > li > div.di-tc.va-t > a")
@@ -75,6 +76,7 @@ public partial class Searches
var elem = (IHtmlAnchorElement)x;
return $"[{elem.InnerHtml}]({elem.Href})";
}));
}
var info = document.QuerySelectorAll("ul.user-status:nth-child(3) > li.clearfix")
.Select(x => Tuple.Create(x.Children[0].InnerHtml, x.Children[1].InnerHtml))

View File

@@ -1,5 +1,6 @@
#nullable disable
using NadekoBot.Modules.Searches.Services;
using System.Globalization;
namespace NadekoBot.Modules.Searches;
@@ -36,30 +37,50 @@ public partial class Searches
var usd = crypto.Quote["USD"];
var sevenDay = usd.PercentChange7d.ToString("F2", Culture);
var lastDay = usd.PercentChange24h.ToString("F2", Culture);
var localCulture = (CultureInfo)Culture.Clone();
localCulture.NumberFormat.CurrencySymbol = "$";
var sevenDay = (usd.PercentChange7d / 100).ToString("P2", localCulture);
var lastDay = (usd.PercentChange24h / 100).ToString("P2", localCulture);
var price = usd.Price < 0.01
? usd.Price.ToString(Culture)
: usd.Price.ToString("F2", Culture);
? usd.Price.ToString(localCulture)
: usd.Price.ToString("C2", localCulture);
var volume = usd.Volume24h.ToString("n0", Culture);
var marketCap = usd.MarketCap.ToString("n0", Culture);
var volume = usd.Volume24h.ToString("C0", localCulture);
var marketCap = usd.MarketCap.ToString("C0", localCulture);
var dominance = (usd.MarketCapDominance / 100).ToString("P2", localCulture);
await ctx.Channel.EmbedAsync(_eb.Create()
.WithOkColor()
.WithAuthor($"#{crypto.CmcRank}")
.WithTitle($"{crypto.Name} ({crypto.Symbol})")
.WithUrl($"https://coinmarketcap.com/currencies/{crypto.Slug}/")
.WithThumbnailUrl(
$"https://s3.coinmarketcap.com/static/img/coins/128x128/{crypto.Id}.png")
.AddField(GetText(strs.market_cap),
$"${marketCap}",
true)
.AddField(GetText(strs.price), $"${price}", true)
.AddField(GetText(strs.volume_24h), $"${volume}", true)
.AddField(GetText(strs.change_7d_24h), $"{sevenDay}% / {lastDay}%", true)
.WithImageUrl(
$"https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/{crypto.Id}.png"));
var toSend = _eb.Create()
.WithOkColor()
.WithAuthor($"#{crypto.CmcRank}")
.WithTitle($"{crypto.Name} ({crypto.Symbol})")
.WithUrl($"https://coinmarketcap.com/currencies/{crypto.Slug}/")
.WithThumbnailUrl( $"https://s3.coinmarketcap.com/static/img/coins/128x128/{crypto.Id}.png")
.AddField(GetText(strs.market_cap), marketCap, true)
.AddField(GetText(strs.price), price, true)
.AddField(GetText(strs.volume_24h), volume, true)
.AddField(GetText(strs.change_7d_24h), $"{sevenDay} / {lastDay}", true)
.AddField(GetText(strs.market_cap_dominance), dominance, true)
.WithImageUrl($"https://s3.coinmarketcap.com/generated/sparklines/web/7d/usd/{crypto.Id}.png");
if (crypto.CirculatingSupply is double cs)
{
var csStr = cs.ToString("N0", localCulture);
if (crypto.MaxSupply is double ms)
{
var perc = (cs / ms).ToString("P1", localCulture);
toSend.AddField(GetText(strs.circulating_supply), $"{csStr} ({perc})", true);
}
else
{
toSend.AddField(GetText(strs.circulating_supply), csStr, true);
}
}
await ctx.Channel.EmbedAsync(toSend);
}
}
}

View File

@@ -69,8 +69,10 @@ public class FeedsService : INService
.ToList();
if (!_lastPosts.TryGetValue(kvp.Key, out var lastFeedUpdate))
{
lastFeedUpdate = _lastPosts[kvp.Key] =
items.Any() ? items[items.Count - 1].LastUpdate : DateTime.UtcNow;
}
foreach (var (feedItem, itemUpdateDate) in items)
{
@@ -106,8 +108,10 @@ public class FeedsService : INService
.FirstOrDefault(x => x.Name.LocalName == "preview");
if (previewElement is null)
{
previewElement = afi.Element.Elements()
.FirstOrDefault(x => x.Name.LocalName == "thumbnail");
}
if (previewElement is not null)
{

View File

@@ -251,9 +251,7 @@ public partial class Searches
// poe.ninja API does not include a "chaosEquivalent" property for Chaos Orbs.
if (cleanCurrency == "Chaos Orb")
{
chaosEquivalent = 1.0F;
}
else
{
var currencyInput = obj["lines"]
@@ -265,9 +263,7 @@ public partial class Searches
}
if (cleanConvert == "Chaos Orb")
{
conversionEquivalent = 1.0F;
}
else
{
var currencyOutput = obj["lines"]

View File

@@ -60,9 +60,7 @@ public partial class Searches : NadekoModule<SearchesService>
var data = await _service.GetWeatherDataAsync(query);
if (data is null)
{
embed.WithDescription(GetText(strs.city_not_found)).WithErrorColor();
}
else
{
var f = StandardConversions.CelsiusToFahrenheit;
@@ -284,6 +282,7 @@ public partial class Searches : NadekoModule<SearchesService>
query = query.Trim();
if (!_cachedShortenedLinks.TryGetValue(query, out var shortLink))
{
try
{
using var http = _httpFactory.CreateClient();
@@ -310,6 +309,7 @@ public partial class Searches : NadekoModule<SearchesService>
Log.Error(ex, "Error shortening a link: {Message}", ex.Message);
return;
}
}
await ctx.Channel.EmbedAsync(_eb.Create()
.WithOkColor()
@@ -693,9 +693,7 @@ public partial class Searches : NadekoModule<SearchesService>
}
if (obj.Error is not null || obj.Verses is null || obj.Verses.Length == 0)
{
await SendErrorAsync(obj.Error ?? "No verse found.");
}
else
{
var v = obj.Verses[0];

View File

@@ -76,9 +76,7 @@ public class SearchesService : INService
Log.Warning("data/magicitems.json is missing. Magic items are not loaded");
if (File.Exists("data/yomama.txt"))
{
_yomamaJokes = File.ReadAllLines("data/yomama.txt").Shuffle().ToList();
}
else
{
_yomamaJokes = new();

View File

@@ -172,8 +172,10 @@ public partial class Searches
}
if (data.IsLive)
{
await ReplyConfirmLocalizedAsync(strs.streamer_online(Format.Bold(data.Name),
Format.Bold(data.Viewers.ToString())));
}
else
await ReplyConfirmLocalizedAsync(strs.streamer_offline(data.Name));
}

View File

@@ -127,7 +127,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
{
if (_client.ShardId != 0)
return;
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(30));
while (await timer.WaitForNextTickAsync())
{
@@ -185,10 +185,12 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
if (_trackCounter.ContainsKey(key))
_trackCounter[key].Add(info.GuildId);
else
{
_trackCounter[key] = new()
{
info.GuildId
};
}
}
return default;
@@ -230,6 +232,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
{
var key = stream.CreateKey();
if (_shardTrackedStreams.TryGetValue(key, out var fss))
{
await fss
// send offline stream notifications only to guilds which enable it with .stoff
.SelectMany(x => x.Value)
@@ -238,6 +241,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
?.GetTextChannel(fs.ChannelId)
?.EmbedAsync(GetEmbed(fs.GuildId, stream)))
.WhenAll();
}
}
}
@@ -247,6 +251,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
{
var key = stream.CreateKey();
if (_shardTrackedStreams.TryGetValue(key, out var fss))
{
await fss.SelectMany(x => x.Value)
.Select(fs =>
{
@@ -264,6 +269,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
return textChannel.EmbedAsync(GetEmbed(fs.GuildId, stream), message);
})
.WhenAll();
}
}
}

View File

@@ -15,9 +15,12 @@ public class ImageCacherObject : IComparable<ImageCacherObject>
if (type == Booru.Danbooru && !Uri.IsWellFormedUriString(obj.FileUrl, UriKind.Absolute))
FileUrl = "https://danbooru.donmai.us" + obj.FileUrl;
else
{
FileUrl = obj.FileUrl.StartsWith("http", StringComparison.InvariantCulture)
? obj.FileUrl
: "https:" + obj.FileUrl;
}
SearchType = type;
Rating = obj.Rating;
Tags = new((obj.Tags ?? obj.TagString).Split(' '));

View File

@@ -46,8 +46,10 @@ public class NotifChecker
.ToList();
if (remove)
{
foreach (var toBeRemoved in toReturn)
_streamProviders[toBeRemoved.Type].ClearErrorsFor(toBeRemoved.Name);
}
return toReturn;
}
@@ -74,9 +76,11 @@ public class NotifChecker
// get all stream data for the streams of this type
if (_streamProviders.TryGetValue(x.Key,
out var provider))
{
return provider.GetStreamDataAsync(x.Value
.Select(entry => entry.Key)
.ToList());
}
// this means there's no provider for this stream data, (and there was before?)
return Task.FromResult<IReadOnlyCollection<StreamData>>(
@@ -111,9 +115,7 @@ public class NotifChecker
// before it sends an offline notification to the subscribers.
var streamId = (key.Type, key.Name);
if (!newData.IsLive && _offlineBuffer.Remove(streamId))
{
newlyOffline.Add(newData);
}
else if (newData.IsLive != oldData.IsLive)
{
if (newData.IsLive)