- More code cleanup and codestyle updates

- Fixed some possible nullref exceptions
- Methods signatures now have up to 3 parameters before breakaing down each parameter in a separate line
- Method invocations have the same rule, except the first parameter will be in the same line as the invocation to prevent some ugliness when passing lambas as arguments
- Applied many more codestyles
- Extensions folder fully reformatted
This commit is contained in:
Kwoth
2021-12-26 17:28:39 +01:00
parent b85ba177cd
commit d5fd6aae8e
217 changed files with 1017 additions and 1494 deletions

View File

@@ -34,12 +34,10 @@ public static class ClubExtensions
.Max() + 1;
public static List<ClubInfo> GetClubLeaderboardPage(this DbSet<ClubInfo> clubs, int page)
{
return clubs
=> clubs
.AsNoTracking()
.OrderByDescending(x => x.Xp)
.Skip(page * 9)
.Take(9)
.ToList();
}
}

View File

@@ -6,13 +6,11 @@ namespace NadekoBot.Db;
public static class CurrencyTransactionExtensions
{
public static List<CurrencyTransaction> GetPageFor(this DbSet<CurrencyTransaction> set, ulong userId, int page)
{
return set.AsQueryable()
=> set.AsQueryable()
.AsNoTracking()
.Where(x => x.UserId == userId)
.OrderByDescending(x => x.DateAdded)
.Skip(15 * page)
.Take(15)
.ToList();
}
}

View File

@@ -7,21 +7,15 @@ namespace NadekoBot.Db;
public static class CustomReactionsExtensions
{
public static int ClearFromGuild(this DbSet<CustomReaction> crs, ulong guildId)
{
return crs.Delete(x => x.GuildId == guildId);
}
=> crs.Delete(x => x.GuildId == guildId);
public static IEnumerable<CustomReaction> ForId(this DbSet<CustomReaction> crs, ulong id)
{
return crs
=> crs
.AsNoTracking()
.AsQueryable()
.Where(x => x.GuildId == id)
.ToArray();
}
public static CustomReaction GetByGuildIdAndInput(this DbSet<CustomReaction> crs, ulong? guildId, string input)
{
return crs.FirstOrDefault(x => x.GuildId == guildId && x.Trigger.ToUpper() == input);
}
=> crs.FirstOrDefault(x => x.GuildId == guildId && x.Trigger.ToUpper() == input);
}

View File

@@ -9,8 +9,7 @@ namespace NadekoBot.Db;
public static class DiscordUserExtensions
{
public static void EnsureUserCreated(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
{
ctx.DiscordUser
=> ctx.DiscordUser
.ToLinqToDBTable()
.InsertOrUpdate(() => new()
{
@@ -30,7 +29,6 @@ public static class DiscordUserExtensions
{
UserId = userId
});
}
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
@@ -45,44 +43,36 @@ public static class DiscordUserExtensions
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId);
public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id)
{
return users.AsQueryable()
=> users.AsQueryable()
.Where(x => x.TotalXp > users
.AsQueryable()
.Where(y => y.UserId == id)
.Select(y => y.TotalXp)
.FirstOrDefault())
.Count() + 1;
}
public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page)
{
return users.AsQueryable()
=> users.AsQueryable()
.OrderByDescending(x => x.TotalXp)
.Skip(page * 9)
.Take(9)
.AsEnumerable()
.ToArray();
}
public static List<DiscordUser> GetTopRichest(this DbSet<DiscordUser> users, ulong botId, int count, int page = 0)
{
return users.AsQueryable()
=> users.AsQueryable()
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
.OrderByDescending(c => c.CurrencyAmount)
.Skip(page * 9)
.Take(count)
.ToList();
}
public static List<DiscordUser> GetTopRichest(this DbSet<DiscordUser> users, ulong botId, int count)
{
return users.AsQueryable()
=> users.AsQueryable()
.Where(c => c.CurrencyAmount > 0 && botId != c.UserId)
.OrderByDescending(c => c.CurrencyAmount)
.Take(count)
.ToList();
}
public static long GetUserCurrency(this DbSet<DiscordUser> users, ulong userId) =>
users.AsNoTracking()
@@ -163,17 +153,13 @@ VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount}, 0);
}
public static decimal GetTotalCurrency(this DbSet<DiscordUser> users)
{
return users
=> users
.Sum((Func<DiscordUser, decimal>)(x => x.CurrencyAmount));
}
public static decimal GetTopOnePercentCurrency(this DbSet<DiscordUser> users, ulong botId)
{
return users.AsQueryable()
=> users.AsQueryable()
.Where(x => x.UserId != botId)
.OrderByDescending(x => x.CurrencyAmount)
.Take(users.Count() / 100 == 0 ? 1 : users.Count() / 100)
.Sum(x => x.CurrencyAmount);
}
}

View File

@@ -44,20 +44,17 @@ public static class GuildConfigExtensions
};
private static IQueryable<GuildConfig> IncludeEverything(this DbSet<GuildConfig> configs)
{
return configs
.AsQueryable()
.AsSplitQuery()
.Include(gc => gc.CommandCooldowns)
.Include(gc => gc.FollowedStreams)
.Include(gc => gc.StreamRole)
.Include(gc => gc.XpSettings)
.ThenInclude(x => x.ExclusionList)
.Include(gc => gc.DelMsgOnCmdChannels)
.Include(gc => gc.ReactionRoleMessages)
.ThenInclude(x => x.ReactionRoles)
;
}
=> configs
.AsQueryable()
.AsSplitQuery()
.Include(gc => gc.CommandCooldowns)
.Include(gc => gc.FollowedStreams)
.Include(gc => gc.StreamRole)
.Include(gc => gc.XpSettings)
.ThenInclude(x => x.ExclusionList)
.Include(gc => gc.DelMsgOnCmdChannels)
.Include(gc => gc.ReactionRoleMessages)
.ThenInclude(x => x.ReactionRoles);
public static IEnumerable<GuildConfig> GetAllGuildConfigs(this DbSet<GuildConfig> configs, List<ulong> availableGuilds)
=> configs
@@ -168,22 +165,18 @@ public static class GuildConfigExtensions
}
public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs)
{
return configs
=> configs
.AsQueryable()
.Include(x => x.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)
.ToArray();
}
public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs, List<ulong> included)
{
return configs.AsQueryable()
=> configs.AsQueryable()
.Where(gc => included.Contains(gc.GuildId))
.Include(gc => gc.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)
.ToList();
}
public static void SetCleverbotEnabled(this DbSet<GuildConfig> configs, ulong id, bool cleverbotEnabled)
{
@@ -212,8 +205,7 @@ public static class GuildConfigExtensions
}
public static IEnumerable<GeneratingChannel> GetGeneratingChannels(this DbSet<GuildConfig> configs)
{
return configs
=> configs
.AsQueryable()
.Include(x => x.GenerateCurrencyChannelIds)
.Where(x => x.GenerateCurrencyChannelIds.Any())
@@ -224,5 +216,4 @@ public static class GuildConfigExtensions
GuildId = x.GuildConfig.GuildId
})
.ToArray();
}
}

View File

@@ -7,11 +7,9 @@ namespace NadekoBot.Db;
public static class PollExtensions
{
public static IEnumerable<Poll> GetAllPolls(this DbSet<Poll> polls)
{
return polls.Include(x => x.Answers)
=> polls.Include(x => x.Answers)
.Include(x => x.Votes)
.ToArray();
}
public static void RemovePoll(this NadekoContext ctx, int id)
{

View File

@@ -6,10 +6,8 @@ namespace NadekoBot.Db;
public static class QuoteExtensions
{
public static IEnumerable<Quote> GetForGuild(this DbSet<Quote> quotes, ulong guildId)
{
return quotes.AsQueryable().Where(x => x.GuildId == guildId);
}
=> quotes.AsQueryable().Where(x => x.GuildId == guildId);
public static IEnumerable<Quote> GetGroup(this DbSet<Quote> quotes, ulong guildId, int page, OrderType order)
{
var q = quotes.AsQueryable().Where(x => x.GuildId == guildId);
@@ -46,8 +44,5 @@ public static class QuoteExtensions
}
public static void RemoveAllByKeyword(this DbSet<Quote> quotes, ulong guildId, string keyword)
{
quotes.RemoveRange(quotes.AsQueryable().Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword));
}
=> quotes.RemoveRange(quotes.AsQueryable().Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword));
}

View File

@@ -26,8 +26,7 @@ public static class UserXpExtensions
}
public static List<UserXpStats> GetUsersFor(this DbSet<UserXpStats> xps, ulong guildId, int page)
{
return xps
=> xps
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId)
@@ -35,29 +34,24 @@ public static class UserXpExtensions
.Skip(page * 9)
.Take(9)
.ToList();
}
public static List<UserXpStats> GetTopUserXps(this DbSet<UserXpStats> xps, ulong guildId, int count)
{
return xps
=> xps
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId)
.OrderByDescending(x => x.Xp + x.AwardedXp)
.Take(count)
.ToList();
}
public static int GetUserGuildRanking(this DbSet<UserXpStats> xps, ulong userId, ulong guildId)
{
// @"SELECT COUNT(*) + 1
//FROM UserXpStats
//WHERE GuildId = @p1 AND ((Xp + AwardedXp) > (SELECT Xp + AwardedXp
// FROM UserXpStats
// WHERE UserId = @p2 AND GuildId = @p1
// LIMIT 1));";
return xps
=> xps
.AsQueryable()
.AsNoTracking()
.Where(x => x.GuildId == guildId && x.Xp + x.AwardedXp >
@@ -66,15 +60,10 @@ public static class UserXpExtensions
.Select(y => y.Xp + y.AwardedXp)
.FirstOrDefault())
.Count() + 1;
}
public static void ResetGuildUserXp(this DbSet<UserXpStats> xps, ulong userId, ulong guildId)
{
xps.Delete(x => x.UserId == userId && x.GuildId == guildId);
}
=> xps.Delete(x => x.UserId == userId && x.GuildId == guildId);
public static void ResetGuildXp(this DbSet<UserXpStats> xps, ulong guildId)
{
xps.Delete(x => x.GuildId == guildId);
}
=> xps.Delete(x => x.GuildId == guildId);
}

View File

@@ -65,24 +65,20 @@ public static class WaifuExtensions
}
public static decimal GetTotalValue(this DbSet<WaifuInfo> waifus)
{
return waifus
=> waifus
.AsQueryable()
.Where(x => x.ClaimerId != null)
.Sum(x => x.Price);
}
public static ulong GetWaifuUserId(this DbSet<WaifuInfo> waifus, ulong ownerId, string name)
{
return waifus
=> waifus
.AsQueryable()
.AsNoTracking()
.Where(x => x.Claimer.UserId == ownerId
&& x.Waifu.Username + "#" + x.Waifu.Discriminator == name)
.Select(x => x.Waifu.UserId)
.FirstOrDefault();
}
public static WaifuInfoStats GetWaifuInfo(this NadekoContext ctx, ulong userId)
{
ctx.Database.ExecuteSqlInterpolated($@"

View File

@@ -33,8 +33,7 @@ public static class WarningExtensions
}
public static async Task ForgiveAll(this DbSet<Warning> warnings, ulong guildId, ulong userId, string mod)
{
await warnings.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId)
=> await warnings.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId)
.ForEachAsync(x =>
{
if (x.Forgiven != true)
@@ -43,10 +42,7 @@ public static class WarningExtensions
x.ForgivenBy = mod;
}
});
}
public static Warning[] GetForGuild(this DbSet<Warning> warnings, ulong id)
{
return warnings.AsQueryable().Where(x => x.GuildId == id).ToArray();
}
=> warnings.AsQueryable().Where(x => x.GuildId == id).ToArray();
}

View File

@@ -57,9 +57,7 @@ public class AntiSpamIgnore : DbEntity
public override int GetHashCode() => ChannelId.GetHashCode();
public override bool Equals(object obj)
{
return obj is AntiSpamIgnore inst
=> obj is AntiSpamIgnore inst
? inst.ChannelId == ChannelId
: false;
}
}

View File

@@ -23,9 +23,7 @@ public class ClubInfo : DbEntity
public string Description { get; set; }
public override string ToString()
{
return Name + "#" + Discrim;
}
=> Name + "#" + Discrim;
}
public class ClubApplicants

View File

@@ -17,7 +17,7 @@ public class CustomReaction : DbEntity
? Array.Empty<string>()
: Reactions.Split("@@@");
public bool IsGlobal() => GuildId is null || GuildId == 0;
public bool IsGlobal() => GuildId is null or 0;
}
public class ReactionResponse : DbEntity

View File

@@ -6,13 +6,9 @@ public class DelMsgOnCmdChannel : DbEntity
public bool State { get; set; }
public override int GetHashCode()
{
return ChannelId.GetHashCode();
}
=> ChannelId.GetHashCode();
public override bool Equals(object obj)
{
return obj is DelMsgOnCmdChannel x
&& x.ChannelId == ChannelId;
}
=> obj is DelMsgOnCmdChannel x
&& x.ChannelId == ChannelId;
}

View File

@@ -20,16 +20,12 @@ public class DiscordUser : DbEntity
public long CurrencyAmount { get; set; }
public override bool Equals(object obj)
{
return obj is DiscordUser du
=> obj is DiscordUser du
? du.UserId == UserId
: false;
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
=> UserId.GetHashCode();
public override string ToString() =>
Username + "#" + Discriminator;

View File

@@ -9,14 +9,10 @@ public class FeedSub : DbEntity
public string Url { get; set; }
public override int GetHashCode()
{
return Url.GetHashCode(StringComparison.InvariantCulture) ^ GuildConfigId.GetHashCode();
}
=> Url.GetHashCode(StringComparison.InvariantCulture) ^ GuildConfigId.GetHashCode();
public override bool Equals(object obj)
{
return obj is FeedSub s
&& s.Url.ToLower() == Url.ToLower()
&& s.GuildConfigId == GuildConfigId;
}
=> obj is FeedSub s
&& s.Url.ToLower() == Url.ToLower()
&& s.GuildConfigId == GuildConfigId;
}

View File

@@ -20,16 +20,12 @@ public class FollowedStream : DbEntity
}
protected bool Equals(FollowedStream other)
{
return ChannelId == other.ChannelId
&& Username.Trim().ToUpperInvariant() == other.Username.Trim().ToUpperInvariant()
&& Type == other.Type;
}
=> ChannelId == other.ChannelId
&& Username.Trim().ToUpperInvariant() == other.Username.Trim().ToUpperInvariant()
&& Type == other.Type;
public override int GetHashCode()
{
return HashCode.Combine(ChannelId, Username, (int) Type);
}
=> HashCode.Combine(ChannelId, Username, (int) Type);
public override bool Equals(object obj)
=> obj is FollowedStream fs && Equals(fs);

View File

@@ -6,11 +6,9 @@ public class GCChannelId : DbEntity
public ulong ChannelId { get; set; }
public override bool Equals(object obj)
{
return obj is GCChannelId gc
=> obj is GCChannelId gc
? gc.ChannelId == ChannelId
: false;
}
public override int GetHashCode() =>
this.ChannelId.GetHashCode();

View File

@@ -5,14 +5,10 @@ public class MutedUserId : DbEntity
public ulong UserId { get; set; }
public override int GetHashCode()
{
return UserId.GetHashCode();
}
=> UserId.GetHashCode();
public override bool Equals(object obj)
{
return obj is MutedUserId mui
=> obj is MutedUserId mui
? mui.UserId == UserId
: false;
}
}

View File

@@ -6,14 +6,10 @@ public class PollVote : DbEntity
public int VoteIndex { get; set; }
public override int GetHashCode()
{
return UserId.GetHashCode();
}
=> UserId.GetHashCode();
public override bool Equals(object obj)
{
return obj is PollVote p
=> obj is PollVote p
? p.UserId == UserId
: false;
}
}

View File

@@ -17,7 +17,5 @@ public class SlowmodeIgnoredRole : DbEntity
// override object.GetHashCode
public override int GetHashCode()
{
return RoleId.GetHashCode();
}
=> RoleId.GetHashCode();
}

View File

@@ -17,7 +17,5 @@ public class SlowmodeIgnoredUser : DbEntity
// override object.GetHashCode
public override int GetHashCode()
{
return UserId.GetHashCode();
}
=> UserId.GetHashCode();
}

View File

@@ -51,9 +51,7 @@ public class StreamRoleBlacklistedUser : DbEntity
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
=> UserId.GetHashCode();
}
public class StreamRoleWhitelistedUser : DbEntity
@@ -62,14 +60,10 @@ public class StreamRoleWhitelistedUser : DbEntity
public string Username { get; set; }
public override bool Equals(object obj)
{
return obj is StreamRoleWhitelistedUser x
=> obj is StreamRoleWhitelistedUser x
? x.UserId == UserId
: false;
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
=> UserId.GetHashCode();
}

View File

@@ -9,9 +9,7 @@ public class UnbanTimer : DbEntity
UserId.GetHashCode();
public override bool Equals(object obj)
{
return obj is UnbanTimer ut
=> obj is UnbanTimer ut
? ut.UserId == UserId
: false;
}
}

View File

@@ -9,9 +9,7 @@ public class UnmuteTimer : DbEntity
UserId.GetHashCode();
public override bool Equals(object obj)
{
return obj is UnmuteTimer ut
=> obj is UnmuteTimer ut
? ut.UserId == UserId
: false;
}
}

View File

@@ -10,9 +10,7 @@ public class UnroleTimer : DbEntity
UserId.GetHashCode() ^ RoleId.GetHashCode();
public override bool Equals(object obj)
{
return obj is UnroleTimer ut
=> obj is UnroleTimer ut
? ut.UserId == UserId && ut.RoleId == RoleId
: false;
}
}

View File

@@ -27,14 +27,10 @@ public class XpRoleReward : DbEntity
public bool Remove { get; set; }
public override int GetHashCode()
{
return Level.GetHashCode() ^ XpSettingsId.GetHashCode();
}
=> Level.GetHashCode() ^ XpSettingsId.GetHashCode();
public override bool Equals(object obj)
{
return obj is XpRoleReward xrr && xrr.Level == Level && xrr.XpSettingsId == XpSettingsId;
}
=> obj is XpRoleReward xrr && xrr.Level == Level && xrr.XpSettingsId == XpSettingsId;
}
public class XpCurrencyReward : DbEntity
@@ -46,14 +42,10 @@ public class XpCurrencyReward : DbEntity
public int Amount { get; set; }
public override int GetHashCode()
{
return Level.GetHashCode() ^ XpSettingsId.GetHashCode();
}
=> Level.GetHashCode() ^ XpSettingsId.GetHashCode();
public override bool Equals(object obj)
{
return obj is XpCurrencyReward xrr && xrr.Level == Level && xrr.XpSettingsId == XpSettingsId;
}
=> obj is XpCurrencyReward xrr && xrr.Level == Level && xrr.XpSettingsId == XpSettingsId;
}
public class ExcludedItem : DbEntity
@@ -62,12 +54,8 @@ public class ExcludedItem : DbEntity
public ExcludedItemType ItemType { get; set; }
public override int GetHashCode()
{
return ItemId.GetHashCode() ^ ItemType.GetHashCode();
}
=> ItemId.GetHashCode() ^ ItemType.GetHashCode();
public override bool Equals(object obj)
{
return obj is ExcludedItem ei && ei.ItemId == ItemId && ei.ItemType == ItemType;
}
=> obj is ExcludedItem ei && ei.ItemId == ItemId && ei.ItemType == ItemType;
}

View File

@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore.Design;
using NadekoBot.Services.Database.Models;
using Microsoft.Extensions.Logging;
using NadekoBot.Db.Models;
// ReSharper disable UnusedAutoPropertyAccessor.Global
namespace NadekoBot.Services.Database;
@@ -68,9 +69,7 @@ public class NadekoContext : DbContext
private static readonly ILoggerFactory _debugLoggerFactory =
LoggerFactory.Create(x => x.AddConsole());
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(_debugLoggerFactory);
}
=> optionsBuilder.UseLoggerFactory(_debugLoggerFactory);
#endif
protected override void OnModelCreating(ModelBuilder modelBuilder)