diff --git a/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs b/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs index 816d2ece2..3d7be3800 100644 --- a/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs +++ b/src/Nadeko.Bot.Common/Currency/ICurrencyService.cs @@ -1,3 +1,4 @@ +using NadekoBot.Db.Models; using NadekoBot.Services.Currency; namespace NadekoBot.Services; @@ -37,4 +38,6 @@ public interface ICurrencyService IUser user, long amount, TxData? txData); + + Task> GetTopRichest(ulong ignoreId, int count); } \ 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 fd2c697ca..14fc4e34f 100644 --- a/src/Nadeko.Bot.Common/Services/Currency/CurrencyService.cs +++ b/src/Nadeko.Bot.Common/Services/Currency/CurrencyService.cs @@ -1,6 +1,7 @@ #nullable disable using LinqToDB; using LinqToDB.EntityFrameworkCore; +using NadekoBot.Db; using NadekoBot.Db.Models; using NadekoBot.Services.Currency; @@ -106,4 +107,10 @@ public sealed class CurrencyService : ICurrencyService, INService long amount, TxData txData) => await RemoveAsync(user.Id, amount, txData); + + public async Task> GetTopRichest(ulong ignoreId, int count) + { + await using var uow = _db.GetDbContext(); + return await uow.Set().GetTopRichest(ignoreId, count); + } } \ 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 3f15e189a..58bc7f729 100644 --- a/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs +++ b/src/Nadeko.Bot.Db/Extensions/DiscordUserExtensions.cs @@ -92,7 +92,7 @@ public static class DiscordUserExtensions public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet users, int page) => users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * 9).Take(9).AsEnumerable().ToArray(); - public static List GetTopRichest( + public static Task> GetTopRichest( this DbSet users, ulong botId, int count, @@ -102,7 +102,7 @@ public static class DiscordUserExtensions .OrderByDescending(c => c.CurrencyAmount) .Skip(page * 9) .Take(count) - .ToList(); + .ToListAsyncLinqToDB(); public static async Task GetUserCurrencyAsync(this DbSet users, ulong userId) => (await users.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId))?.CurrencyAmount ?? 0; diff --git a/src/Nadeko.Bot.Modules.Administration/GreetBye/GreetService.cs b/src/Nadeko.Bot.Modules.Administration/GreetBye/GreetService.cs index 9db490c10..7da82e528 100644 --- a/src/Nadeko.Bot.Modules.Administration/GreetBye/GreetService.cs +++ b/src/Nadeko.Bot.Modules.Administration/GreetBye/GreetService.cs @@ -81,25 +81,34 @@ public class GreetService : INService, IReadyExecutor if (channel is null) return; - if (string.IsNullOrWhiteSpace(conf.BoostMessage)) - return; - - var toSend = SmartText.CreateFrom(conf.BoostMessage); - - try - { - var newContent = await _repSvc.ReplaceAsync(toSend, - new(client: _client, guild: user.Guild, channel: channel, users: user)); - var toDelete = await channel.SendAsync(newContent); - if (conf.BoostMessageDeleteAfter > 0) - toDelete.DeleteAfter(conf.BoostMessageDeleteAfter); - } - catch (Exception ex) - { - Log.Error(ex, "Error sending boost message"); - } + await SendBoostMessage(conf, user, channel); }; + private async Task SendBoostMessage(GreetSettings conf, IGuildUser user, ITextChannel channel) + { + if (string.IsNullOrWhiteSpace(conf.BoostMessage)) + return false; + + var toSend = SmartText.CreateFrom(conf.BoostMessage); + + try + { + var newContent = await _repSvc.ReplaceAsync(toSend, + new(client: _client, guild: user.Guild, channel: channel, users: user)); + var toDelete = await channel.SendAsync(newContent); + if (conf.BoostMessageDeleteAfter > 0) + toDelete.DeleteAfter(conf.BoostMessageDeleteAfter); + + return true; + } + catch (Exception ex) + { + Log.Error(ex, "Error sending boost message"); + } + + return false; + } + private Task OnClientLeftGuild(SocketGuild arg) { _guildConfigsCache.TryRemove(arg.Id, out _); @@ -174,6 +183,15 @@ public class GreetService : INService, IReadyExecutor return uow.GuildConfigsForId(gid, set => set).BoostMessage; } + public GreetSettings GetGreetSettings(ulong gid) + { + if (_guildConfigsCache.TryGetValue(gid, out var gs)) + return gs; + + using var uow = _db.GetDbContext(); + return GreetSettings.Create(uow.GuildConfigsForId(gid, set => set)); + } + private Task ByeUsers(GreetSettings conf, ITextChannel channel, IUser user) => ByeUsers(conf, channel, new[] { user }); @@ -579,11 +597,16 @@ public class GreetService : INService, IReadyExecutor await uow.SaveChangesAsync(); } - public async Task ToggleBoost(ulong guildId, ulong channelId) + public async Task ToggleBoost(ulong guildId, ulong channelId, bool? forceState = null) { await using var uow = _db.GetDbContext(); var conf = uow.GuildConfigsForId(guildId, set => set); - conf.SendBoostMessage = !conf.SendBoostMessage; + + if (forceState is not bool fs) + conf.SendBoostMessage = !conf.SendBoostMessage; + else + conf.SendBoostMessage = fs; + conf.BoostMessageChannelId = channelId; await uow.SaveChangesAsync(); @@ -637,5 +660,11 @@ public class GreetService : INService, IReadyExecutor return GreetDmUser(conf, user); } + public Task BoostTest(ITextChannel channel, IGuildUser user) + { + var conf = GetOrAddSettingsForGuild(user.GuildId); + return SendBoostMessage(conf, user, channel); + } + #endregion } \ 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 0aca1322f..8142ea7f6 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 = uow.Set().GetTopRichest(_client.CurrentUser.Id, 10_000); + cleanRichest = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 10_000); } await ctx.Channel.TriggerTypingAsync(); @@ -763,19 +763,19 @@ public partial class Gambling : GamblingModule else { await using var uow = _db.GetDbContext(); - cleanRichest = uow.Set().GetTopRichest(_client.CurrentUser.Id, 9, page).ToList(); + cleanRichest = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 9, page); } await ctx.SendPaginatedConfirmAsync(page, - curPage => + async curPage => { var embed = _eb.Create().WithOkColor().WithTitle(CurrencySign + " " + GetText(strs.leaderboard)); List toSend; if (!opts.Clean) { - using var uow = _db.GetDbContext(); - toSend = uow.Set().GetTopRichest(_client.CurrentUser.Id, 9, curPage); + await using var uow = _db.GetDbContext(); + toSend = await uow.Set().GetTopRichest(_client.CurrentUser.Id, 9, curPage); } else {