diff --git a/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs b/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs index d7b23566d..8bd6eaba9 100644 --- a/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs +++ b/src/NadekoBot.Coordinator/Services/CoordinatorRunner.cs @@ -417,8 +417,7 @@ namespace NadekoBot.Coordinator { lock (locker) { - if (shardId >= _shardStatuses.Length) - throw new ArgumentOutOfRangeException(nameof(shardId)); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(shardId, _shardStatuses.Length); return _shardStatuses[shardId]; } diff --git a/src/NadekoBot/Modules/Administration/Mute/MuteService.cs b/src/NadekoBot/Modules/Administration/Mute/MuteService.cs index 24686e7c1..45640067f 100644 --- a/src/NadekoBot/Modules/Administration/Mute/MuteService.cs +++ b/src/NadekoBot/Modules/Administration/Mute/MuteService.cs @@ -291,8 +291,7 @@ public class MuteService : INService public async Task GetMuteRole(IGuild guild) { - if (guild is null) - throw new ArgumentNullException(nameof(guild)); + ArgumentNullException.ThrowIfNull(guild); const string defaultMuteRoleName = "nadeko-mute"; diff --git a/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs b/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs index 892387d90..4ae1855c0 100644 --- a/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs +++ b/src/NadekoBot/Modules/Administration/VcRole/VcRoleService.cs @@ -130,8 +130,7 @@ public class VcRoleService : INService public void AddVcRole(ulong guildId, IRole role, ulong vcId) { - if (role is null) - throw new ArgumentNullException(nameof(role)); + ArgumentNullException.ThrowIfNull(role); var guildVcRoles = VcRoles.GetOrAdd(guildId, new ConcurrentDictionary()); diff --git a/src/NadekoBot/Modules/Gambling/GamblingTopLevelModule.cs b/src/NadekoBot/Modules/Gambling/GamblingTopLevelModule.cs index 25828c25c..48965bfc6 100644 --- a/src/NadekoBot/Modules/Gambling/GamblingTopLevelModule.cs +++ b/src/NadekoBot/Modules/Gambling/GamblingTopLevelModule.cs @@ -24,6 +24,7 @@ public abstract class GamblingModule : NadekoModule { if (amount < 1) return false; + if (amount < Config.MinBet) { await Response().Error(strs.min_bet_limit(Format.Bold(Config.MinBet.ToString()) + CurrencySign)).SendAsync(); diff --git a/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs b/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs index ed7d39555..79fa43657 100644 --- a/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs +++ b/src/NadekoBot/Modules/Music/_common/Impl/MusicQueue.cs @@ -267,20 +267,8 @@ public sealed partial class MusicQueue : IMusicQueue { lock (_locker) { - var list = tracks.ToList(); - - for (var i = 0; i < list.Count; i++) - { - var struck = rng.Next(i, list.Count); - (list[struck], list[i]) = (list[i], list[struck]); - - // could preserving the index during shuffling be done better? - if (i == index) - index = struck; - else if (struck == index) - index = i; - } - + var list = tracks.ToArray(); + rng.Shuffle(list); tracks = new(list); } } diff --git a/src/NadekoBot/Modules/Patronage/PatronageService.cs b/src/NadekoBot/Modules/Patronage/PatronageService.cs index f0798a4f6..da6bc85e2 100644 --- a/src/NadekoBot/Modules/Patronage/PatronageService.cs +++ b/src/NadekoBot/Modules/Patronage/PatronageService.cs @@ -111,7 +111,6 @@ public sealed class PatronageService var lastDate = lastRun.ToDateOnly(); await using var ctx = _db.GetDbContext(); - await using var tran = await ctx.Database.BeginTransactionAsync(); if ((lastDate.Day == 1 || (lastDate.Month != nowDate.Month)) && nowDate.Day > 1) { @@ -141,7 +140,6 @@ public sealed class PatronageService // assumes that the code above runs in less than an hour await _cache.AddAsync(_quotaKey, now.ToBinary()); - await tran.CommitAsync(); } catch (Exception ex) { @@ -171,7 +169,6 @@ public sealed class PatronageService var lastChargeUtc = subscriber.LastCharge.Value.ToUniversalTime(); var dateInOneMonth = lastChargeUtc.Date.AddMonths(1); - // await using var tran = await ctx.Database.BeginTransactionAsync(); try { var dbPatron = await ctx.GetTable() diff --git a/src/NadekoBot/Modules/Xp/XpService.cs b/src/NadekoBot/Modules/Xp/XpService.cs index 71d1f7f4a..61a9cbc82 100644 --- a/src/NadekoBot/Modules/Xp/XpService.cs +++ b/src/NadekoBot/Modules/Xp/XpService.cs @@ -1469,7 +1469,6 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand } await using var ctx = _db.GetDbContext(); - // await using var tran = await ctx.Database.BeginTransactionAsync(); try { if (await ctx.GetTable() diff --git a/src/NadekoBot/_common/Abstractions/NadekoRandom.cs b/src/NadekoBot/_common/Abstractions/NadekoRandom.cs index 1bc11fa05..98162ccad 100644 --- a/src/NadekoBot/_common/Abstractions/NadekoRandom.cs +++ b/src/NadekoBot/_common/Abstractions/NadekoRandom.cs @@ -39,7 +39,6 @@ public sealed class NadekoRandom : Random public long NextLong(long minValue, long maxValue) { ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue); - if (minValue == maxValue) return minValue; var bytes = new byte[sizeof(long)]; diff --git a/src/NadekoBot/_common/DbService.cs b/src/NadekoBot/_common/DbService.cs index 522a0d4c1..7a5621271 100644 --- a/src/NadekoBot/_common/DbService.cs +++ b/src/NadekoBot/_common/DbService.cs @@ -12,5 +12,5 @@ public abstract class DbService public abstract Task SetupAsync(); public abstract DbContext CreateRawDbContext(string dbType, string connString); - public abstract DbContext GetDbContext(); + public abstract NadekoContext GetDbContext(); } \ No newline at end of file diff --git a/src/NadekoBot/_common/Services/CommandHandler.cs b/src/NadekoBot/_common/Services/CommandHandler.cs index 8728b22f3..519ab1c2b 100644 --- a/src/NadekoBot/_common/Services/CommandHandler.cs +++ b/src/NadekoBot/_common/Services/CommandHandler.cs @@ -94,10 +94,8 @@ public class CommandHandler : INService, IReadyExecutor, ICommandHandler public string SetPrefix(IGuild guild, string prefix) { - if (string.IsNullOrWhiteSpace(prefix)) - throw new ArgumentNullException(nameof(prefix)); - if (guild is null) - throw new ArgumentNullException(nameof(guild)); + ArgumentNullException.ThrowIfNullOrWhiteSpace(prefix); + ArgumentNullException.ThrowIfNull(guild); using (var uow = _db.GetDbContext()) { diff --git a/src/NadekoBot/_common/Services/Currency/DefaultWallet.cs b/src/NadekoBot/_common/Services/Currency/DefaultWallet.cs index 7806ff588..e30206d7c 100644 --- a/src/NadekoBot/_common/Services/Currency/DefaultWallet.cs +++ b/src/NadekoBot/_common/Services/Currency/DefaultWallet.cs @@ -20,10 +20,10 @@ public class DefaultWallet : IWallet await using var ctx = _db.GetDbContext(); var userId = UserId; return await ctx - .GetTable() - .Where(x => x.UserId == userId) - .Select(x => x.CurrencyAmount) - .FirstOrDefaultAsync(); + .GetTable() + .Where(x => x.UserId == userId) + .Select(x => x.CurrencyAmount) + .FirstOrDefaultAsync(); } public async Task Take(long amount, TxData? txData) @@ -35,12 +35,12 @@ public class DefaultWallet : IWallet var userId = UserId; var changed = await ctx - .GetTable() - .Where(x => x.UserId == userId && x.CurrencyAmount >= amount) - .UpdateAsync(x => new() - { - CurrencyAmount = x.CurrencyAmount - amount - }); + .GetTable() + .Where(x => x.UserId == userId && x.CurrencyAmount >= amount) + .UpdateAsync(x => new() + { + CurrencyAmount = x.CurrencyAmount - amount + }); if (changed == 0) return false; @@ -48,17 +48,17 @@ public class DefaultWallet : IWallet if (txData is not null) { await ctx - .GetTable() - .InsertAsync(() => new() - { - Amount = -amount, - Note = txData.Note, - UserId = userId, - Type = txData.Type, - Extra = txData.Extra, - OtherId = txData.OtherId, - DateAdded = DateTime.UtcNow - }); + .GetTable() + .InsertAsync(() => new() + { + Amount = -amount, + Note = txData.Note, + UserId = userId, + Type = txData.Type, + Extra = txData.Extra, + OtherId = txData.OtherId, + DateAdded = DateTime.UtcNow + }); } return true; @@ -72,43 +72,37 @@ public class DefaultWallet : IWallet await using var ctx = _db.GetDbContext(); var userId = UserId; - await using (var tran = await ctx.Database.BeginTransactionAsync()) - { - var changed = await ctx - .GetTable() - .Where(x => x.UserId == userId) - .UpdateAsync(x => new() - { - CurrencyAmount = x.CurrencyAmount + amount - }); - if (changed == 0) - { - await ctx - .GetTable() - .Value(x => x.UserId, userId) - .Value(x => x.Username, "Unknown") - .Value(x => x.Discriminator, "????") - .Value(x => x.CurrencyAmount, amount) - .InsertAsync(); - } - - await tran.CommitAsync(); - } + await ctx.GetTable() + .InsertOrUpdateAsync(() => new() + { + UserId = userId, + Username = "Unknown", + Discriminator = "????", + CurrencyAmount = amount, + }, + (old) => new() + { + CurrencyAmount = old.CurrencyAmount + amount + }, + () => new() + { + UserId = userId + }); if (txData is not null) { await ctx.GetTable() - .InsertAsync(() => new() - { - Amount = amount, - UserId = userId, - Note = txData.Note, - Type = txData.Type, - Extra = txData.Extra, - OtherId = txData.OtherId, - DateAdded = DateTime.UtcNow - }); + .InsertAsync(() => new() + { + Amount = amount, + UserId = userId, + Note = txData.Note, + Type = txData.Type, + Extra = txData.Extra, + OtherId = txData.OtherId, + DateAdded = DateTime.UtcNow + }); } } } \ No newline at end of file