Added some convenience methods

This commit is contained in:
Kwoth
2024-04-19 14:56:02 +00:00
parent 1c41fbfce2
commit cc060da1a7
5 changed files with 65 additions and 26 deletions

View File

@@ -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<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int count);
}

View File

@@ -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<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int count)
{
await using var uow = _db.GetDbContext();
return await uow.Set<DiscordUser>().GetTopRichest(ignoreId, count);
}
}

View File

@@ -92,7 +92,7 @@ public static class DiscordUserExtensions
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 List<DiscordUser> GetTopRichest(
public static Task<List<DiscordUser>> GetTopRichest(
this DbSet<DiscordUser> 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<long> GetUserCurrencyAsync(this DbSet<DiscordUser> users, ulong userId)
=> (await users.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId))?.CurrencyAmount ?? 0;

View File

@@ -81,8 +81,13 @@ public class GreetService : INService, IReadyExecutor
if (channel is null)
return;
await SendBoostMessage(conf, user, channel);
};
private async Task<bool> SendBoostMessage(GreetSettings conf, IGuildUser user, ITextChannel channel)
{
if (string.IsNullOrWhiteSpace(conf.BoostMessage))
return;
return false;
var toSend = SmartText.CreateFrom(conf.BoostMessage);
@@ -93,12 +98,16 @@ public class GreetService : INService, IReadyExecutor
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)
{
@@ -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<bool> ToggleBoost(ulong guildId, ulong channelId)
public async Task<bool> ToggleBoost(ulong guildId, ulong channelId, bool? forceState = null)
{
await using var uow = _db.GetDbContext();
var conf = uow.GuildConfigsForId(guildId, set => set);
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<bool> BoostTest(ITextChannel channel, IGuildUser user)
{
var conf = GetOrAddSettingsForGuild(user.GuildId);
return SendBoostMessage(conf, user, channel);
}
#endregion
}

View File

@@ -751,7 +751,7 @@ public partial class Gambling : GamblingModule<GamblingService>
{
await using (var uow = _db.GetDbContext())
{
cleanRichest = uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 10_000);
cleanRichest = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 10_000);
}
await ctx.Channel.TriggerTypingAsync();
@@ -763,19 +763,19 @@ public partial class Gambling : GamblingModule<GamblingService>
else
{
await using var uow = _db.GetDbContext();
cleanRichest = uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, page).ToList();
cleanRichest = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, page);
}
await ctx.SendPaginatedConfirmAsync(page,
curPage =>
async curPage =>
{
var embed = _eb.Create().WithOkColor().WithTitle(CurrencySign + " " + GetText(strs.leaderboard));
List<DiscordUser> toSend;
if (!opts.Clean)
{
using var uow = _db.GetDbContext();
toSend = uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, curPage);
await using var uow = _db.GetDbContext();
toSend = await uow.Set<DiscordUser>().GetTopRichest(_client.CurrentUser.Id, 9, curPage);
}
else
{