NadekoBot Patronage system, Search commands improvements + fixes

This commit is contained in:
Kwoth
2022-06-14 07:24:33 +00:00
parent 18b10b8c6f
commit 7b5145f116
165 changed files with 14920 additions and 1457 deletions

View File

@@ -10,7 +10,7 @@ public static class CurrencyServiceExtensions
return await wallet.GetBalance();
}
// todo transfer should be a transaction
// FUTURE should be a transaction
public static async Task<bool> TransferAsync(
this ICurrencyService cs,
IEmbedBuilderService ebs,

View File

@@ -1,5 +1,4 @@
#nullable disable
using Google.Apis.Customsearch.v1.Data;
namespace NadekoBot.Services;
@@ -13,21 +12,8 @@ public interface IGoogleApiService
Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1, string user = null);
Task<IEnumerable<string>> GetPlaylistTracksAsync(string playlistId, int count = 50);
Task<IReadOnlyDictionary<string, TimeSpan>> GetVideoDurationsAsync(IEnumerable<string> videoIds);
Task<ImageResult> GetImageAsync(string query);
Task<string> Translate(string sourceText, string sourceLanguage, string targetLanguage);
Task<string> ShortenUrl(string url);
Task<string> ShortenUrl(Uri url);
}
public struct ImageResult
{
public Result.ImageData Image { get; }
public string Link { get; }
public ImageResult(Result.ImageData image, string link)
{
Image = image;
Link = link;
}
}

View File

@@ -112,8 +112,6 @@ public sealed class BotCredsProvider : IBotCredsProvider
ymlData = Yaml.Serializer.Serialize(creds);
File.WriteAllText(CREDS_FILE_NAME, ymlData);
Reload();
}
private string OldCredsJsonPath
@@ -174,5 +172,10 @@ public sealed class BotCredsProvider : IBotCredsProvider
}
public IBotCredentials GetCreds()
=> _creds;
{
lock (_reloadLock)
{
return _creds;
}
}
}

View File

@@ -1,6 +1,5 @@
#nullable disable
using Google;
using Google.Apis.Customsearch.v1;
using Google.Apis.Services;
using Google.Apis.Urlshortener.v1;
using Google.Apis.YouTube.v3;
@@ -13,8 +12,6 @@ namespace NadekoBot.Services;
public class GoogleApiService : IGoogleApiService, INService
{
private const string SEARCH_ENGINE_ID = "018084019232060951019:hs5piey28-e";
private static readonly Regex
_plRegex = new("(?:youtu\\.be\\/|list=)(?<id>[\\da-zA-Z\\-_]*)", RegexOptions.Compiled);
@@ -153,13 +150,12 @@ public class GoogleApiService : IGoogleApiService, INService
private readonly YouTubeService _yt;
private readonly UrlshortenerService _sh;
private readonly CustomsearchService _cs;
//private readonly Regex YtVideoIdRegex = new Regex(@"(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|youtu\.be\/)(?<id>[a-zA-Z0-9_-]{6,11})", RegexOptions.Compiled);
private readonly IBotCredentials _creds;
private readonly IBotCredsProvider _creds;
private readonly IHttpClientFactory _httpFactory;
public GoogleApiService(IBotCredentials creds, IHttpClientFactory factory)
public GoogleApiService(IBotCredsProvider creds, IHttpClientFactory factory)
{
_creds = creds;
_httpFactory = factory;
@@ -167,12 +163,11 @@ public class GoogleApiService : IGoogleApiService, INService
var bcs = new BaseClientService.Initializer
{
ApplicationName = "Nadeko Bot",
ApiKey = _creds.GoogleApiKey
ApiKey = _creds.GetCreds().GoogleApiKey
};
_yt = new(bcs);
_sh = new(bcs);
_cs = new(bcs);
}
public async Task<IEnumerable<string>> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1)
@@ -207,7 +202,7 @@ public class GoogleApiService : IGoogleApiService, INService
query.RelatedToVideoId = id;
query.Type = "video";
query.QuotaUser = user;
return (await query.ExecuteAsync()).Items.Select(i => "http://www.youtube.com/watch?v=" + i.Id.VideoId);
return (await query.ExecuteAsync()).Items.Select(i => "https://www.youtube.com/watch?v=" + i.Id.VideoId);
}
public async Task<IEnumerable<string>> GetVideoLinksByKeywordAsync(string keywords, int count = 1)
@@ -223,7 +218,7 @@ public class GoogleApiService : IGoogleApiService, INService
query.Q = keywords;
query.Type = "video";
query.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.Strict;
return (await query.ExecuteAsync()).Items.Select(i => "http://www.youtube.com/watch?v=" + i.Id.VideoId);
return (await query.ExecuteAsync()).Items.Select(i => "https://www.youtube.com/watch?v=" + i.Id.VideoId);
}
public async Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(
@@ -241,7 +236,7 @@ public class GoogleApiService : IGoogleApiService, INService
query.Q = keywords;
query.Type = "video";
return (await query.ExecuteAsync()).Items.Select(i
=> (i.Snippet.Title.TrimTo(50), i.Id.VideoId, "http://www.youtube.com/watch?v=" + i.Id.VideoId));
=> (i.Snippet.Title.TrimTo(50), i.Id.VideoId, "https://www.youtube.com/watch?v=" + i.Id.VideoId));
}
public Task<string> ShortenUrl(Uri url)
@@ -252,7 +247,7 @@ public class GoogleApiService : IGoogleApiService, INService
if (string.IsNullOrWhiteSpace(url))
throw new ArgumentNullException(nameof(url));
if (string.IsNullOrWhiteSpace(_creds.GoogleApiKey))
if (string.IsNullOrWhiteSpace(_creds.GetCreds().GoogleApiKey))
return url;
try
@@ -332,25 +327,6 @@ public class GoogleApiService : IGoogleApiService, INService
return toReturn;
}
public async Task<ImageResult> GetImageAsync(string query)
{
if (string.IsNullOrWhiteSpace(query))
throw new ArgumentNullException(nameof(query));
var req = _cs.Cse.List();
req.Q = query;
req.Cx = SEARCH_ENGINE_ID;
req.Num = 1;
req.Fields = "items(image(contextLink,thumbnailLink),link)";
req.SearchType = CseResource.ListRequest.SearchTypeEnum.Image;
req.Start = new NadekoRandom().Next(0, 20);
req.Safe = CseResource.ListRequest.SafeEnum.Active;
var search = await req.ExecuteAsync();
return new(search.Items[0].Image, search.Items[0].Link);
}
public async Task<string> Translate(string sourceText, string sourceLanguage, string targetLanguage)
{
string text;
@@ -379,4 +355,5 @@ public class GoogleApiService : IGoogleApiService, INService
Languages.TryGetValue(language, out var mode);
return mode;
}
}
}

View File

@@ -164,6 +164,15 @@ public sealed class RedisImagesCache : IImageCache, IReadyExecutor
localImageUrls.Version = 3;
File.WriteAllText(_imagesPath, Yaml.Serializer.Serialize(localImageUrls));
}
if (localImageUrls.Version == 3)
{
localImageUrls.Version = 4;
if (localImageUrls.Xp?.Bg.ToString() == "https://cdn.nadeko.bot/other/xp/bg.png")
localImageUrls.Xp.Bg = new("https://cdn.nadeko.bot/other/xp/bg_k.png");
File.WriteAllText(_imagesPath, Yaml.Serializer.Serialize(localImageUrls));
}
}
public async Task Reload()

View File

@@ -7,7 +7,7 @@ namespace NadekoBot.Services;
public sealed class StatsService : IStatsService, IReadyExecutor, INService
{
public const string BOT_VERSION = "4.1.6";
public const string BOT_VERSION = "4.2.0";
public string Author
=> "Kwoth#2452";

View File

@@ -8,9 +8,13 @@ namespace NadekoBot.Services;
public class YtdlOperation
{
private readonly string _baseArgString;
private readonly bool _isYtDlp;
public YtdlOperation(string baseArgString)
=> _baseArgString = baseArgString;
public YtdlOperation(string baseArgString, bool isYtDlp = false)
{
_baseArgString = baseArgString;
_isYtDlp = isYtDlp;
}
private Process CreateProcess(string[] args)
{
@@ -19,7 +23,7 @@ public class YtdlOperation
{
StartInfo = new()
{
FileName = "youtube-dl",
FileName = _isYtDlp ? "yt-dlp" : "youtube-dl",
Arguments = string.Format(_baseArgString, newArgs),
UseShellExecute = false,
RedirectStandardError = true,

View File

@@ -1,4 +1,5 @@
#nullable disable
using NadekoBot.Modules.Searches;
using SixLabors.ImageSharp.PixelFormats;
using System.Globalization;
@@ -31,6 +32,10 @@ public static class ConfigParsers
return false;
}
}
public static bool InsensitiveEnum<T>(string input, out T output)
where T: struct
=> Enum.TryParse(input, true, out output);
}
public static class ConfigPrinters

View File

@@ -12,7 +12,7 @@ namespace NadekoBot.Services;
public abstract class ConfigServiceBase<TSettings> : IConfigService
where TSettings : ICloneable<TSettings>, new()
{
// todo future config arrays are not copied - they're not protected from mutations
// FUTURE config arrays are not copied - they're not protected from mutations
public TSettings Data
=> data.Clone();