This commit is contained in:
Kwoth
2023-04-24 17:00:59 +02:00
parent ec2b7b3ad6
commit b34cd534c3
21 changed files with 72 additions and 65 deletions

View File

@@ -7,6 +7,6 @@ namespace NadekoBot.Extensions;
public static class DbExtensions public static class DbExtensions
{ {
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original, Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>>? includes = null) public static DiscordUser GetOrCreateUser(this NadekoBaseContext ctx, IUser original, Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>>? includes = null)
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes); => ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes);
} }

View File

@@ -3,7 +3,6 @@ using LinqToDB;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NadekoBot.Db.Models; using NadekoBot.Db.Models;
using NadekoBot.Services.Database;
namespace NadekoBot.Db; namespace NadekoBot.Db;
@@ -15,12 +14,12 @@ public static class DiscordUserExtensions
=> set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId); => set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId);
public static void EnsureUserCreated( public static void EnsureUserCreated(
this NadekoContext ctx, this DbContext ctx,
ulong userId, ulong userId,
string username, string username,
string discrim, string discrim,
string avatarId) string avatarId)
=> ctx.DiscordUser.ToLinqToDBTable() => ctx.GetTable<DiscordUser>()
.InsertOrUpdate( .InsertOrUpdate(
() => new() () => new()
{ {
@@ -43,10 +42,9 @@ public static class DiscordUserExtensions
}); });
public static Task EnsureUserCreatedAsync( public static Task EnsureUserCreatedAsync(
this NadekoContext ctx, this NadekoBaseContext ctx,
ulong userId) ulong userId)
=> ctx.DiscordUser => ctx.GetTable<DiscordUser>()
.ToLinqToDBTable()
.InsertOrUpdateAsync( .InsertOrUpdateAsync(
() => new() () => new()
{ {
@@ -68,7 +66,7 @@ public static class DiscordUserExtensions
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown //temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
public static DiscordUser GetOrCreateUser( public static DiscordUser GetOrCreateUser(
this NadekoContext ctx, this NadekoBaseContext ctx,
ulong userId, ulong userId,
string username, string username,
string discrim, string discrim,
@@ -77,7 +75,7 @@ public static class DiscordUserExtensions
{ {
ctx.EnsureUserCreated(userId, username, discrim, avatarId); ctx.EnsureUserCreated(userId, username, discrim, avatarId);
IQueryable<DiscordUser> queryable = ctx.DiscordUser; IQueryable<DiscordUser> queryable = ctx.Set<DiscordUser>();
if (includes is not null) if (includes is not null)
queryable = includes(queryable); queryable = includes(queryable);
return queryable.First(u => u.UserId == userId); return queryable.First(u => u.UserId == userId);

View File

@@ -29,12 +29,12 @@ public static class GuildConfigExtensions
/// <param name="ctx">Db Context</param> /// <param name="ctx">Db Context</param>
/// <param name="guildId">Id of the guild to get stream role settings for.</param> /// <param name="guildId">Id of the guild to get stream role settings for.</param>
/// <returns>Guild'p stream role settings</returns> /// <returns>Guild'p stream role settings</returns>
public static StreamRoleSettings GetStreamRoleSettings(this NadekoContext ctx, ulong guildId) public static StreamRoleSettings GetStreamRoleSettings(this NadekoBaseContext ctx, ulong guildId)
{ {
var conf = ctx.GuildConfigsForId(guildId, var conf = ctx.GuildConfigsForId(guildId,
set => set.Include(y => y.StreamRole) set => set.Include(y => y.StreamRole)
.Include(y => y.StreamRole.Whitelist) .Include(y => y.StreamRole.Whitelist)
.Include(y => y.StreamRole.Blacklist)); .Include(y => y.StreamRole.Blacklist));
if (conf.StreamRole is null) if (conf.StreamRole is null)
conf.StreamRole = new(); conf.StreamRole = new();
@@ -44,13 +44,13 @@ public static class GuildConfigExtensions
private static IQueryable<GuildConfig> IncludeEverything(this DbSet<GuildConfig> configs) private static IQueryable<GuildConfig> IncludeEverything(this DbSet<GuildConfig> configs)
=> configs.AsQueryable() => configs.AsQueryable()
.AsSplitQuery() .AsSplitQuery()
.Include(gc => gc.CommandCooldowns) .Include(gc => gc.CommandCooldowns)
.Include(gc => gc.FollowedStreams) .Include(gc => gc.FollowedStreams)
.Include(gc => gc.StreamRole) .Include(gc => gc.StreamRole)
.Include(gc => gc.XpSettings) .Include(gc => gc.XpSettings)
.ThenInclude(x => x.ExclusionList) .ThenInclude(x => x.ExclusionList)
.Include(gc => gc.DelMsgOnCmdChannels); .Include(gc => gc.DelMsgOnCmdChannels);
public static IEnumerable<GuildConfig> GetAllGuildConfigs( public static IEnumerable<GuildConfig> GetAllGuildConfigs(
this DbSet<GuildConfig> configs, this DbSet<GuildConfig> configs,
@@ -65,23 +65,23 @@ public static class GuildConfigExtensions
/// <param name="includes">Use to manipulate the set however you want. Pass null to include everything</param> /// <param name="includes">Use to manipulate the set however you want. Pass null to include everything</param>
/// <returns>Config for the guild</returns> /// <returns>Config for the guild</returns>
public static GuildConfig GuildConfigsForId( public static GuildConfig GuildConfigsForId(
this NadekoContext ctx, this NadekoBaseContext ctx,
ulong guildId, ulong guildId,
Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes) Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes)
{ {
GuildConfig config; GuildConfig config;
if (includes is null) if (includes is null)
config = ctx.GuildConfigs.IncludeEverything().FirstOrDefault(c => c.GuildId == guildId); config = ctx.Set<GuildConfig>().IncludeEverything().FirstOrDefault(c => c.GuildId == guildId);
else else
{ {
var set = includes(ctx.GuildConfigs); var set = includes(ctx.Set<GuildConfig>());
config = set.FirstOrDefault(c => c.GuildId == guildId); config = set.FirstOrDefault(c => c.GuildId == guildId);
} }
if (config is null) if (config is null)
{ {
ctx.GuildConfigs.Add(config = new() ctx.Set<GuildConfig>().Add(config = new()
{ {
GuildId = guildId, GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist, Permissions = Permissionv2.GetDefaultPermlist,
@@ -120,19 +120,21 @@ public static class GuildConfigExtensions
// .First(x => x.GuildId == guildId); // .First(x => x.GuildId == guildId);
} }
public static LogSetting LogSettingsFor(this NadekoContext ctx, ulong guildId) public static LogSetting LogSettingsFor(this NadekoBaseContext ctx, ulong guildId)
{ {
var logSetting = ctx.LogSettings.AsQueryable() var logSetting = ctx.Set<LogSetting>()
.Include(x => x.LogIgnores) .AsQueryable()
.Where(x => x.GuildId == guildId) .Include(x => x.LogIgnores)
.FirstOrDefault(); .Where(x => x.GuildId == guildId)
.FirstOrDefault();
if (logSetting is null) if (logSetting is null)
{ {
ctx.LogSettings.Add(logSetting = new() ctx.Set<LogSetting>()
{ .Add(logSetting = new()
GuildId = guildId {
}); GuildId = guildId
});
ctx.SaveChanges(); ctx.SaveChanges();
} }
@@ -146,16 +148,16 @@ public static class GuildConfigExtensions
return query.ToList(); return query.ToList();
} }
public static GuildConfig GcWithPermissionsFor(this NadekoContext ctx, ulong guildId) public static GuildConfig GcWithPermissionsFor(this NadekoBaseContext ctx, ulong guildId)
{ {
var config = ctx.GuildConfigs.AsQueryable() var config = ctx.Set<GuildConfig>().AsQueryable()
.Where(gc => gc.GuildId == guildId) .Where(gc => gc.GuildId == guildId)
.Include(gc => gc.Permissions) .Include(gc => gc.Permissions)
.FirstOrDefault(); .FirstOrDefault();
if (config is null) // if there is no guildconfig, create new one if (config is null) // if there is no guildconfig, create new one
{ {
ctx.GuildConfigs.Add(config = new() ctx.Set<GuildConfig>().Add(config = new()
{ {
GuildId = guildId, GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist Permissions = Permissionv2.GetDefaultPermlist
@@ -176,10 +178,10 @@ public static class GuildConfigExtensions
public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs, List<ulong> included) public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs, List<ulong> included)
=> configs.AsQueryable() => configs.AsQueryable()
.Where(gc => included.Contains(gc.GuildId)) .Where(gc => included.Contains(gc.GuildId))
.Include(gc => gc.FollowedStreams) .Include(gc => gc.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams) .SelectMany(gc => gc.FollowedStreams)
.ToList(); .ToList();
public static void SetCleverbotEnabled(this DbSet<GuildConfig> configs, ulong id, bool cleverbotEnabled) public static void SetCleverbotEnabled(this DbSet<GuildConfig> configs, ulong id, bool cleverbotEnabled)
{ {
@@ -191,15 +193,15 @@ public static class GuildConfigExtensions
conf.CleverbotEnabled = cleverbotEnabled; conf.CleverbotEnabled = cleverbotEnabled;
} }
public static XpSettings XpSettingsFor(this NadekoContext ctx, ulong guildId) public static XpSettings XpSettingsFor(this NadekoBaseContext ctx, ulong guildId)
{ {
var gc = ctx.GuildConfigsForId(guildId, var gc = ctx.GuildConfigsForId(guildId,
set => set.Include(x => x.XpSettings) set => set.Include(x => x.XpSettings)
.ThenInclude(x => x.RoleRewards) .ThenInclude(x => x.RoleRewards)
.Include(x => x.XpSettings) .Include(x => x.XpSettings)
.ThenInclude(x => x.CurrencyRewards) .ThenInclude(x => x.CurrencyRewards)
.Include(x => x.XpSettings) .Include(x => x.XpSettings)
.ThenInclude(x => x.ExclusionList)); .ThenInclude(x => x.ExclusionList));
if (gc.XpSettings is null) if (gc.XpSettings is null)
gc.XpSettings = new(); gc.XpSettings = new();
@@ -209,15 +211,15 @@ public static class GuildConfigExtensions
public static IEnumerable<GeneratingChannel> GetGeneratingChannels(this DbSet<GuildConfig> configs) public static IEnumerable<GeneratingChannel> GetGeneratingChannels(this DbSet<GuildConfig> configs)
=> configs.AsQueryable() => configs.AsQueryable()
.Include(x => x.GenerateCurrencyChannelIds) .Include(x => x.GenerateCurrencyChannelIds)
.Where(x => x.GenerateCurrencyChannelIds.Any()) .Where(x => x.GenerateCurrencyChannelIds.Any())
.SelectMany(x => x.GenerateCurrencyChannelIds) .SelectMany(x => x.GenerateCurrencyChannelIds)
.Select(x => new GeneratingChannel .Select(x => new GeneratingChannel
{ {
ChannelId = x.ChannelId, ChannelId = x.ChannelId,
GuildId = x.GuildConfig.GuildId GuildId = x.GuildConfig.GuildId
}) })
.ToArray(); .ToArray();
public class GeneratingChannel public class GeneratingChannel
{ {

View File

@@ -0,0 +1,7 @@
using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Db;
public abstract class NadekoBaseContext : DbContext
{
}

View File

@@ -1,5 +1,6 @@
using LinqToDB; using LinqToDB;
using LinqToDB.EntityFrameworkCore; using LinqToDB.EntityFrameworkCore;
using NadekoBot.Db.Models;
namespace NadekoBot.Modules.Gambling.Bank; namespace NadekoBot.Modules.Gambling.Bank;
@@ -20,8 +21,7 @@ public sealed class BankService : IBankService, INService
throw new ArgumentOutOfRangeException(nameof(amount)); throw new ArgumentOutOfRangeException(nameof(amount));
await using var ctx = _db.GetDbContext(); await using var ctx = _db.GetDbContext();
await ctx.BankUsers await ctx.GetTable<BankUser>()
.ToLinqToDBTable()
.InsertOrUpdateAsync(() => new() .InsertOrUpdateAsync(() => new()
{ {
UserId = userId, UserId = userId,

View File

@@ -7,21 +7,21 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0"/> <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<!-- .eval --> <!-- .eval -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.5.0"/> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.5.0" />
<!-- .calc --> <!-- .calc -->
<PackageReference Include="CoreCLR-NCalc" Version="2.2.110"/> <PackageReference Include="CoreCLR-NCalc" Version="2.2.110" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Nadeko.Bot.Common\Nadeko.Bot.Common.csproj"/> <ProjectReference Include="..\Nadeko.Bot.Common\Nadeko.Bot.Common.csproj" />
<ProjectReference Include="..\Nadeko.Bot.Modules.Searches\Nadeko.Bot.Modules.Searches.csproj"/> <ProjectReference Include="..\Nadeko.Bot.Modules.Searches\Nadeko.Bot.Modules.Searches.csproj" />
<ProjectReference Include="..\Nadeko.Bot.Generators.Cloneable\Nadeko.Bot.Generators.Cloneable.csproj" OutputItemType="Analyzer"/> <ProjectReference Include="..\Nadeko.Bot.Generators.Cloneable\Nadeko.Bot.Generators.Cloneable.csproj" OutputItemType="Analyzer" />
</ItemGroup> </ItemGroup>
</Project> </Project>