refactor: add SqlKata for SQL generation, move connection handling into ModelRepository

This commit is contained in:
spiral
2021-09-29 21:51:38 -04:00
parent 6251d29abb
commit 92e45a07ff
60 changed files with 806 additions and 640 deletions

View File

@@ -35,7 +35,7 @@ namespace PluralKit.Bot
throw Errors.StringTooLongError("Member name", newName.Length, Limits.MaxMemberNameLength);
// Warn if there's already a member by this name
var existingMember = await _db.Execute(conn => _repo.GetMemberByName(conn, ctx.System.Id, newName));
var existingMember = await _repo.GetMemberByName(ctx.System.Id, newName);
if (existingMember != null && existingMember.Id != target.Id)
{
var msg = $"{Emojis.Warn} You already have a member in your system with the name \"{existingMember.NameFor(ctx)}\" (`{existingMember.Hid}`). Do you want to rename this member to that name too?";
@@ -44,7 +44,7 @@ namespace PluralKit.Bot
// Rename the member
var patch = new MemberPatch { Name = Partial<string>.Present(newName) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member renamed.");
if (newName.Contains(" ")) await ctx.Reply($"{Emojis.Note} Note that this member's name now contains spaces. You will need to surround it with \"double quotes\" when using commands referring to it.");
@@ -52,7 +52,7 @@ namespace PluralKit.Bot
if (ctx.Guild != null)
{
var memberGuildConfig = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id));
var memberGuildConfig = await _repo.GetMemberGuild(ctx.Guild.Id, target.Id);
if (memberGuildConfig.DisplayName != null)
await ctx.Reply($"{Emojis.Note} Note that this member has a server name set ({memberGuildConfig.DisplayName}) in this server ({ctx.Guild.Name}), and will be proxied using that name here.");
}
@@ -94,7 +94,7 @@ namespace PluralKit.Bot
if (await ctx.MatchClear("this member's description"))
{
var patch = new MemberPatch { Description = Partial<string>.Null() };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member description cleared.");
}
else
@@ -104,7 +104,7 @@ namespace PluralKit.Bot
throw Errors.StringTooLongError("Description", description.Length, Limits.MaxDescriptionLength);
var patch = new MemberPatch { Description = Partial<string>.Present(description) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member description changed.");
}
@@ -142,7 +142,7 @@ namespace PluralKit.Bot
if (await ctx.MatchClear("this member's pronouns"))
{
var patch = new MemberPatch { Pronouns = Partial<string>.Null() };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member pronouns cleared.");
}
else
@@ -152,7 +152,7 @@ namespace PluralKit.Bot
throw Errors.StringTooLongError("Pronouns", pronouns.Length, Limits.MaxPronounsLength);
var patch = new MemberPatch { Pronouns = Partial<string>.Present(pronouns) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member pronouns changed.");
}
@@ -164,7 +164,7 @@ namespace PluralKit.Bot
async Task ClearBannerImage()
{
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch { BannerImage = null }));
await _repo.UpdateMember(target.Id, new() { BannerImage = null });
await ctx.Reply($"{Emojis.Success} Member banner image cleared.");
}
@@ -172,7 +172,7 @@ namespace PluralKit.Bot
{
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch { BannerImage = img.Url }));
await _repo.UpdateMember(target.Id, new() { BannerImage = img.Url });
var msg = img.Source switch
{
@@ -219,7 +219,7 @@ namespace PluralKit.Bot
ctx.CheckOwnMember(target);
var patch = new MemberPatch { Color = Partial<string>.Null() };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member color cleared.");
}
@@ -251,7 +251,7 @@ namespace PluralKit.Bot
if (!Regex.IsMatch(color, "^[0-9a-fA-F]{6}$")) throw Errors.InvalidColorError(color);
var patch = new MemberPatch { Color = Partial<string>.Present(color.ToLowerInvariant()) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply(embed: new EmbedBuilder()
.Title($"{Emojis.Success} Member color changed.")
@@ -267,7 +267,7 @@ namespace PluralKit.Bot
ctx.CheckOwnMember(target);
var patch = new MemberPatch { Birthday = Partial<LocalDate?>.Null() };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member birthdate cleared.");
}
@@ -292,7 +292,7 @@ namespace PluralKit.Bot
if (birthday == null) throw Errors.BirthdayParseError(birthdayStr);
var patch = new MemberPatch { Birthday = Partial<LocalDate?>.Present(birthday) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await ctx.Reply($"{Emojis.Success} Member birthdate changed.");
}
@@ -304,7 +304,7 @@ namespace PluralKit.Bot
MemberGuildSettings memberGuildConfig = null;
if (ctx.Guild != null)
memberGuildConfig = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id));
memberGuildConfig = await _repo.GetMemberGuild(ctx.Guild.Id, target.Id);
var eb = new EmbedBuilder()
.Title($"Member names")
@@ -341,7 +341,7 @@ namespace PluralKit.Bot
var successStr = text;
if (ctx.Guild != null)
{
var memberGuildConfig = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id));
var memberGuildConfig = await _repo.GetMemberGuild(ctx.Guild.Id, target.Id);
if (memberGuildConfig.DisplayName != null)
successStr += $" However, this member has a server name set in this server ({ctx.Guild.Name}), and will be proxied using that name, \"{memberGuildConfig.DisplayName}\", here.";
}
@@ -379,7 +379,7 @@ namespace PluralKit.Bot
if (await ctx.MatchClear("this member's display name"))
{
var patch = new MemberPatch { DisplayName = Partial<string>.Null() };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await PrintSuccess($"{Emojis.Success} Member display name cleared. This member will now be proxied using their member name \"{target.NameFor(ctx)}\".");
}
@@ -388,7 +388,7 @@ namespace PluralKit.Bot
var newDisplayName = ctx.RemainderOrNull(skipFlags: false).NormalizeLineEndSpacing();
var patch = new MemberPatch { DisplayName = Partial<string>.Present(newDisplayName) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
await PrintSuccess($"{Emojis.Success} Member display name changed. This member will now be proxied using the name \"{newDisplayName}\".");
}
@@ -403,10 +403,10 @@ namespace PluralKit.Bot
noServerNameSetMessage += $" To set one, type `pk;member {target.Reference()} servername <server name>`.";
// No perms check, display name isn't covered by member privacy
var memberGuildConfig = await _repo.GetMemberGuild(ctx.Guild.Id, target.Id);
if (ctx.MatchRaw())
{
MemberGuildSettings memberGuildConfig = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id));
if (memberGuildConfig.DisplayName == null)
await ctx.Reply(noServerNameSetMessage);
@@ -427,8 +427,7 @@ namespace PluralKit.Bot
if (await ctx.MatchClear("this member's server name"))
{
var patch = new MemberGuildPatch { DisplayName = null };
await _db.Execute(conn => _repo.UpsertMemberGuild(conn, target.Id, ctx.Guild.Id, patch));
await _repo.UpdateMemberGuild(target.Id, ctx.Guild.Id, new() { DisplayName = null });
if (target.DisplayName != null)
await ctx.Reply($"{Emojis.Success} Member server name cleared. This member will now be proxied using their global display name \"{target.DisplayName}\" in this server ({ctx.Guild.Name}).");
@@ -439,8 +438,7 @@ namespace PluralKit.Bot
{
var newServerName = ctx.RemainderOrNull(skipFlags: false).NormalizeLineEndSpacing();
var patch = new MemberGuildPatch { DisplayName = newServerName };
await _db.Execute(conn => _repo.UpsertMemberGuild(conn, target.Id, ctx.Guild.Id, patch));
await _repo.UpdateMemberGuild(target.Id, ctx.Guild.Id, new() { DisplayName = newServerName });
await ctx.Reply($"{Emojis.Success} Member server name changed. This member will now be proxied using the name \"{newServerName}\" in this server ({ctx.Guild.Name}).");
}
@@ -464,7 +462,7 @@ namespace PluralKit.Bot
};
var patch = new MemberPatch { KeepProxy = Partial<bool>.Present(newValue) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
if (newValue)
await ctx.Reply($"{Emojis.Success} Member proxy tags will now be included in the resulting message when proxying.");
@@ -491,7 +489,7 @@ namespace PluralKit.Bot
};
var patch = new MemberPatch { AllowAutoproxy = Partial<bool>.Present(newValue) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
await _repo.UpdateMember(target.Id, patch);
if (newValue)
await ctx.Reply($"{Emojis.Success} Latch / front autoproxy have been **enabled** for this member.");
@@ -523,11 +521,11 @@ namespace PluralKit.Bot
// Get guild settings (mostly for warnings and such)
MemberGuildSettings guildSettings = null;
if (ctx.Guild != null)
guildSettings = await _db.Execute(c => _repo.GetMemberGuild(c, ctx.Guild.Id, target.Id));
guildSettings = await _repo.GetMemberGuild(ctx.Guild.Id, target.Id);
async Task SetAll(PrivacyLevel level)
{
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch().WithAllPrivacy(level)));
await _repo.UpdateMember(target.Id, new MemberPatch().WithAllPrivacy(level));
if (level == PrivacyLevel.Private)
await ctx.Reply($"{Emojis.Success} All {target.NameFor(ctx)}'s privacy settings have been set to **{level.LevelName()}**. Other accounts will now see nothing on the member card.");
@@ -537,7 +535,7 @@ namespace PluralKit.Bot
async Task SetLevel(MemberPrivacySubject subject, PrivacyLevel level)
{
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch().WithPrivacy(subject, level)));
await _repo.UpdateMember(target.Id, new MemberPatch().WithPrivacy(subject, level));
var subjectName = subject switch
{
@@ -596,7 +594,7 @@ namespace PluralKit.Bot
await ctx.Reply($"{Emojis.Warn} Are you sure you want to delete \"{target.NameFor(ctx)}\"? If so, reply to this message with the member's ID (`{target.Hid}`). __***This cannot be undone!***__");
if (!await ctx.ConfirmWithReply(target.Hid)) throw Errors.MemberDeleteCancelled;
await _db.Execute(conn => _repo.DeleteMember(conn, target.Id));
await _repo.DeleteMember(target.Id);
await ctx.Reply($"{Emojis.Success} Member deleted.");
}