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; using NadekoBot.Services.Currency;
namespace NadekoBot.Services; namespace NadekoBot.Services;
@@ -37,4 +38,6 @@ public interface ICurrencyService
IUser user, IUser user,
long amount, long amount,
TxData? txData); TxData? txData);
Task<IReadOnlyList<DiscordUser>> GetTopRichest(ulong ignoreId, int count);
} }

View File

@@ -1,6 +1,7 @@
#nullable disable #nullable disable
using LinqToDB; using LinqToDB;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using NadekoBot.Db;
using NadekoBot.Db.Models; using NadekoBot.Db.Models;
using NadekoBot.Services.Currency; using NadekoBot.Services.Currency;
@@ -106,4 +107,10 @@ public sealed class CurrencyService : ICurrencyService, INService
long amount, long amount,
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)
{
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) public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page)
=> users.AsQueryable().OrderByDescending(x => x.TotalXp).Skip(page * 9).Take(9).AsEnumerable().ToArray(); => 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, this DbSet<DiscordUser> users,
ulong botId, ulong botId,
int count, int count,
@@ -102,7 +102,7 @@ public static class DiscordUserExtensions
.OrderByDescending(c => c.CurrencyAmount) .OrderByDescending(c => c.CurrencyAmount)
.Skip(page * 9) .Skip(page * 9)
.Take(count) .Take(count)
.ToList(); .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)
=> (await users.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId))?.CurrencyAmount ?? 0; => (await users.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId))?.CurrencyAmount ?? 0;

View File

@@ -81,25 +81,34 @@ public class GreetService : INService, IReadyExecutor
if (channel is null) if (channel is null)
return; return;
if (string.IsNullOrWhiteSpace(conf.BoostMessage)) await SendBoostMessage(conf, user, channel);
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");
}
}; };
private async Task<bool> 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) private Task OnClientLeftGuild(SocketGuild arg)
{ {
_guildConfigsCache.TryRemove(arg.Id, out _); _guildConfigsCache.TryRemove(arg.Id, out _);
@@ -174,6 +183,15 @@ public class GreetService : INService, IReadyExecutor
return uow.GuildConfigsForId(gid, set => set).BoostMessage; 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) private Task ByeUsers(GreetSettings conf, ITextChannel channel, IUser user)
=> ByeUsers(conf, channel, new[] { user }); => ByeUsers(conf, channel, new[] { user });
@@ -579,11 +597,16 @@ public class GreetService : INService, IReadyExecutor
await uow.SaveChangesAsync(); 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(); await using var uow = _db.GetDbContext();
var conf = uow.GuildConfigsForId(guildId, set => set); 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; conf.BoostMessageChannelId = channelId;
await uow.SaveChangesAsync(); await uow.SaveChangesAsync();
@@ -637,5 +660,11 @@ public class GreetService : INService, IReadyExecutor
return GreetDmUser(conf, user); return GreetDmUser(conf, user);
} }
public Task<bool> BoostTest(ITextChannel channel, IGuildUser user)
{
var conf = GetOrAddSettingsForGuild(user.GuildId);
return SendBoostMessage(conf, user, channel);
}
#endregion #endregion
} }

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