More convenience methods for the dashy

This commit is contained in:
Kwoth
2024-04-23 09:12:24 +00:00
parent cc060da1a7
commit c9fb0a8b03
7 changed files with 96 additions and 70 deletions

View File

@@ -39,5 +39,5 @@ public interface ICurrencyService
long amount, long amount,
TxData? txData); TxData? txData);
Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int count); Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int page = 0, int perPage = 9);
} }

View File

@@ -94,10 +94,10 @@ public sealed class CurrencyService : ICurrencyService, INService
{ {
if (amount == 0) if (amount == 0)
return true; return true;
var wallet = await GetWalletAsync(userId); var wallet = await GetWalletAsync(userId);
var result = await wallet.Take(amount, txData); var result = await wallet.Take(amount, txData);
if(result) if (result)
await _txTracker.TrackRemove(amount, txData); await _txTracker.TrackRemove(amount, txData);
return result; return result;
} }
@@ -108,9 +108,9 @@ public sealed class CurrencyService : ICurrencyService, INService
TxData txData) TxData txData)
=> await RemoveAsync(user.Id, amount, txData); => await RemoveAsync(user.Id, amount, txData);
public async Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int count) public async Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int page = 0, int perPage = 9)
{ {
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
return await uow.Set<DiscordUser>().GetTopRichest(ignoreId, count); return await uow.Set<DiscordUser>().GetTopRichest(ignoreId, page, perPage);
} }
} }

View File

@@ -12,7 +12,7 @@ public static class DiscordUserExtensions
this IQueryable<DiscordUser> set, this IQueryable<DiscordUser> set,
ulong userId) ulong userId)
=> set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId); => set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId);
public static void EnsureUserCreated( public static void EnsureUserCreated(
this DbContext ctx, this DbContext ctx,
ulong userId, ulong userId,
@@ -20,50 +20,49 @@ public static class DiscordUserExtensions
string discrim, string discrim,
string avatarId) string avatarId)
=> ctx.GetTable<DiscordUser>() => ctx.GetTable<DiscordUser>()
.InsertOrUpdate( .InsertOrUpdate(
() => new() () => new()
{ {
UserId = userId, UserId = userId,
Username = username, Username = username,
Discriminator = discrim, Discriminator = discrim,
AvatarId = avatarId, AvatarId = avatarId,
TotalXp = 0, TotalXp = 0,
CurrencyAmount = 0 CurrencyAmount = 0
}, },
old => new() old => new()
{ {
Username = username, Username = username,
Discriminator = discrim, Discriminator = discrim,
AvatarId = avatarId AvatarId = avatarId
}, },
() => new() () => new()
{ {
UserId = userId UserId = userId
}); });
public static Task EnsureUserCreatedAsync( public static Task EnsureUserCreatedAsync(
this DbContext ctx, this DbContext ctx,
ulong userId) ulong userId)
=> ctx.GetTable<DiscordUser>() => ctx.GetTable<DiscordUser>()
.InsertOrUpdateAsync( .InsertOrUpdateAsync(
() => new() () => new()
{ {
UserId = userId, UserId = userId,
Username = "Unknown", Username = "Unknown",
Discriminator = "????", Discriminator = "????",
AvatarId = string.Empty, AvatarId = string.Empty,
TotalXp = 0, TotalXp = 0,
CurrencyAmount = 0 CurrencyAmount = 0
}, },
old => new() old => new()
{ {
},
() => new()
{
UserId = userId
});
},
() => new()
{
UserId = userId
});
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown //temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
public static DiscordUser GetOrCreateUser( public static DiscordUser GetOrCreateUser(
this DbContext ctx, this DbContext ctx,
@@ -84,25 +83,25 @@ public static class DiscordUserExtensions
public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id) public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id)
=> users.AsQueryable() => users.AsQueryable()
.Where(x => x.TotalXp .Where(x => x.TotalXp
> users.AsQueryable().Where(y => y.UserId == id).Select(y => y.TotalXp).FirstOrDefault()) > users.AsQueryable().Where(y => y.UserId == id).Select(y => y.TotalXp).FirstOrDefault())
.Count() .Count()
+ 1; + 1;
public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page) public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page, int perPage)
=> users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * 9).Take(9).AsEnumerable().ToArray(); => users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * perPage).Take(perPage).AsEnumerable()
.ToArray();
public static Task<List<DiscordUser>> GetTopRichest( public static Task<List<DiscordUser>> GetTopRichest(
this DbSet<DiscordUser> users, this DbSet<DiscordUser> users,
ulong botId, ulong botId,
int count, int page = 0, int perPage = 9)
int page = 0)
=> users.AsQueryable() => users.AsQueryable()
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId) .Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
.OrderByDescending(c => c.CurrencyAmount) .OrderByDescending(c => c.CurrencyAmount)
.Skip(page * 9) .Skip(page * perPage)
.Take(count) .Take(perPage)
.ToListAsyncLinqToDB(); .ToListAsyncLinqToDB();
public static async Task<long> GetUserCurrencyAsync(this DbSet<DiscordUser> users, ulong userId) public static async Task<long> GetUserCurrencyAsync(this DbSet<DiscordUser> users, ulong userId)
=> (await users.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId))?.CurrencyAmount ?? 0; => (await users.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId))?.CurrencyAmount ?? 0;
@@ -119,8 +118,8 @@ public static class DiscordUserExtensions
public static decimal GetTopOnePercentCurrency(this DbSet<DiscordUser> users, ulong botId) public static decimal GetTopOnePercentCurrency(this DbSet<DiscordUser> users, ulong botId)
=> users.AsQueryable() => users.AsQueryable()
.Where(x => x.UserId != botId) .Where(x => x.UserId != botId)
.OrderByDescending(x => x.CurrencyAmount) .OrderByDescending(x => x.CurrencyAmount)
.Take(users.Count() / 100 == 0 ? 1 : users.Count() / 100) .Take(users.Count() / 100 == 0 ? 1 : users.Count() / 100)
.Sum(x => x.CurrencyAmount); .Sum(x => x.CurrencyAmount);
} }

View File

@@ -1,4 +1,5 @@
#nullable disable #nullable disable
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Common.Yml; using NadekoBot.Common.Yml;
@@ -653,14 +654,18 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
#region Basic Operations #region Basic Operations
public async Task<NadekoExpression> AddAsync(ulong? guildId, string key, string message) public async Task<NadekoExpression> AddAsync(ulong? guildId, string key, string message,
bool ca = false, bool ad = false, bool dm = false)
{ {
key = key.ToLowerInvariant(); key = key.ToLowerInvariant();
var expr = new NadekoExpression var expr = new NadekoExpression
{ {
GuildId = guildId, GuildId = guildId,
Trigger = key, Trigger = key,
Response = message Response = message,
ContainsAnywhere = ca,
AutoDeleteTrigger = ad,
DmResponse = dm
}; };
if (expr.Response.Contains("%target%", StringComparison.OrdinalIgnoreCase)) if (expr.Response.Contains("%target%", StringComparison.OrdinalIgnoreCase))
@@ -677,7 +682,8 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
return expr; return expr;
} }
public async Task<NadekoExpression> EditAsync(ulong? guildId, int id, string message) public async Task<NadekoExpression> EditAsync(ulong? guildId, int id, string message,
bool? ca = null, bool? ad = null, bool? dm = null)
{ {
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var expr = uow.Set<NadekoExpression>().GetById(id); var expr = uow.Set<NadekoExpression>().GetById(id);
@@ -696,6 +702,10 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
if (expr.Response.Contains("%target%", StringComparison.OrdinalIgnoreCase)) if (expr.Response.Contains("%target%", StringComparison.OrdinalIgnoreCase))
expr.AllowTarget = true; expr.AllowTarget = true;
expr.ContainsAnywhere = ca ?? expr.ContainsAnywhere;
expr.AutoDeleteTrigger = ad ?? expr.AutoDeleteTrigger;
expr.DmResponse = dm ?? expr.DmResponse;
await uow.SaveChangesAsync(); await uow.SaveChangesAsync();
await UpdateInternalAsync(guildId, expr); await UpdateInternalAsync(guildId, expr);
@@ -747,4 +757,21 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
return toReturn; return toReturn;
} }
public async Task<(IReadOnlyCollection<NadekoExpression> Exprs, int TotalCount)> FindExpressionsAsync(ulong guildId,
string query, int page)
{
await using var ctx = _db.GetDbContext();
if (newguildExpressions.TryGetValue(guildId, out var exprs))
{
return (exprs.Where(x => x.Trigger.Contains(query))
.Skip(page * 9)
.Take(9)
.ToArray(), exprs.Length);
}
return ([], 0);
}
} }

View File

@@ -751,7 +751,7 @@ public partial class Gambling : GamblingModule<GamblingService>
{ {
await using (var uow = _db.GetDbContext()) await using (var uow = _db.GetDbContext())
{ {
cleanRichest = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 10_000); cleanRichest = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 0, 10_000);
} }
await ctx.Channel.TriggerTypingAsync(); await ctx.Channel.TriggerTypingAsync();
@@ -763,7 +763,7 @@ public partial class Gambling : GamblingModule<GamblingService>
else else
{ {
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
cleanRichest = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, page); cleanRichest = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, page);
} }
await ctx.SendPaginatedConfirmAsync(page, await ctx.SendPaginatedConfirmAsync(page,
@@ -775,7 +775,7 @@ public partial class Gambling : GamblingModule<GamblingService>
if (!opts.Clean) if (!opts.Clean)
{ {
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
toSend = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, curPage); toSend = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, curPage);
} }
else else
{ {

View File

@@ -288,10 +288,10 @@ public class WaifuService : INService, IReadyExecutor
return (oldAff, success, remaining); return (oldAff, success, remaining);
} }
public IEnumerable<WaifuLbResult> GetTopWaifusAtPage(int page) public IEnumerable<WaifuLbResult> GetTopWaifusAtPage(int page, int perPage = 9)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
return uow.Set<WaifuInfo>().GetTop(9, page * 9); return uow.Set<WaifuInfo>().GetTop(perPage, page * perPage);
} }
public ulong GetWaifuUserId(ulong ownerId, string name) public ulong GetWaifuUserId(ulong ownerId, string name)

View File

@@ -568,10 +568,10 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
return uow.Set<UserXpStats>().GetTopUserXps(guildId, count); return uow.Set<UserXpStats>().GetTopUserXps(guildId, count);
} }
public DiscordUser[] GetUserXps(int page) public DiscordUser[] GetUserXps(int page, int perPage = 9)
{ {
using var uow = _db.GetDbContext(); using var uow = _db.GetDbContext();
return uow.Set<DiscordUser>().GetUsersXpLeaderboardFor(page); return uow.Set<DiscordUser>().GetUsersXpLeaderboardFor(page, perPage);
} }
public async Task ChangeNotificationType(ulong userId, ulong guildId, XpNotificationLocation type) public async Task ChangeNotificationType(ulong userId, ulong guildId, XpNotificationLocation type)