- Updated editorconfig rules to hopefully look a bit nicer.

- Removed configureawait(false) from everywhere as it doesnt' do anything in a console app and just makes the code look ugly
- Started using .WhenAll extension instead of Task.WhenAll to make it look nicer when chaining methods
This commit is contained in:
Kwoth
2021-12-28 21:14:26 +01:00
parent d093f7eed7
commit 723447c7d4
171 changed files with 1523 additions and 1594 deletions

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using AngleSharp;
using AngleSharp.Html.Dom;
using NadekoBot.Modules.Searches.Common;
@@ -26,14 +26,14 @@ public class AnimeSearchService : INService
var link = "https://aniapi.nadeko.bot/anime/" + Uri.EscapeDataString(query.Replace("/", " ", StringComparison.InvariantCulture));
link = link.ToLowerInvariant();
var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false);
var (ok, data) = await _cache.TryGetAnimeDataAsync(link);
if (!ok)
{
using (var http = _httpFactory.CreateClient())
{
data = await http.GetStringAsync(link).ConfigureAwait(false);
data = await http.GetStringAsync(link);
}
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
await _cache.SetAnimeDataAsync(link, data);
}
@@ -58,11 +58,11 @@ public class AnimeSearchService : INService
.Replace("/", " ")
);
link = link.ToLowerInvariant();
var (ok, data) = await _cache.TryGetNovelDataAsync(link).ConfigureAwait(false);
var (ok, data) = await _cache.TryGetNovelDataAsync(link);
if (!ok)
{
var config = Configuration.Default.WithDefaultLoader();
using var document = await BrowsingContext.New(config).OpenAsync(link).ConfigureAwait(false);
using var document = await BrowsingContext.New(config).OpenAsync(link);
var imageElem = document.QuerySelector("div.seriesimg > img");
if (imageElem is null)
return null;
@@ -109,7 +109,7 @@ public class AnimeSearchService : INService
};
await _cache.SetNovelDataAsync(link,
JsonConvert.SerializeObject(obj)).ConfigureAwait(false);
JsonConvert.SerializeObject(obj));
return obj;
}
@@ -132,14 +132,14 @@ public class AnimeSearchService : INService
var link = "https://aniapi.nadeko.bot/manga/" + Uri.EscapeDataString(query.Replace("/", " ", StringComparison.InvariantCulture));
link = link.ToLowerInvariant();
var (ok, data) = await _cache.TryGetAnimeDataAsync(link).ConfigureAwait(false);
var (ok, data) = await _cache.TryGetAnimeDataAsync(link);
if (!ok)
{
using (var http = _httpFactory.CreateClient())
{
data = await http.GetStringAsync(link).ConfigureAwait(false);
data = await http.GetStringAsync(link);
}
await _cache.SetAnimeDataAsync(link, data).ConfigureAwait(false);
await _cache.SetAnimeDataAsync(link, data);
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using NadekoBot.Modules.Searches.Common;
using Newtonsoft.Json;
@@ -25,7 +25,7 @@ public class CryptoService : INService
}
name = name.ToUpperInvariant();
var cryptos = await CryptoData().ConfigureAwait(false);
var cryptos = await CryptoData();
if (cryptos is null)
return (null, null);

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using CodeHollow.FeedReader.Feeds;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
@@ -52,7 +52,7 @@ public class FeedsService : INService
var rssUrl = kvp.Key;
try
{
var feed = await CodeHollow.FeedReader.FeedReader.ReadAsync(rssUrl).ConfigureAwait(false);
var feed = await CodeHollow.FeedReader.FeedReader.ReadAsync(rssUrl);
var items = feed
.Items
@@ -143,7 +143,7 @@ public class FeedsService : INService
.Where(x => x != null)
.Select(x => x.EmbedAsync(embed));
allSendTasks.Add(Task.WhenAll(feedSendTasks));
allSendTasks.Add(feedSendTasks.WhenAll());
}
}
catch
@@ -151,7 +151,7 @@ public class FeedsService : INService
}
}
await Task.WhenAll(Task.WhenAll(allSendTasks), Task.Delay(10000)).ConfigureAwait(false);
await Task.WhenAll(Task.WhenAll(allSendTasks), Task.Delay(10000));
}
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using NadekoBot.Modules.Searches.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -75,7 +75,7 @@ public class SearchesService : INService
var data = await _cache.GetOrAddCachedDataAsync($"nadeko_rip_{text}_{imgUrl}",
GetRipPictureFactory,
(text, imgUrl),
TimeSpan.FromDays(1)).ConfigureAwait(false);
TimeSpan.FromDays(1));
return data.ToStream();
}
@@ -149,7 +149,7 @@ public class SearchesService : INService
var data = await http.GetStringAsync($"http://api.openweathermap.org/data/2.5/weather?" +
$"q={query}&" +
$"appid=42cd627dd60debf25a5739e50a217d74&" +
$"units=metric").ConfigureAwait(false);
$"units=metric");
if (data is null)
return null;
@@ -288,7 +288,7 @@ public class SearchesService : INService
// using (var http = _httpFactory.CreateClient())
// {
// var response = await http.GetStringAsync(new Uri("http://api.yomomma.info/")).ConfigureAwait(false);
// var response = await http.GetStringAsync(new Uri("http://api.yomomma.info/"));
// return JObject.Parse(response)["joke"].ToString() + " 😆";
// }
}
@@ -304,7 +304,7 @@ public class SearchesService : INService
public async Task<string> GetChuckNorrisJoke()
{
using var http = _httpFactory.CreateClient();
var response = await http.GetStringAsync(new Uri("http://api.icndb.com/jokes/random/")).ConfigureAwait(false);
var response = await http.GetStringAsync(new Uri("http://api.icndb.com/jokes/random/"));
return JObject.Parse(response)["value"]["joke"].ToString() + " 😆";
}
@@ -314,7 +314,7 @@ public class SearchesService : INService
var data = await _cache.GetOrAddCachedDataAsync($"nadeko_mtg_{search}",
GetMtgCardFactory,
search,
TimeSpan.FromDays(1)).ConfigureAwait(false);
TimeSpan.FromDays(1));
if (data is null || data.Length == 0)
return null;
@@ -333,7 +333,7 @@ public class SearchesService : INService
$"newSearch=false&" +
$"ProductType=All&" +
$"IsProductNameExact=false&" +
$"ProductName={Uri.EscapeDataString(card.Name)}").ConfigureAwait(false);
$"ProductName={Uri.EscapeDataString(card.Name)}");
}
catch { storeUrl = "<url can't be found>"; }
@@ -350,26 +350,18 @@ public class SearchesService : INService
using var http = _httpFactory.CreateClient();
http.DefaultRequestHeaders.Clear();
var response = await http.GetStringAsync($"https://api.magicthegathering.io/v1/cards?name={Uri.EscapeDataString(search)}")
.ConfigureAwait(false);
var response = await http.GetStringAsync($"https://api.magicthegathering.io/v1/cards?name={Uri.EscapeDataString(search)}");
var responseObject = JsonConvert.DeserializeObject<MtgResponse>(response);
if (responseObject is null)
return new MtgData[0];
return Array.Empty<MtgData>();
var cards = responseObject.Cards.Take(5).ToArray();
if (cards.Length == 0)
return new MtgData[0];
return Array.Empty<MtgData>();
var tasks = new List<Task<MtgData>>(cards.Length);
for (var i = 0; i < cards.Length; i++)
{
var card = cards[i];
tasks.Add(GetMtgDataAsync(card));
}
return await Task.WhenAll(tasks).ConfigureAwait(false);
return await cards.Select(GetMtgDataAsync)
.WhenAll();
}
public Task<HearthstoneCardData> GetHearthstoneCardDataAsync(string name)
@@ -389,7 +381,7 @@ public class SearchesService : INService
try
{
var response = await http.GetStringAsync($"https://omgvamp-hearthstone-v1.p.rapidapi.com/" +
$"cards/search/{Uri.EscapeDataString(name)}").ConfigureAwait(false);
$"cards/search/{Uri.EscapeDataString(name)}");
var objs = JsonConvert.DeserializeObject<HearthstoneCardData[]>(response);
if (objs is null || objs.Length == 0)
return null;
@@ -400,7 +392,7 @@ public class SearchesService : INService
return null;
if (!string.IsNullOrWhiteSpace(data.Img))
{
data.Img = await _google.ShortenUrl(data.Img).ConfigureAwait(false);
data.Img = await _google.ShortenUrl(data.Img);
}
if (!string.IsNullOrWhiteSpace(data.Text))
{
@@ -429,11 +421,11 @@ public class SearchesService : INService
{
using var http = _httpFactory.CreateClient();
var res = await http.GetStringAsync(string.Format("https://omdbapi.nadeko.bot/?t={0}&y=&plot=full&r=json",
name.Trim().Replace(' ', '+'))).ConfigureAwait(false);
name.Trim().Replace(' ', '+')));
var movie = JsonConvert.DeserializeObject<OmdbMovie>(res);
if (movie?.Title is null)
return null;
movie.Poster = await _google.ShortenUrl(movie.Poster).ConfigureAwait(false);
movie.Poster = await _google.ShortenUrl(movie.Poster);
return movie;
}
@@ -442,7 +434,7 @@ public class SearchesService : INService
var redis = _cache.Redis;
var db = redis.GetDatabase();
const string STEAM_GAME_IDS_KEY = "steam_names_to_appid";
var exists = await db.KeyExistsAsync(STEAM_GAME_IDS_KEY).ConfigureAwait(false);
var exists = await db.KeyExistsAsync(STEAM_GAME_IDS_KEY);
// if we didn't get steam name to id map already, get it
//if (!exists)
@@ -450,12 +442,12 @@ public class SearchesService : INService
// using (var http = _httpFactory.CreateClient())
// {
// // https://api.steampowered.com/ISteamApps/GetAppList/v2/
// var gamesStr = await http.GetStringAsync("https://api.steampowered.com/ISteamApps/GetAppList/v2/").ConfigureAwait(false);
// var gamesStr = await http.GetStringAsync("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
// var apps = JsonConvert.DeserializeAnonymousType(gamesStr, new { applist = new { apps = new List<SteamGameId>() } }).applist.apps;
// //await db.HashSetAsync("steam_game_ids", apps.Select(app => new HashEntry(app.Name.Trim().ToLowerInvariant(), app.AppId)).ToArray()).ConfigureAwait(false);
// //await db.HashSetAsync("steam_game_ids", apps.Select(app => new HashEntry(app.Name.Trim().ToLowerInvariant(), app.AppId)).ToArray());
// await db.StringSetAsync("steam_game_ids", gamesStr, TimeSpan.FromHours(24));
// //await db.KeyExpireAsync("steam_game_ids", TimeSpan.FromHours(24), CommandFlags.FireAndForget).ConfigureAwait(false);
// //await db.KeyExpireAsync("steam_game_ids", TimeSpan.FromHours(24), CommandFlags.FireAndForget);
// }
//}
@@ -463,16 +455,16 @@ public class SearchesService : INService
{
using var http = _httpFactory.CreateClient();
// https://api.steampowered.com/ISteamApps/GetAppList/v2/
var gamesStr = await http.GetStringAsync("https://api.steampowered.com/ISteamApps/GetAppList/v2/").ConfigureAwait(false);
var gamesStr = await http.GetStringAsync("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
var apps = JsonConvert.DeserializeAnonymousType(gamesStr, new { applist = new { apps = new List<SteamGameId>() } }).applist.apps;
return apps
.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase)
.GroupBy(x => x.Name)
.ToDictionary(x => x.Key, x => x.First().AppId);
//await db.HashSetAsync("steam_game_ids", apps.Select(app => new HashEntry(app.Name.Trim().ToLowerInvariant(), app.AppId)).ToArray()).ConfigureAwait(false);
//await db.HashSetAsync("steam_game_ids", apps.Select(app => new HashEntry(app.Name.Trim().ToLowerInvariant(), app.AppId)).ToArray());
//await db.StringSetAsync("steam_game_ids", gamesStr, TimeSpan.FromHours(24));
//await db.KeyExpireAsync("steam_game_ids", TimeSpan.FromHours(24), CommandFlags.FireAndForget).ConfigureAwait(false);
//await db.KeyExpireAsync("steam_game_ids", TimeSpan.FromHours(24), CommandFlags.FireAndForget);
}, default(string), TimeSpan.FromHours(24));
if (gamesMap is null)
@@ -506,7 +498,7 @@ public class SearchesService : INService
// now that we have appid, get the game info with that appid
//var gameData = await _cache.GetOrAddCachedDataAsync($"steam_game:{appid}", SteamGameDataFactory, appid, TimeSpan.FromHours(12))
// .ConfigureAwait(false);
//;
//return gameData;
}
@@ -516,7 +508,7 @@ public class SearchesService : INService
// using (var http = _httpFactory.CreateClient())
// {
// // https://store.steampowered.com/api/appdetails?appids=
// var responseStr = await http.GetStringAsync($"https://store.steampowered.com/api/appdetails?appids={appid}").ConfigureAwait(false);
// var responseStr = await http.GetStringAsync($"https://store.steampowered.com/api/appdetails?appids={appid}");
// var data = JsonConvert.DeserializeObject<Dictionary<int, SteamGameData.Container>>(responseStr);
// if (!data.ContainsKey(appid) || !data[appid].Success)
// return null; // for some reason we can't get the game with valid appid. SHould never happen

View File

@@ -227,15 +227,14 @@ public sealed class StreamNotificationService : INService
var key = stream.CreateKey();
if (_shardTrackedStreams.TryGetValue(key, out var fss))
{
var sendTasks = fss
await fss
// send offline stream notifications only to guilds which enable it with .stoff
.SelectMany(x => x.Value)
.Where(x => _offlineNotificationServers.Contains(x.GuildId))
.Select(fs => _client.GetGuild(fs.GuildId)
?.GetTextChannel(fs.ChannelId)
?.EmbedAsync(GetEmbed(fs.GuildId, stream)));
await Task.WhenAll(sendTasks);
?.EmbedAsync(GetEmbed(fs.GuildId, stream)))
.WhenAll();
}
}
}
@@ -247,7 +246,7 @@ public sealed class StreamNotificationService : INService
var key = stream.CreateKey();
if (_shardTrackedStreams.TryGetValue(key, out var fss))
{
var sendTasks = fss
await fss
.SelectMany(x => x.Value)
.Select(fs =>
{
@@ -266,9 +265,8 @@ public sealed class StreamNotificationService : INService
: rep.Replace(fs.Message);
return textChannel.EmbedAsync(GetEmbed(fs.GuildId, stream), message);
});
await Task.WhenAll(sendTasks);
})
.WhenAll();
}
}
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
using System.Net;
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
@@ -102,7 +102,7 @@ public sealed class TranslateService : ITranslateService, ILateExecutor, IReadyE
if (string.IsNullOrWhiteSpace(text))
throw new ArgumentException("Text is empty or null", nameof(text));
var res = await _google.Translate(text, source, target).ConfigureAwait(false);
var res = await _google.Translate(text, source, target);
return res.SanitizeMentions(true);
}

View File

@@ -1,4 +1,4 @@
#nullable disable
#nullable disable
namespace NadekoBot.Modules.Searches.Services;
// public class YtTrackService : INService
@@ -31,7 +31,7 @@ namespace NadekoBot.Modules.Searches.Services;
// await Task.Delay(10000);
// using (var http = httpClientFactory.CreateClient())
// {
// await Task.WhenAll(followedChannels.Select(kvp => CheckChannel(kvp.Key, kvp.Value.SelectMany(x => x.Value).ToList())));
// await followedChannels.Select(kvp => CheckChannel(kvp.Key, kvp.Value.SelectMany(x => x.Value).ToList())).WhenAll();
// }
// }
// }