From 14f2851072bc7ea38f5f0e6bb8b31914a20a911d Mon Sep 17 00:00:00 2001 From: Kwoth Date: Fri, 17 Dec 2021 17:08:05 +0100 Subject: [PATCH] - you should be able to update your .atl now without disabling it - capitalization of language input in .atl should no longer matter --- .../Searches/Services/AtlExtensions.cs | 5 ++- .../Searches/Services/TranslateService.cs | 40 +++++++++++++------ .../Modules/Searches/TranslatorCommands.cs | 5 ++- src/NadekoBot/Services/IGoogleApiService.cs | 2 +- .../Services/Impl/GoogleApiService.cs | 9 ++--- .../strings/responses/responses.en-US.json | 2 +- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/NadekoBot/Modules/Searches/Services/AtlExtensions.cs b/src/NadekoBot/Modules/Searches/Services/AtlExtensions.cs index 0de1cd452..6890c1685 100644 --- a/src/NadekoBot/Modules/Searches/Services/AtlExtensions.cs +++ b/src/NadekoBot/Modules/Searches/Services/AtlExtensions.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Threading.Tasks; using LinqToDB.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using NadekoBot.Services.Database.Models; namespace NadekoBot.Modules.Searches @@ -8,6 +9,8 @@ namespace NadekoBot.Modules.Searches public static class AtlExtensions { public static Task GetByChannelId(this IQueryable set, ulong channelId) - => set.FirstOrDefaultAsyncLinqToDB(x => x.ChannelId == channelId); + => set + .Include(x => x.Users) + .FirstOrDefaultAsyncEF(x => x.ChannelId == channelId); } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/Services/TranslateService.cs b/src/NadekoBot/Modules/Searches/Services/TranslateService.cs index 55cd6ebfa..f383b3172 100644 --- a/src/NadekoBot/Modules/Searches/Services/TranslateService.cs +++ b/src/NadekoBot/Modules/Searches/Services/TranslateService.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; +using AngleSharp.Common; using Discord; using Discord.Net; using LinqToDB; @@ -12,6 +13,7 @@ using Microsoft.EntityFrameworkCore; using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Extensions; using NadekoBot.Services; +using NadekoBot.Services.Database; namespace NadekoBot.Modules.Searches { @@ -49,7 +51,7 @@ namespace NadekoBot.Modules.Searches foreach (var c in cs) { _atcs[c.ChannelId] = c.AutoDelete; - _users[c.ChannelId] = new(c.Users.ToDictionary(x => x.UserId, x => (x.Source, x.Target))); + _users[c.ChannelId] = new(c.Users.ToDictionary(x => x.UserId, x => (x.Source.ToLower(), x.Target.ToLower()))); } } @@ -162,11 +164,19 @@ namespace NadekoBot.Modules.Searches } + private void UpdateUser(ulong channelId, ulong userId, string from, string to) + { + var dict = _users.GetOrAdd(channelId, new ConcurrentDictionary()); + dict[userId] = (from, to); + } + public async Task RegisterUserAsync(ulong userId, ulong channelId, string from, string to) { + if (!_google.Languages.ContainsKey(from) || !_google.Languages.ContainsKey(to)) + return null; + var ctx = _db.GetDbContext(); var ch = await ctx.AutoTranslateChannels - .ToLinqToDBTable() .GetByChannelId(channelId); if (ch is null) @@ -186,19 +196,25 @@ namespace NadekoBot.Modules.Searches await ctx.SaveChangesAsync(); - var dict = _users.GetOrAdd(channelId, new ConcurrentDictionary()); - dict[userId] = (from, to); + UpdateUser(channelId, userId, from, to); + + return true; + } + + // if it's different from old settings, update + if (user.Source != from || user.Target != to) + { + user.Source = from; + user.Target = to; + + await ctx.SaveChangesAsync(); + UpdateUser(channelId, userId, from, to); + return true; } - ctx.AutoTranslateUsers.Remove(user); - await ctx.SaveChangesAsync(); - - if (_users.TryGetValue(channelId, out var inner)) - inner.TryRemove(userId, out _); - - return true; + return await UnregisterUser(channelId, userId); } public async Task UnregisterUser(ulong channelId, ulong userId) @@ -216,6 +232,6 @@ namespace NadekoBot.Modules.Searches return rows > 0; } - public IEnumerable GetLanguages() => _google.Languages; + public IEnumerable GetLanguages() => _google.Languages.Select(x => x.Key); } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/TranslatorCommands.cs b/src/NadekoBot/Modules/Searches/TranslatorCommands.cs index 1372dbca9..f8957891d 100644 --- a/src/NadekoBot/Modules/Searches/TranslatorCommands.cs +++ b/src/NadekoBot/Modules/Searches/TranslatorCommands.cs @@ -70,7 +70,10 @@ namespace NadekoBot.Modules.Searches [RequireContext(ContextType.Guild)] public async Task AutoTransLang(string from, string to) { - var succ = await _service.RegisterUserAsync(ctx.User.Id, ctx.Channel.Id, from, to); + var succ = await _service.RegisterUserAsync(ctx.User.Id, + ctx.Channel.Id, + from.ToLower(), + to.ToLower()); if (succ is null) { diff --git a/src/NadekoBot/Services/IGoogleApiService.cs b/src/NadekoBot/Services/IGoogleApiService.cs index 6aa93730c..79a378752 100644 --- a/src/NadekoBot/Services/IGoogleApiService.cs +++ b/src/NadekoBot/Services/IGoogleApiService.cs @@ -7,7 +7,7 @@ namespace NadekoBot.Services { public interface IGoogleApiService : INService { - IEnumerable Languages { get; } + IReadOnlyDictionary Languages { get; } Task> GetVideoLinksByKeywordAsync(string keywords, int count = 1); Task> GetVideoInfosByKeywordAsync(string keywords, int count = 1); diff --git a/src/NadekoBot/Services/Impl/GoogleApiService.cs b/src/NadekoBot/Services/Impl/GoogleApiService.cs index eac1dc05c..aa666ddf8 100644 --- a/src/NadekoBot/Services/Impl/GoogleApiService.cs +++ b/src/NadekoBot/Services/Impl/GoogleApiService.cs @@ -226,8 +226,7 @@ namespace NadekoBot.Services return new ImageResult(search.Items[0].Image, search.Items[0].Link); } - public IEnumerable Languages => _languageDictionary.Keys.OrderBy(x => x); - private readonly Dictionary _languageDictionary = new Dictionary() { + public IReadOnlyDictionary Languages { get; } = new Dictionary() { { "afrikaans", "af"}, { "albanian", "sq"}, { "arabic", "ar"}, @@ -365,8 +364,8 @@ namespace NadekoBot.Services await Task.Yield(); string text; - if (!_languageDictionary.ContainsKey(sourceLanguage) || - !_languageDictionary.ContainsKey(targetLanguage)) + if (!Languages.ContainsKey(sourceLanguage) || + !Languages.ContainsKey(targetLanguage)) throw new ArgumentException(nameof(sourceLanguage) + "/" + nameof(targetLanguage)); @@ -385,7 +384,7 @@ namespace NadekoBot.Services private string ConvertToLanguageCode(string language) { - _languageDictionary.TryGetValue(language, out var mode); + Languages.TryGetValue(language, out var mode); return mode; } } diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index 8da675583..ba46f056a 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -450,7 +450,7 @@ "atl_set": "Your auto-translate language has been set to {0}>{1}", "atl_started": "Started automatic translation of messages on this channel.", "atl_stopped": "Stopped automatic translation of messages on this channel.", - "atl_not_enabled": "Automatic translation is not enabled on this channel.", + "atl_not_enabled": "Automatic translation is not enabled on this channel or you've provided an invalid language.", "bad_input_format": "Bad input format, or something went wrong.", "card_not_found": "Couldn't find that card.", "catfact": "fact",