From 4cf3bdb53a22cc82548d5cac764ee3abe3d11db3 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 21 Mar 2022 13:33:43 +0100 Subject: [PATCH] Removed unneeded httpclient for searchimages and some minor cleanup --- .../Modules/Nsfw/SearchImagesService.cs | 18 ++++++------------ src/NadekoBot/Services/Impl/StatsService.cs | 13 +++++++++---- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/NadekoBot/Modules/Nsfw/SearchImagesService.cs b/src/NadekoBot/Modules/Nsfw/SearchImagesService.cs index 0a81408c0..1b266fbea 100644 --- a/src/NadekoBot/Modules/Nsfw/SearchImagesService.cs +++ b/src/NadekoBot/Modules/Nsfw/SearchImagesService.cs @@ -7,12 +7,6 @@ using Newtonsoft.Json.Linq; namespace NadekoBot.Modules.Nsfw; -public record TagRequest( - ulong GuildId, - bool ForceExplicit, - Booru SearchType, - params string[] Tags); - public record UrlReply { public string Error { get; init; } @@ -30,7 +24,6 @@ public class SearchImagesService : ISearchImagesService, INService public ConcurrentDictionary AutoBoobTimers { get; } = new(); public ConcurrentDictionary AutoButtTimers { get; } = new(); private readonly Random _rng; - private readonly HttpClient _http; private readonly SearchImageCacher _cache; private readonly IHttpClientFactory _httpFactory; private readonly DbService _db; @@ -39,14 +32,11 @@ public class SearchImagesService : ISearchImagesService, INService public SearchImagesService( DbService db, - IHttpClientFactory http, SearchImageCacher cacher, IHttpClientFactory httpFactory) { _db = db; _rng = new NadekoRandom(); - _http = http.CreateClient(); - _http.AddFakeHeaders(); _cache = cacher; _httpFactory = httpFactory; @@ -205,8 +195,10 @@ public class SearchImagesService : ISearchImagesService, INService { try { + using var http = _httpFactory.CreateClient(); + http.AddFakeHeaders(); JToken obj; - obj = JArray.Parse(await _http.GetStringAsync($"http://api.oboobs.ru/boobs/{_rng.Next(0, 12000)}"))[0]; + obj = JArray.Parse(await http.GetStringAsync($"http://api.oboobs.ru/boobs/{_rng.Next(0, 12000)}"))[0]; return new() { Error = "", @@ -269,8 +261,10 @@ public class SearchImagesService : ISearchImagesService, INService { try { + using var http = _httpFactory.CreateClient(); + http.AddFakeHeaders(); JToken obj; - obj = JArray.Parse(await _http.GetStringAsync($"http://api.obutts.ru/butts/{_rng.Next(0, 6100)}"))[0]; + obj = JArray.Parse(await http.GetStringAsync($"http://api.obutts.ru/butts/{_rng.Next(0, 6100)}"))[0]; return new() { Error = "", diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index 43376b85e..7c35fec98 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -1,4 +1,4 @@ -#nullable disable +#nullable disable using Humanizer.Localisation; using NadekoBot.Common.ModuleBehaviors; using System.Diagnostics; @@ -133,11 +133,16 @@ public class StatsService : IStatsService, IReadyExecutor, INService, IDisposabl }; } - public async Task OnReadyAsync() + private void InitializeChannelCount() { var guilds = _client.Guilds; - textChannels = guilds.Sum(g => g.Channels.Count(cx => cx is ITextChannel)); - voiceChannels = guilds.Sum(g => g.Channels.Count(cx => cx is IVoiceChannel)); + textChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is ITextChannel)); + voiceChannels = guilds.Sum(static g => g.Channels.Count(static cx => cx is IVoiceChannel)); + } + + public async Task OnReadyAsync() + { + InitializeChannelCount(); using var timer = new PeriodicTimer(TimeSpan.FromHours(1)); do