diff --git a/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs b/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs index 3d7be3800..bd73213cc 100644 --- a/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs +++ b/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs @@ -39,5 +39,5 @@ public interface ICurrencyService long amount, TxData? txData); - Task> GetTopRichest(ulong ignoreId, int count); + Task> GetTopRichest(ulong ignoreId, int page = 0, int perPage = 9); } \ No newline at end of file diff --git a/src/Nadeko.Bot.Common/Services/Currency/CurrencyService.cs b/src/Nadeko.Bot.Common/Services/Currency/CurrencyService.cs index 14fc4e34f..d8cddcd74 100644 --- a/src/Nadeko.Bot.Common/Services/Currency/CurrencyService.cs +++ b/src/Nadeko.Bot.Common/Services/Currency/CurrencyService.cs @@ -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> GetTopRichest(ulong ignoreId, int count) + public async Task> GetTopRichest(ulong ignoreId, int page = 0, int perPage = 9) { await using var uow = _db.GetDbContext(); - return await uow.Set().GetTopRichest(ignoreId, count); + return await uow.Set().GetTopRichest(ignoreId, page, perPage); } } \ No newline at end of file diff --git a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs index 58bc7f729..5965d662b 100644 --- a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs +++ b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs @@ -12,7 +12,7 @@ public static class DiscordUserExtensions this IQueryable 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() - .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() - .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 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 users, int page) - => users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * 9).Take(9).AsEnumerable().ToArray(); + public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet users, int page, int perPage) + => users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * perPage).Take(perPage).AsEnumerable() + .ToArray(); public static Task> GetTopRichest( this DbSet 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 GetUserCurrencyAsync(this DbSet 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 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); } \ No newline at end of file diff --git a/src/Nadeko.Bot.Modules.Expresssions/NadekoExpressionsService.cs b/src/Nadeko.Bot.Modules.Expresssions/NadekoExpressionsService.cs index a330e47db..6082bb57d 100644 --- a/src/Nadeko.Bot.Modules.Expresssions/NadekoExpressionsService.cs +++ b/src/Nadeko.Bot.Modules.Expresssions/NadekoExpressionsService.cs @@ -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 AddAsync(ulong? guildId, string key, string message) + public async Task 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 EditAsync(ulong? guildId, int id, string message) + public async Task 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().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 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); + } } \ No newline at end of file diff --git a/src/Nadeko.Bot.Modules.Gambling/Gambling/Gambling.cs b/src/Nadeko.Bot.Modules.Gambling/Gambling/Gambling.cs index 8142ea7f6..270f9bbec 100644 --- a/src/Nadeko.Bot.Modules.Gambling/Gambling/Gambling.cs +++ b/src/Nadeko.Bot.Modules.Gambling/Gambling/Gambling.cs @@ -751,7 +751,7 @@ public partial class Gambling : GamblingModule { await using (var uow = _db.GetDbContext()) { - cleanRichest = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 10_000); + cleanRichest = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 0, 10_000); } await ctx.Channel.TriggerTypingAsync(); @@ -763,7 +763,7 @@ public partial class Gambling : GamblingModule else { await using var uow = _db.GetDbContext(); - cleanRichest = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 9, page); + cleanRichest = await uow.Set().GetTopRichest(_client.CurrentUser.Id, page); } await ctx.SendPaginatedConfirmAsync(page, @@ -775,7 +775,7 @@ public partial class Gambling : GamblingModule if (!opts.Clean) { await using var uow = _db.GetDbContext(); - toSend = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 9, curPage); + toSend = await uow.Set().GetTopRichest(_client.CurrentUser.Id, curPage); } else { diff --git a/src/Nadeko.Bot.Modules.Gambling/Gambling/Waifus/WaifuService.cs b/src/Nadeko.Bot.Modules.Gambling/Gambling/Waifus/WaifuService.cs index 85a8b937b..3ce28811a 100644 --- a/src/Nadeko.Bot.Modules.Gambling/Gambling/Waifus/WaifuService.cs +++ b/src/Nadeko.Bot.Modules.Gambling/Gambling/Waifus/WaifuService.cs @@ -288,10 +288,10 @@ public class WaifuService : INService, IReadyExecutor return (oldAff, success, remaining); } - public IEnumerable GetTopWaifusAtPage(int page) + public IEnumerable GetTopWaifusAtPage(int page, int perPage = 9) { using var uow = _db.GetDbContext(); - return uow.Set().GetTop(9, page * 9); + return uow.Set().GetTop(perPage, page * perPage); } public ulong GetWaifuUserId(ulong ownerId, string name) diff --git a/src/Nadeko.Bot.Modules.Xp/XpService.cs b/src/Nadeko.Bot.Modules.Xp/XpService.cs index ea5d08ca4..6525fe7c6 100644 --- a/src/Nadeko.Bot.Modules.Xp/XpService.cs +++ b/src/Nadeko.Bot.Modules.Xp/XpService.cs @@ -568,10 +568,10 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand return uow.Set().GetTopUserXps(guildId, count); } - public DiscordUser[] GetUserXps(int page) + public DiscordUser[] GetUserXps(int page, int perPage = 9) { using var uow = _db.GetDbContext(); - return uow.Set().GetUsersXpLeaderboardFor(page); + return uow.Set().GetUsersXpLeaderboardFor(page, perPage); } public async Task ChangeNotificationType(ulong userId, ulong guildId, XpNotificationLocation type)