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

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

@@ -57,7 +57,6 @@ public static class DiscordUserExtensions
}, },
old => new() old => new()
{ {
}, },
() => new() () => new()
{ {
@@ -89,19 +88,19 @@ public static class DiscordUserExtensions
.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)

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)