- you should be able to update your .atl now without disabling it

- capitalization of language input in .atl should no longer matter
This commit is contained in:
Kwoth
2021-12-17 17:08:05 +01:00
parent a2b25f8246
commit 14f2851072
6 changed files with 42 additions and 21 deletions

View File

@@ -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<AutoTranslateChannel> GetByChannelId(this IQueryable<AutoTranslateChannel> set, ulong channelId)
=> set.FirstOrDefaultAsyncLinqToDB(x => x.ChannelId == channelId);
=> set
.Include(x => x.Users)
.FirstOrDefaultAsyncEF(x => x.ChannelId == channelId);
}
}

View File

@@ -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<ulong, (string, string)>());
dict[userId] = (from, to);
}
public async Task<bool?> 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<ulong, (string, string)>());
dict[userId] = (from, to);
UpdateUser(channelId, userId, from, to);
return true;
}
ctx.AutoTranslateUsers.Remove(user);
await ctx.SaveChangesAsync();
// if it's different from old settings, update
if (user.Source != from || user.Target != to)
{
user.Source = from;
user.Target = to;
if (_users.TryGetValue(channelId, out var inner))
inner.TryRemove(userId, out _);
await ctx.SaveChangesAsync();
return true;
UpdateUser(channelId, userId, from, to);
return true;
}
return await UnregisterUser(channelId, userId);
}
public async Task<bool> UnregisterUser(ulong channelId, ulong userId)
@@ -216,6 +232,6 @@ namespace NadekoBot.Modules.Searches
return rows > 0;
}
public IEnumerable<string> GetLanguages() => _google.Languages;
public IEnumerable<string> GetLanguages() => _google.Languages.Select(x => x.Key);
}
}

View File

@@ -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)
{

View File

@@ -7,7 +7,7 @@ namespace NadekoBot.Services
{
public interface IGoogleApiService : INService
{
IEnumerable<string> Languages { get; }
IReadOnlyDictionary<string, string> Languages { get; }
Task<IEnumerable<string>> GetVideoLinksByKeywordAsync(string keywords, int count = 1);
Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1);

View File

@@ -226,8 +226,7 @@ namespace NadekoBot.Services
return new ImageResult(search.Items[0].Image, search.Items[0].Link);
}
public IEnumerable<string> Languages => _languageDictionary.Keys.OrderBy(x => x);
private readonly Dictionary<string, string> _languageDictionary = new Dictionary<string, string>() {
public IReadOnlyDictionary<string, string> Languages { get; } = new Dictionary<string, string>() {
{ "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;
}
}

View File

@@ -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",