- 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;
}
// 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<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)
{