Fixed .log commands

This commit is contained in:
Kwoth
2021-09-12 22:07:56 +02:00
parent 0429210a73
commit d115261536
9 changed files with 2735 additions and 57 deletions

View File

@@ -113,33 +113,15 @@ namespace NadekoBot.Db
return config;
}
public static GuildConfig LogSettingsFor(this NadekoContext ctx, ulong guildId)
public static LogSetting LogSettingsFor(this NadekoContext ctx, ulong guildId)
{
var config = ctx
.GuildConfigs
var logSetting = ctx.LogSettings
.AsQueryable()
.Include(gc => gc.LogSetting)
.ThenInclude(gc => gc.IgnoredChannels)
.FirstOrDefault(x => x.GuildId == guildId);
.Include(x => x.IgnoredChannels)
.Where(x => x.GuildId == guildId)
.FirstOrDefault();
if (config is null)
{
ctx.GuildConfigs.Add((config = new GuildConfig
{
GuildId = guildId,
Permissions = Permissionv2.GetDefaultPermlist,
WarningsInitialized = true,
WarnPunishments = DefaultWarnPunishments,
}));
ctx.SaveChanges();
}
if (!config.WarningsInitialized)
{
config.WarningsInitialized = true;
config.WarnPunishments = DefaultWarnPunishments;
}
return config;
return logSetting;
}
public static IEnumerable<GuildConfig> Permissionsv2ForAll(this DbSet<GuildConfig> configs, List<ulong> include)

View File

@@ -41,8 +41,6 @@ namespace NadekoBot.Services.Database.Models
public bool SendChannelByeMessage { get; set; }
public string ChannelByeMessageText { get; set; } = "%user% has left!";
public LogSetting LogSetting { get; set; } = new LogSetting();
//self assignable roles
public bool ExclusiveSelfAssignedRoles { get; set; }
public bool AutoDeleteSelfAssignedRoleMessages { get; set; }

View File

@@ -8,6 +8,7 @@ namespace NadekoBot.Services.Database.Models
public HashSet<IgnoredLogChannel> IgnoredChannels { get; set; } = new HashSet<IgnoredLogChannel>();
public HashSet<IgnoredVoicePresenceChannel> IgnoredVoicePresenceChannelIds { get; set; } = new HashSet<IgnoredVoicePresenceChannel>();
public ulong GuildId { get; set; }
public ulong? LogOtherId { get; set; }
public ulong? MessageUpdatedId { get; set; }
public ulong? MessageDeletedId { get; set; }

View File

@@ -337,6 +337,14 @@ namespace NadekoBot.Services.Database
.OnDelete(DeleteBehavior.Cascade));
#endregion
#region LogSettings
modelBuilder.Entity<LogSetting>(ls => ls
.HasIndex(x => x.GuildId)
.IsUnique());
#endregion
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,69 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace NadekoBot.Migrations
{
public partial class logsettingsindependence : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<ulong>(
name: "GuildId",
table: "LogSettings",
type: "INTEGER",
nullable: false,
defaultValue: 0ul);
migrationBuilder.Sql(
@"UPDATE LogSettings SET GuildId = (SELECT GuildId FROM GuildConfigs WHERE LogSettingId = LogSettings.Id);
DELETE FROM LogSettings WHERE GuildId = 0;");
migrationBuilder.DropForeignKey(
name: "FK_GuildConfigs_LogSettings_LogSettingId",
table: "GuildConfigs");
migrationBuilder.DropIndex(
name: "IX_GuildConfigs_LogSettingId",
table: "GuildConfigs");
migrationBuilder.DropColumn(
name: "LogSettingId",
table: "GuildConfigs");
migrationBuilder.CreateIndex(
name: "IX_LogSettings_GuildId",
table: "LogSettings",
column: "GuildId",
unique: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_LogSettings_GuildId",
table: "LogSettings");
migrationBuilder.DropColumn(
name: "GuildId",
table: "LogSettings");
migrationBuilder.AddColumn<int>(
name: "LogSettingId",
table: "GuildConfigs",
type: "INTEGER",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_GuildConfigs_LogSettingId",
table: "GuildConfigs",
column: "LogSettingId");
migrationBuilder.AddForeignKey(
name: "FK_GuildConfigs_LogSettings_LogSettingId",
table: "GuildConfigs",
column: "LogSettingId",
principalTable: "LogSettings",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}

View File

@@ -795,9 +795,6 @@ namespace NadekoBot.Migrations
b.Property<string>("Locale")
.HasColumnType("TEXT");
b.Property<int?>("LogSettingId")
.HasColumnType("INTEGER");
b.Property<string>("MuteRoleName")
.HasColumnType("TEXT");
@@ -845,8 +842,6 @@ namespace NadekoBot.Migrations
b.HasIndex("GuildId")
.IsUnique();
b.HasIndex("LogSettingId");
b.HasIndex("WarnExpireHours");
b.ToTable("GuildConfigs");
@@ -914,6 +909,9 @@ namespace NadekoBot.Migrations
b.Property<DateTime?>("DateAdded")
.HasColumnType("TEXT");
b.Property<ulong>("GuildId")
.HasColumnType("INTEGER");
b.Property<ulong?>("LogOtherId")
.HasColumnType("INTEGER");
@@ -952,6 +950,9 @@ namespace NadekoBot.Migrations
b.HasKey("Id");
b.HasIndex("GuildId")
.IsUnique();
b.ToTable("LogSettings");
});
@@ -2245,15 +2246,6 @@ namespace NadekoBot.Migrations
b.Navigation("GuildConfig");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.GuildConfig", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.LogSetting", "LogSetting")
.WithMany()
.HasForeignKey("LogSettingId");
b.Navigation("LogSetting");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.IgnoredLogChannel", b =>
{
b.HasOne("NadekoBot.Services.Database.Models.LogSetting", "LogSetting")

View File

@@ -93,15 +93,15 @@ namespace NadekoBot.Modules.Administration.Services
{
var guildIds = client.Guilds.Select(x => x.Id).ToList();
var configs = uow
.Set<GuildConfig>()
.LogSettings
.AsQueryable()
.Include(gc => gc.LogSetting)
.ThenInclude(ls => ls.IgnoredChannels)
.AsNoTracking()
.Where(x => guildIds.Contains(x.GuildId))
.Include(ls => ls.IgnoredChannels)
.ToList();
GuildLogSettings = configs
.ToDictionary(g => g.GuildId, g => g.LogSetting)
.ToDictionary(ls => ls.GuildId)
.ToConcurrent();
}
@@ -170,17 +170,15 @@ namespace NadekoBot.Modules.Administration.Services
int removed = 0;
using (var uow = _db.GetDbContext())
{
var config = uow.LogSettingsFor(gid);
LogSetting logSetting = GuildLogSettings.GetOrAdd(gid, (id) => config.LogSetting);
var logSetting = uow.LogSettingsFor(gid);
removed = logSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == cid);
config.LogSetting.IgnoredChannels.RemoveWhere(ilc => ilc.ChannelId == cid);
if (removed == 0)
{
var toAdd = new IgnoredLogChannel {ChannelId = cid};
logSetting.IgnoredChannels.Add(toAdd);
config.LogSetting.IgnoredChannels.Add(toAdd);
}
GuildLogSettings.AddOrUpdate(gid, logSetting, (_, _) => logSetting);
uow.SaveChanges();
}
@@ -209,11 +207,10 @@ namespace NadekoBot.Modules.Administration.Services
public async Task LogServer(ulong guildId, ulong channelId, bool value)
{
LogSetting logSetting;
using (var uow = _db.GetDbContext())
{
logSetting = uow.LogSettingsFor(guildId).LogSetting;
GuildLogSettings.AddOrUpdate(guildId, (id) => logSetting, (id, old) => logSetting);
var logSetting = uow.LogSettingsFor(guildId);
logSetting.LogOtherId =
logSetting.MessageUpdatedId =
logSetting.MessageDeletedId =
@@ -230,8 +227,9 @@ namespace NadekoBot.Modules.Administration.Services
logSetting.UserMutedId =
logSetting.LogVoicePresenceTTSId =
(value ? channelId : (ulong?) null);
;
await uow.SaveChangesAsync();
GuildLogSettings.AddOrUpdate(guildId, (id) => logSetting, (id, old) => logSetting);
}
}
@@ -301,7 +299,7 @@ namespace NadekoBot.Modules.Administration.Services
ulong? channelId = null;
using (var uow = _db.GetDbContext())
{
var logSetting = uow.LogSettingsFor(gid).LogSetting;
var logSetting = uow.LogSettingsFor(gid);
GuildLogSettings.AddOrUpdate(gid, (id) => logSetting, (id, old) => logSetting);
switch (type)
{
@@ -1238,7 +1236,7 @@ namespace NadekoBot.Modules.Administration.Services
{
using (var uow = _db.GetDbContext())
{
var newLogSetting = uow.LogSettingsFor(guildId).LogSetting;
var newLogSetting = uow.LogSettingsFor(guildId);
switch (logChannelType)
{
case LogType.Other:

View File

@@ -57,8 +57,8 @@ namespace NadekoBot.Services
// if user is a new booster
// or boosted again the same server
if ((oldUser is { PremiumSince: null } && newUser is { PremiumSince: not null })
|| (oldUser.PremiumSince is DateTimeOffset oldDate
&& newUser.PremiumSince is DateTimeOffset newDate
|| (oldUser?.PremiumSince is DateTimeOffset oldDate
&& newUser?.PremiumSince is DateTimeOffset newDate
&& newDate > oldDate))
{
var conf = GetOrAddSettingsForGuild(newUser.Guild.Id);