mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
dev: Removed discrim from the database
add: .translateflags command add: captcha to timely, configurable in .conf gambling change: change bonuses for patreon rewards fix: nunchi message color fix
This commit is contained in:
@@ -453,7 +453,6 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
{
|
||||
x.UserId,
|
||||
x.Username,
|
||||
x.Discriminator
|
||||
})
|
||||
.Where(x => users.Select(y => y.Id).Contains(x.UserId))
|
||||
.ToArrayAsyncEF();
|
||||
@@ -465,12 +464,11 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
UserId = x.Id,
|
||||
AvatarId = x.AvatarId,
|
||||
Username = x.Username,
|
||||
Discriminator = x.Discriminator
|
||||
});
|
||||
|
||||
var added = (await ctx.BulkCopyAsync(usersToAdd)).RowsCopied;
|
||||
var toUpdateUserIds = presentDbUsers
|
||||
.Where(x => x.Username == "Unknown" && x.Discriminator == "????")
|
||||
.Where(x => x.Username.StartsWith("??"))
|
||||
.Select(x => x.UserId)
|
||||
.ToArray();
|
||||
|
||||
@@ -481,7 +479,6 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, INService
|
||||
.UpdateAsync(x => new DiscordUser()
|
||||
{
|
||||
Username = user.Username,
|
||||
Discriminator = user.Discriminator,
|
||||
|
||||
// .award tends to set AvatarId and DateAdded to NULL, so account for that.
|
||||
AvatarId = user.AvatarId,
|
||||
|
@@ -603,7 +603,7 @@ public class WaifuService : INService, IReadyExecutor
|
||||
.Where(wi => wi.ClaimerId == waifuId)
|
||||
.Select(wi => wi.WaifuId)
|
||||
.Contains(x.Id))
|
||||
.Select(x => $"{x.Username}#{x.Discriminator}")
|
||||
.Select(x => x.Username)
|
||||
.ToListAsyncEF();
|
||||
}
|
||||
|
||||
@@ -615,7 +615,7 @@ public class WaifuService : INService, IReadyExecutor
|
||||
.Where(wi => wi.AffinityId == waifuId)
|
||||
.Select(wi => wi.WaifuId)
|
||||
.Contains(x.Id))
|
||||
.Select(x => $"{x.Username}#{x.Discriminator}")
|
||||
.Select(x => x.Username)
|
||||
.ToListAsyncEF();
|
||||
}
|
||||
|
||||
|
@@ -42,15 +42,12 @@ public static class WaifuExtensions
|
||||
{
|
||||
Affinity = x.Affinity == null
|
||||
? null
|
||||
: x.Affinity.Username
|
||||
+ (x.Affinity.Discriminator != "0000" ? "#" + x.Affinity.Discriminator : ""),
|
||||
: x.Affinity.Username,
|
||||
ClaimerName =
|
||||
x.Claimer == null
|
||||
? null
|
||||
: x.Claimer.Username
|
||||
+ (x.Claimer.Discriminator != "0000" ? "#" + x.Claimer.Discriminator : ""),
|
||||
WaifuName = x.Waifu.Username
|
||||
+ (x.Waifu.Discriminator != "0000" ? "#" + x.Waifu.Discriminator : ""),
|
||||
: x.Claimer.Username,
|
||||
WaifuName = x.Waifu.Username,
|
||||
Price = x.Price
|
||||
})
|
||||
.ToListAsyncEF();
|
||||
@@ -62,7 +59,7 @@ public static class WaifuExtensions
|
||||
public static ulong GetWaifuUserId(this DbSet<WaifuInfo> waifus, ulong ownerId, string name)
|
||||
=> waifus.AsQueryable()
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Claimer.UserId == ownerId && x.Waifu.Username + "#" + x.Waifu.Discriminator == name)
|
||||
.Where(x => x.Claimer.UserId == ownerId && x.Waifu.Username == name)
|
||||
.Select(x => x.Waifu.UserId)
|
||||
.FirstOrDefault();
|
||||
|
||||
@@ -100,7 +97,7 @@ public static class WaifuExtensions
|
||||
ctx.Set<DiscordUser>()
|
||||
.AsQueryable()
|
||||
.Where(u => u.UserId == userId)
|
||||
.Select(u => u.Username + "#" + u.Discriminator)
|
||||
.Select(u => u.Username)
|
||||
.FirstOrDefault(),
|
||||
AffinityCount =
|
||||
ctx.Set<WaifuUpdate>()
|
||||
@@ -112,14 +109,14 @@ public static class WaifuExtensions
|
||||
ctx.Set<DiscordUser>()
|
||||
.AsQueryable()
|
||||
.Where(u => u.Id == w.AffinityId)
|
||||
.Select(u => u.Username + "#" + u.Discriminator)
|
||||
.Select(u => u.Username)
|
||||
.FirstOrDefault(),
|
||||
ClaimCount = ctx.Set<WaifuInfo>().AsQueryable().Count(x => x.ClaimerId == w.WaifuId),
|
||||
ClaimerName =
|
||||
ctx.Set<DiscordUser>()
|
||||
.AsQueryable()
|
||||
.Where(u => u.Id == w.ClaimerId)
|
||||
.Select(u => u.Username + "#" + u.Discriminator)
|
||||
.Select(u => u.Username)
|
||||
.FirstOrDefault(),
|
||||
DivorceCount =
|
||||
ctx.Set<WaifuUpdate>()
|
||||
|
@@ -29,7 +29,7 @@ public partial class Games
|
||||
if (!await nunchi.Join(ctx.User.Id, ctx.User.ToString()))
|
||||
return;
|
||||
|
||||
await Response().Error(strs.nunchi_joined(nunchi.ParticipantCount)).SendAsync();
|
||||
await Response().Confirm(strs.nunchi_joined(nunchi.ParticipantCount)).SendAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@@ -122,11 +122,11 @@ public sealed class CurrencyRewardService : INService, IReadyExecutor
|
||||
var dollarValue = pledgeCents / 100;
|
||||
percentBonus = dollarValue switch
|
||||
{
|
||||
>= 100 => 100,
|
||||
>= 50 => 50,
|
||||
>= 20 => 20,
|
||||
>= 10 => 10,
|
||||
>= 5 => 5,
|
||||
>= 100 => 20,
|
||||
>= 50 => 10,
|
||||
>= 20 => 5,
|
||||
>= 10 => 3,
|
||||
>= 5 => 1,
|
||||
_ => 0
|
||||
};
|
||||
return (long)(modifiedAmount * (1 + (percentBonus / 100.0f)));
|
||||
|
191
src/NadekoBot/Modules/Searches/Translate/FlagTranslateService.cs
Normal file
191
src/NadekoBot/Modules/Searches/Translate/FlagTranslateService.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
#nullable disable
|
||||
using LinqToDB;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using NadekoBot.Common.ModuleBehaviors;
|
||||
using NadekoBot.Db.Models;
|
||||
using System.Collections.Frozen;
|
||||
|
||||
namespace NadekoBot.Modules.Searches;
|
||||
|
||||
public sealed partial class FlagTranslateService : IReadyExecutor, INService
|
||||
{
|
||||
private readonly IBotCreds _creds;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly TranslateService _ts;
|
||||
private readonly IMessageSenderService _sender;
|
||||
private IReadOnlyDictionary<string, string> _supportedFlags;
|
||||
private readonly DbService _db;
|
||||
private ConcurrentHashSet<ulong> _enabledChannels;
|
||||
private readonly IBotCache _cache;
|
||||
|
||||
// disallow same message being translated multiple times to the same language
|
||||
private readonly ConcurrentHashSet<(ulong, string)> _msgLangs = new();
|
||||
|
||||
public FlagTranslateService(
|
||||
IBotCreds creds,
|
||||
DiscordSocketClient client,
|
||||
TranslateService ts,
|
||||
IMessageSenderService sender,
|
||||
DbService db,
|
||||
IBotCache cache)
|
||||
{
|
||||
_creds = creds;
|
||||
_client = client;
|
||||
_ts = ts;
|
||||
_sender = sender;
|
||||
_db = db;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task OnReadyAsync()
|
||||
{
|
||||
_supportedFlags = COUNTRIES
|
||||
.Split('\n')
|
||||
.Select(x => x.Split(' '))
|
||||
.ToDictionary(x => x[0], x => x[1].TrimEnd())
|
||||
.ToFrozenDictionary();
|
||||
|
||||
await using (var uow = _db.GetDbContext())
|
||||
{
|
||||
_enabledChannels = (await uow.GetTable<FlagTranslateChannel>()
|
||||
.Where(x => Linq2DbExpressions.GuildOnShard(x.GuildId,
|
||||
_creds.TotalShards,
|
||||
_client.ShardId))
|
||||
.Select(x => new
|
||||
{
|
||||
x.ChannelId,
|
||||
x.GuildId
|
||||
})
|
||||
.ToListAsyncLinqToDB())
|
||||
.Select(x => x.ChannelId)
|
||||
.ToHashSet()
|
||||
.ToConcurrentSet();
|
||||
}
|
||||
|
||||
_client.ReactionAdded += OnReactionAdded;
|
||||
|
||||
var periodicCleanup = new PeriodicTimer(TimeSpan.FromHours(24));
|
||||
|
||||
while (await periodicCleanup.WaitForNextTickAsync())
|
||||
{
|
||||
_msgLangs.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private const int FLAG_START = 127462;
|
||||
|
||||
private static TypedKey<bool> CdKey(ulong userId)
|
||||
=> new($"flagtranslate:{userId}");
|
||||
|
||||
private Task OnReactionAdded(
|
||||
Cacheable<IUserMessage, ulong> arg1,
|
||||
Cacheable<IMessageChannel, ulong> arg2,
|
||||
SocketReaction reaction)
|
||||
{
|
||||
if (!_enabledChannels.Contains(reaction.Channel.Id))
|
||||
return Task.CompletedTask;
|
||||
|
||||
var runes = reaction.Emote.Name.EnumerateRunes();
|
||||
if (!runes.MoveNext()
|
||||
|| runes.Current is not { Value: >= 127462 and <= 127487 } l1
|
||||
|| !runes.MoveNext()
|
||||
|| runes.Current is not { Value: >= 127462 and <= 127487 } l2)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
if (reaction.Channel is not SocketTextChannel tc)
|
||||
return;
|
||||
|
||||
var user = await ((IGuild)tc.Guild).GetUserAsync(reaction.UserId);
|
||||
|
||||
if (user is null)
|
||||
return;
|
||||
|
||||
if (!user.GetPermissions(tc).SendMessages)
|
||||
return;
|
||||
|
||||
if (!tc.Guild.CurrentUser.GetPermissions(tc).SendMessages
|
||||
|| !tc.Guild.CurrentUser.GetPermissions(tc).EmbedLinks)
|
||||
{
|
||||
await Disable(tc.Guild.Id, tc.Id);
|
||||
return;
|
||||
}
|
||||
|
||||
var c1 = (char)(l1.Value - FLAG_START + 65);
|
||||
var c2 = (char)(l2.Value - FLAG_START + 65);
|
||||
|
||||
var code = $"{c1}{c2}".ToUpper();
|
||||
|
||||
if (!_supportedFlags.TryGetValue(code, out var lang))
|
||||
return;
|
||||
|
||||
if (!_msgLangs.Add((reaction.MessageId, lang)))
|
||||
return;
|
||||
|
||||
var result = await _cache.GetAsync(CdKey(reaction.UserId));
|
||||
if (result.TryPickT0(out _, out _))
|
||||
return;
|
||||
|
||||
await _cache.AddAsync(CdKey(reaction.UserId), true, TimeSpan.FromSeconds(5));
|
||||
|
||||
var msg = await arg1.GetOrDownloadAsync();
|
||||
|
||||
var response = await _ts.Translate("", lang, msg.Content).ConfigureAwait(false);
|
||||
|
||||
await msg.ReplyAsync(embed: _sender.CreateEmbed()
|
||||
.WithOkColor()
|
||||
.WithFooter(user.ToString() ?? reaction.UserId.ToString(),
|
||||
user.RealAvatarUrl().ToString())
|
||||
.WithDescription(response)
|
||||
.WithAuthor(reaction.Emote.ToString())
|
||||
.Build(),
|
||||
allowedMentions: AllowedMentions.None
|
||||
);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task Disable(ulong guildId, ulong tcId)
|
||||
{
|
||||
if (!_enabledChannels.TryRemove(tcId))
|
||||
return;
|
||||
|
||||
await using var uow = _db.GetDbContext();
|
||||
await uow.GetTable<FlagTranslateChannel>()
|
||||
.Where(x => x.GuildId == guildId
|
||||
&& x.ChannelId == tcId)
|
||||
.DeleteAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> Toggle(ulong guildId, ulong tcId)
|
||||
{
|
||||
if (_enabledChannels.Contains(tcId))
|
||||
{
|
||||
await Disable(guildId, tcId);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
await Enable(guildId, tcId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task Enable(ulong guildId, ulong tcId)
|
||||
{
|
||||
if (!_enabledChannels.Add(tcId))
|
||||
return;
|
||||
|
||||
await using var uow = _db.GetDbContext();
|
||||
await uow.GetTable<FlagTranslateChannel>()
|
||||
.InsertAsync(() => new FlagTranslateChannel
|
||||
{
|
||||
GuildId = guildId,
|
||||
ChannelId = tcId
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
namespace NadekoBot.Modules.Searches;
|
||||
|
||||
public partial class FlagTranslateService
|
||||
{
|
||||
private const string COUNTRIES = """
|
||||
CN zh
|
||||
IN hi
|
||||
US en
|
||||
ID id
|
||||
PK ur
|
||||
BR pt
|
||||
NG ha
|
||||
BD bn
|
||||
RU ru
|
||||
JP ja
|
||||
MX es
|
||||
PH tl
|
||||
VN vi
|
||||
EG ar
|
||||
ET am
|
||||
DE de
|
||||
IR fa
|
||||
TR tr
|
||||
TH th
|
||||
FR fr
|
||||
CD fr
|
||||
MM my
|
||||
UG en
|
||||
MZ pt
|
||||
ZA zu
|
||||
CO es
|
||||
BG bg
|
||||
HR hr
|
||||
MY ms
|
||||
NL nl
|
||||
RO ro
|
||||
CZ cs
|
||||
GR el
|
||||
SK sk
|
||||
PT pt
|
||||
KR ko
|
||||
IT it
|
||||
ES es
|
||||
RS sr
|
||||
TN ar
|
||||
PL pl
|
||||
SD ar
|
||||
CM fr
|
||||
SN fr
|
||||
ML fr
|
||||
NE ha
|
||||
BI fr
|
||||
AO pt
|
||||
AF ps
|
||||
MA ar
|
||||
DZ ar
|
||||
GB en
|
||||
AR es
|
||||
ZW ny
|
||||
KE sw
|
||||
GH en
|
||||
SA ar
|
||||
IL he
|
||||
IQ ar
|
||||
UA ua
|
||||
LY ar
|
||||
KW ar
|
||||
OM ar
|
||||
YE ar
|
||||
AL sq
|
||||
AE ar
|
||||
""";
|
||||
}
|
@@ -44,12 +44,10 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
|
||||
foreach (var c in cs)
|
||||
{
|
||||
_atcs[c.ChannelId] = c.AutoDelete;
|
||||
_users[c.ChannelId] =
|
||||
new(c.Users.ToDictionary(x => x.UserId, x => (x.Source.ToLower(), x.Target.ToLower())));
|
||||
_users[c.ChannelId] = new(c.Users.ToDictionary(x => x.UserId, x => (x.Source.ToLower(), x.Target.ToLower())));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(msg.Content))
|
||||
@@ -95,7 +93,7 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> Translate(string source, string target, string text = null)
|
||||
public async Task<string> Translate(string source, string target, string text)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
throw new ArgumentException("Text is empty or null", nameof(text));
|
||||
|
@@ -6,6 +6,14 @@ public partial class Searches
|
||||
[Group]
|
||||
public partial class TranslateCommands : NadekoModule<ITranslateService>
|
||||
{
|
||||
private readonly FlagTranslateService _flagSvc;
|
||||
|
||||
public TranslateCommands(FlagTranslateService flagSvc)
|
||||
{
|
||||
_flagSvc = flagSvc;
|
||||
}
|
||||
|
||||
|
||||
public enum AutoDeleteAutoTranslate
|
||||
{
|
||||
Del,
|
||||
@@ -91,5 +99,18 @@ public partial class Searches
|
||||
|
||||
await Response().Embed(eb).SendAsync();
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
[RequireContext(ContextType.Guild)]
|
||||
[UserPerm(ChannelPermission.ManageChannels)]
|
||||
[BotPerm(ChannelPermission.SendMessages | ChannelPermission.EmbedLinks)]
|
||||
public async Task TranslateFlags()
|
||||
{
|
||||
var enabled = await _flagSvc.Toggle(ctx.Guild.Id, ctx.Channel.Id);
|
||||
if (enabled)
|
||||
await Response().Confirm(strs.trfl_enabled).SendAsync();
|
||||
else
|
||||
await Response().Confirm(strs.trfl_disabled).SendAsync();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user