mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 01:38:27 -04:00
Added some convenience methods
This commit is contained in:
@@ -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);
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
|
@@ -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<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)
|
||||
{
|
||||
_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<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);
|
||||
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<bool> BoostTest(ITextChannel channel, IGuildUser user)
|
||||
{
|
||||
var conf = GetOrAddSettingsForGuild(user.GuildId);
|
||||
return SendBoostMessage(conf, user, channel);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
@@ -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
|
||||
{
|
||||
|
Reference in New Issue
Block a user