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 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);
}

View File

@@ -3,7 +3,6 @@ using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db.Models;
using NadekoBot.Services.Database;
namespace NadekoBot.Db;
@@ -15,12 +14,12 @@ public static class DiscordUserExtensions
=> set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId);
public static void EnsureUserCreated(
this NadekoContext ctx,
this DbContext ctx,
ulong userId,
string username,
string discrim,
string avatarId)
=> ctx.DiscordUser.ToLinqToDBTable()
=> ctx.GetTable<DiscordUser>()
.InsertOrUpdate(
() => new()
{
@@ -43,10 +42,9 @@ public static class DiscordUserExtensions
});
public static Task EnsureUserCreatedAsync(
this NadekoContext ctx,
this NadekoBaseContext ctx,
ulong userId)
=> ctx.DiscordUser
.ToLinqToDBTable()
=> ctx.GetTable<DiscordUser>()
.InsertOrUpdateAsync(
() => 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
public static DiscordUser GetOrCreateUser(
this NadekoContext ctx,
this NadekoBaseContext ctx,
ulong userId,
string username,
string discrim,
@@ -77,7 +75,7 @@ public static class DiscordUserExtensions
{
ctx.EnsureUserCreated(userId, username, discrim, avatarId);
IQueryable<DiscordUser> queryable = ctx.DiscordUser;
IQueryable<DiscordUser> queryable = ctx.Set<DiscordUser>();
if (includes is not null)
queryable = includes(queryable);
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="guildId">Id of the guild to get stream role settings for.</param>
/// <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,
set => set.Include(y => y.StreamRole)
.Include(y => y.StreamRole.Whitelist)
.Include(y => y.StreamRole.Blacklist));
.Include(y => y.StreamRole.Whitelist)
.Include(y => y.StreamRole.Blacklist));
if (conf.StreamRole is null)
conf.StreamRole = new();
@@ -44,13 +44,13 @@ public static class GuildConfigExtensions
private static IQueryable<GuildConfig> IncludeEverything(this DbSet<GuildConfig> configs)
=> 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);
.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);
public static IEnumerable<GuildConfig> GetAllGuildConfigs(
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>
/// <returns>Config for the guild</returns>
public static GuildConfig GuildConfigsForId(
this NadekoContext ctx,
this NadekoBaseContext ctx,
ulong guildId,
Func<DbSet<GuildConfig>, IQueryable<GuildConfig>> includes)
{
GuildConfig config;
if (includes is null)
config = ctx.GuildConfigs.IncludeEverything().FirstOrDefault(c => c.GuildId == guildId);
config = ctx.Set<GuildConfig>().IncludeEverything().FirstOrDefault(c => c.GuildId == guildId);
else
{
var set = includes(ctx.GuildConfigs);
var set = includes(ctx.Set<GuildConfig>());
config = set.FirstOrDefault(c => c.GuildId == guildId);
}
if (config is null)
{
ctx.GuildConfigs.Add(config = new()
ctx.Set<GuildConfig>().Add(config = new()
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist,
@@ -120,19 +120,21 @@ public static class GuildConfigExtensions
// .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()
.Include(x => x.LogIgnores)
.Where(x => x.GuildId == guildId)
.FirstOrDefault();
var logSetting = ctx.Set<LogSetting>()
.AsQueryable()
.Include(x => x.LogIgnores)
.Where(x => x.GuildId == guildId)
.FirstOrDefault();
if (logSetting is null)
{
ctx.LogSettings.Add(logSetting = new()
{
GuildId = guildId
});
ctx.Set<LogSetting>()
.Add(logSetting = new()
{
GuildId = guildId
});
ctx.SaveChanges();
}
@@ -146,16 +148,16 @@ public static class GuildConfigExtensions
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()
.Where(gc => gc.GuildId == guildId)
.Include(gc => gc.Permissions)
.FirstOrDefault();
var config = ctx.Set<GuildConfig>().AsQueryable()
.Where(gc => gc.GuildId == guildId)
.Include(gc => gc.Permissions)
.FirstOrDefault();
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,
Permissions = Permissionv2.GetDefaultPermlist
@@ -176,10 +178,10 @@ public static class GuildConfigExtensions
public static IEnumerable<FollowedStream> GetFollowedStreams(this DbSet<GuildConfig> configs, List<ulong> included)
=> configs.AsQueryable()
.Where(gc => included.Contains(gc.GuildId))
.Include(gc => gc.FollowedStreams)
.SelectMany(gc => gc.FollowedStreams)
.ToList();
.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)
{
@@ -191,15 +193,15 @@ public static class GuildConfigExtensions
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,
set => set.Include(x => x.XpSettings)
.ThenInclude(x => x.RoleRewards)
.Include(x => x.XpSettings)
.ThenInclude(x => x.CurrencyRewards)
.Include(x => x.XpSettings)
.ThenInclude(x => x.ExclusionList));
.ThenInclude(x => x.RoleRewards)
.Include(x => x.XpSettings)
.ThenInclude(x => x.CurrencyRewards)
.Include(x => x.XpSettings)
.ThenInclude(x => x.ExclusionList));
if (gc.XpSettings is null)
gc.XpSettings = new();
@@ -209,15 +211,15 @@ public static class GuildConfigExtensions
public static IEnumerable<GeneratingChannel> GetGeneratingChannels(this DbSet<GuildConfig> configs)
=> configs.AsQueryable()
.Include(x => x.GenerateCurrencyChannelIds)
.Where(x => x.GenerateCurrencyChannelIds.Any())
.SelectMany(x => x.GenerateCurrencyChannelIds)
.Select(x => new GeneratingChannel
{
ChannelId = x.ChannelId,
GuildId = x.GuildConfig.GuildId
})
.ToArray();
.Include(x => x.GenerateCurrencyChannelIds)
.Where(x => x.GenerateCurrencyChannelIds.Any())
.SelectMany(x => x.GenerateCurrencyChannelIds)
.Select(x => new GeneratingChannel
{
ChannelId = x.ChannelId,
GuildId = x.GuildConfig.GuildId
})
.ToArray();
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.EntityFrameworkCore;
using NadekoBot.Db.Models;
namespace NadekoBot.Modules.Gambling.Bank;
@@ -20,8 +21,7 @@ public sealed class BankService : IBankService, INService
throw new ArgumentOutOfRangeException(nameof(amount));
await using var ctx = _db.GetDbContext();
await ctx.BankUsers
.ToLinqToDBTable()
await ctx.GetTable<BankUser>()
.InsertOrUpdateAsync(() => new()
{
UserId = userId,

View File

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