Fixed a bug in cmdcd not modifying the database. Added total stats to .betstats

This commit is contained in:
Kwoth
2022-10-07 11:01:26 +02:00
parent e6b7c31a72
commit eedf6998b6
2 changed files with 34 additions and 1 deletions

View File

@@ -83,6 +83,16 @@ public partial class Gambling : GamblingModule<GamblingService>
str += $"`{stat.Feature}` | {N(stat.Bet)} | {N(stat.PaidOut)} | {perc}\n"; str += $"`{stat.Feature}` | {N(stat.Bet)} | {N(stat.PaidOut)} | {perc}\n";
} }
var bet = stats.Sum(x => x.Bet);
var paidOut = stats.Sum(x => x.PaidOut);
if (bet == 0)
bet = 1;
var tPerc = (paidOut / bet).ToString("P2", Culture);
str += "--------------------------------------------\n";
str += $"**TOTAL** | {N(bet)} | {N(paidOut)} | {tPerc}";
eb.WithDescription(str); eb.WithDescription(str);
await ctx.Channel.EmbedAsync(eb); await ctx.Channel.EmbedAsync(eb);

View File

@@ -1,10 +1,13 @@
#nullable disable #nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db;
namespace NadekoBot.Modules.Permissions.Services; namespace NadekoBot.Modules.Permissions.Services;
public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService
{ {
private readonly DbService _db;
private readonly ConcurrentDictionary<ulong, ConcurrentDictionary<string, int>> _settings = new(); private readonly ConcurrentDictionary<ulong, ConcurrentDictionary<string, int>> _settings = new();
private readonly ConcurrentDictionary<(ulong, string), ConcurrentDictionary<ulong, DateTime>> _activeCooldowns = private readonly ConcurrentDictionary<(ulong, string), ConcurrentDictionary<ulong, DateTime>> _activeCooldowns =
@@ -12,11 +15,13 @@ public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService
public int Priority => 0; public int Priority => 0;
public CmdCdService(Bot bot) public CmdCdService(Bot bot, DbService db)
{ {
_db = db;
_settings = bot _settings = bot
.AllGuildConfigs .AllGuildConfigs
.ToDictionary(x => x.GuildId, x => x.CommandCooldowns .ToDictionary(x => x.GuildId, x => x.CommandCooldowns
.DistinctBy(x => x.CommandName.ToLowerInvariant())
.ToDictionary(c => c.CommandName, c => c.Seconds) .ToDictionary(c => c.CommandName, c => c.Seconds)
.ToConcurrent()) .ToConcurrent())
.ToConcurrent(); .ToConcurrent();
@@ -97,16 +102,34 @@ public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService
dict.TryRemove(cmdName, out _); dict.TryRemove(cmdName, out _);
_activeCooldowns.TryRemove((guildId, cmdName), out _); _activeCooldowns.TryRemove((guildId, cmdName), out _);
using var ctx = _db.GetDbContext();
var gc = ctx.GuildConfigsForId(guildId, x => x.Include(x => x.CommandCooldowns));
gc.CommandCooldowns.RemoveWhere(x => x.CommandName == cmdName);
ctx.SaveChanges();
} }
public void AddCooldown(ulong guildId, string name, int secs) public void AddCooldown(ulong guildId, string name, int secs)
{ {
if (secs <= 0)
throw new ArgumentOutOfRangeException(nameof(secs));
var sett = _settings.GetOrAdd(guildId, static _ => new()); var sett = _settings.GetOrAdd(guildId, static _ => new());
sett[name] = secs; sett[name] = secs;
// force cleanup // force cleanup
if (_activeCooldowns.TryGetValue((guildId, name), out var dict)) if (_activeCooldowns.TryGetValue((guildId, name), out var dict))
Cleanup(dict, secs); Cleanup(dict, secs);
using var ctx = _db.GetDbContext();
var gc = ctx.GuildConfigsForId(guildId, x => x.Include(x => x.CommandCooldowns));
gc.CommandCooldowns.RemoveWhere(x => x.CommandName == name);
gc.CommandCooldowns.Add(new()
{
Seconds = secs,
CommandName = name
});
ctx.SaveChanges();
} }
public IReadOnlyCollection<(string CommandName, int Seconds)> GetCommandCooldowns(ulong guildId) public IReadOnlyCollection<(string CommandName, int Seconds)> GetCommandCooldowns(ulong guildId)