mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
- 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:
@@ -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();
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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)
|
||||
{
|
||||
|
@@ -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));
|
||||
}
|
@@ -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);
|
||||
}
|
@@ -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($@"
|
||||
|
@@ -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();
|
||||
}
|
Reference in New Issue
Block a user