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,
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)
return true;
var wallet = await GetWalletAsync(userId);
var result = await wallet.Take(amount, txData);
if(result)
if (result)
await _txTracker.TrackRemove(amount, txData);
return result;
}
@@ -108,9 +108,9 @@ public sealed class CurrencyService : ICurrencyService, INService
TxData 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();
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,
ulong userId)
=> set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId);
public static void EnsureUserCreated(
this DbContext ctx,
ulong userId,
@@ -20,50 +20,49 @@ public static class DiscordUserExtensions
string discrim,
string avatarId)
=> ctx.GetTable<DiscordUser>()
.InsertOrUpdate(
() => new()
{
UserId = userId,
Username = username,
Discriminator = discrim,
AvatarId = avatarId,
TotalXp = 0,
CurrencyAmount = 0
},
old => new()
{
Username = username,
Discriminator = discrim,
AvatarId = avatarId
},
() => new()
{
UserId = userId
});
.InsertOrUpdate(
() => new()
{
UserId = userId,
Username = username,
Discriminator = discrim,
AvatarId = avatarId,
TotalXp = 0,
CurrencyAmount = 0
},
old => new()
{
Username = username,
Discriminator = discrim,
AvatarId = avatarId
},
() => new()
{
UserId = userId
});
public static Task EnsureUserCreatedAsync(
this DbContext ctx,
ulong userId)
=> ctx.GetTable<DiscordUser>()
.InsertOrUpdateAsync(
() => new()
{
UserId = userId,
Username = "Unknown",
Discriminator = "????",
AvatarId = string.Empty,
TotalXp = 0,
CurrencyAmount = 0
},
old => new()
{
.InsertOrUpdateAsync(
() => new()
{
UserId = userId,
Username = "Unknown",
Discriminator = "????",
AvatarId = string.Empty,
TotalXp = 0,
CurrencyAmount = 0
},
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
public static DiscordUser GetOrCreateUser(
this DbContext ctx,
@@ -84,25 +83,25 @@ public static class DiscordUserExtensions
public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id)
=> users.AsQueryable()
.Where(x => x.TotalXp
> users.AsQueryable().Where(y => y.UserId == id).Select(y => y.TotalXp).FirstOrDefault())
.Count()
.Where(x => x.TotalXp
> users.AsQueryable().Where(y => y.UserId == id).Select(y => y.TotalXp).FirstOrDefault())
.Count()
+ 1;
public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page)
=> users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * 9).Take(9).AsEnumerable().ToArray();
public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page, int perPage)
=> users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * perPage).Take(perPage).AsEnumerable()
.ToArray();
public static Task<List<DiscordUser>> GetTopRichest(
this DbSet<DiscordUser> users,
ulong botId,
int count,
int page = 0)
int page = 0, int perPage = 9)
=> users.AsQueryable()
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
.OrderByDescending(c => c.CurrencyAmount)
.Skip(page * 9)
.Take(count)
.ToListAsyncLinqToDB();
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
.OrderByDescending(c => c.CurrencyAmount)
.Skip(page * perPage)
.Take(perPage)
.ToListAsyncLinqToDB();
public static async Task<long> GetUserCurrencyAsync(this DbSet<DiscordUser> users, ulong userId)
=> (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)
=> users.AsQueryable()
.Where(x => x.UserId != botId)
.OrderByDescending(x => x.CurrencyAmount)
.Take(users.Count() / 100 == 0 ? 1 : users.Count() / 100)
.Sum(x => x.CurrencyAmount);
.Where(x => x.UserId != botId)
.OrderByDescending(x => x.CurrencyAmount)
.Take(users.Count() / 100 == 0 ? 1 : users.Count() / 100)
.Sum(x => x.CurrencyAmount);
}

View File

@@ -1,4 +1,5 @@
#nullable disable
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Common.Yml;
@@ -653,14 +654,18 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
#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();
var expr = new NadekoExpression
{
GuildId = guildId,
Trigger = key,
Response = message
Response = message,
ContainsAnywhere = ca,
AutoDeleteTrigger = ad,
DmResponse = dm
};
if (expr.Response.Contains("%target%", StringComparison.OrdinalIgnoreCase))
@@ -677,7 +682,8 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
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();
var expr = uow.Set<NadekoExpression>().GetById(id);
@@ -696,6 +702,10 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
if (expr.Response.Contains("%target%", StringComparison.OrdinalIgnoreCase))
expr.AllowTarget = true;
expr.ContainsAnywhere = ca ?? expr.ContainsAnywhere;
expr.AutoDeleteTrigger = ad ?? expr.AutoDeleteTrigger;
expr.DmResponse = dm ?? expr.DmResponse;
await uow.SaveChangesAsync();
await UpdateInternalAsync(guildId, expr);
@@ -747,4 +757,21 @@ public sealed class NadekoExpressionsService : IExecOnMessage, IReadyExecutor
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())
{
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();
@@ -763,7 +763,7 @@ public partial class Gambling : GamblingModule<GamblingService>
else
{
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,
@@ -775,7 +775,7 @@ public partial class Gambling : GamblingModule<GamblingService>
if (!opts.Clean)
{
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
{

View File

@@ -288,10 +288,10 @@ public class WaifuService : INService, IReadyExecutor
return (oldAff, success, remaining);
}
public IEnumerable<WaifuLbResult> GetTopWaifusAtPage(int page)
public IEnumerable<WaifuLbResult> GetTopWaifusAtPage(int page, int perPage = 9)
{
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)

View File

@@ -568,10 +568,10 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
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();
return uow.Set<DiscordUser>().GetUsersXpLeaderboardFor(page);
return uow.Set<DiscordUser>().GetUsersXpLeaderboardFor(page, perPage);
}
public async Task ChangeNotificationType(ulong userId, ulong guildId, XpNotificationLocation type)