Migrate guild objects to the patch system

This commit is contained in:
Ske
2020-06-29 15:20:28 +02:00
parent 467ce78522
commit 0598c53f62
11 changed files with 124 additions and 62 deletions

View File

@@ -122,11 +122,11 @@ namespace PluralKit.Bot
return eb.Build();
}
private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember) =>
_db.Execute(c =>
c.ExecuteAsync(
"insert into system_guild (system, guild, autoproxy_mode, autoproxy_member) values (@system, @guild, @autoproxyMode, @autoproxyMember) on conflict (system, guild) do update set autoproxy_mode = @autoproxyMode, autoproxy_member = @autoproxyMember",
new {autoproxyMode, autoproxyMember, guild = ctx.Guild.Id, system = ctx.System.Id}));
private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember)
{
var patch = new SystemGuildPatch {AutoproxyMode = autoproxyMode, AutoproxyMember = autoproxyMember};
return _db.Execute(conn => conn.UpsertSystemGuild(ctx.System.Id, ctx.Guild.Id, patch));
}
}
}

View File

@@ -139,19 +139,20 @@ namespace PluralKit.Bot
else throw new Exception("Unexpected condition when parsing avatar command");
}
private Task UpdateAvatar(AvatarLocation location, Context ctx, PKMember target, string? avatar) =>
location switch
private Task UpdateAvatar(AvatarLocation location, Context ctx, PKMember target, string? avatar)
{
switch (location)
{
AvatarLocation.Server => _db.Execute(c =>
c.ExecuteAsync(
"insert into member_guild(member, guild, avatar_url) values (@Member, @Guild, @Avatar) on conflict (member, guild) do update set avatar_url = @Avatar",
new {Avatar = avatar, Guild = ctx.Guild.Id, Member = target.Id})),
AvatarLocation.Member => _db.Execute(c =>
c.ExecuteAsync(
"update members set avatar_url = @Avatar where id = @Member",
new {Avatar = avatar, Member = target.Id})),
_ => throw new ArgumentOutOfRangeException($"Unknown avatar location {location}")
};
case AvatarLocation.Server:
var serverPatch = new MemberGuildPatch { AvatarUrl = avatar };
return _db.Execute(c => c.UpsertMemberGuild(target.Id, ctx.Guild.Id, serverPatch));
case AvatarLocation.Member:
var memberPatch = new MemberPatch { AvatarUrl = avatar };
return _db.Execute(c => c.UpdateMember(target.Id, memberPatch));
default:
throw new ArgumentOutOfRangeException($"Unknown avatar location {location}");
}
}
private enum AvatarLocation
{

View File

@@ -319,9 +319,8 @@ namespace PluralKit.Bot
{
CheckEditMemberPermission(ctx, target);
await _db.Execute(c =>
c.ExecuteAsync("update member_guild set display_name = null where member = @member and guild = @guild",
new {member = target.Id, guild = ctx.Guild.Id}));
var patch = new MemberGuildPatch {DisplayName = null};
await _db.Execute(conn => conn.UpsertMemberGuild(target.Id, ctx.Guild.Id, patch));
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}).");
@@ -341,10 +340,9 @@ namespace PluralKit.Bot
CheckEditMemberPermission(ctx, target);
var newServerName = ctx.RemainderOrNull();
await _db.Execute(c =>
c.ExecuteAsync("insert into member_guild(member, guild, display_name) values (@member, @guild, @newServerName) on conflict (member, guild) do update set display_name = @newServerName",
new {member = target.Id, guild = ctx.Guild.Id, newServerName}));
var patch = new MemberGuildPatch {DisplayName = newServerName};
await _db.Execute(conn => conn.UpsertMemberGuild(target.Id, ctx.Guild.Id, patch));
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}).");
}

View File

@@ -2,8 +2,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using DSharpPlus;
using DSharpPlus.Entities;
@@ -30,10 +28,8 @@ namespace PluralKit.Bot
channel = await ctx.MatchChannel() ?? throw new PKSyntaxError("You must pass a #channel to set.");
if (channel != null && channel.GuildId != ctx.Guild.Id) throw new PKError("That channel is not in this server!");
await _db.Execute(c => c.ExecuteAsync(QueryBuilder.Upsert("servers", "id")
.Constant("id", "@Id")
.Variable("log_channel", "@LogChannel")
.Build(), new {Id = ctx.Guild.Id, LogChannel = channel?.Id}));
var patch = new GuildPatch {LogChannel = channel?.Id};
await _db.Execute(conn => conn.UpsertGuild(ctx.Guild.Id, patch));
if (channel != null)
await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name}.");
@@ -66,8 +62,9 @@ namespace PluralKit.Bot
blacklist.ExceptWith(affectedChannels.Select(c => c.Id));
else
blacklist.UnionWith(affectedChannels.Select(c => c.Id));
await conn.ExecuteAsync("update servers set log_blacklist = @LogBlacklist where id = @Id",
new {ctx.Guild.Id, LogBlacklist = blacklist.ToArray()});
var patch = new GuildPatch {LogBlacklist = blacklist.ToArray()};
await conn.UpsertGuild(ctx.Guild.Id, patch);
}
await ctx.Reply(
@@ -98,8 +95,9 @@ namespace PluralKit.Bot
blacklist.UnionWith(affectedChannels.Select(c => c.Id));
else
blacklist.ExceptWith(affectedChannels.Select(c => c.Id));
await conn.ExecuteAsync("update servers set blacklist = @Blacklist where id = @Id",
new {ctx.Guild.Id, Blacklist = blacklist.ToArray()});
var patch = new GuildPatch {Blacklist = blacklist.ToArray()};
await conn.UpsertGuild(ctx.Guild.Id, patch);
}
await ctx.Reply($"{Emojis.Success} Channels {(shouldAdd ? "added to" : "removed from")} the proxy blacklist.");
@@ -131,9 +129,9 @@ namespace PluralKit.Bot
return;
}
await _db.Execute(c => c.ExecuteAsync("update servers set log_cleanup_enabled = @Value where id = @Id",
new {ctx.Guild.Id, Value = newValue}));
var patch = new GuildPatch {LogCleanupEnabled = newValue};
await _db.Execute(conn => conn.UpsertGuild(ctx.Guild.Id, patch));
if (newValue)
await ctx.Reply($"{Emojis.Success} Log cleanup has been **enabled** for this server. Messages deleted by PluralKit will now be cleaned up from logging channels managed by the following bots:\n- **{botList}**\n\n{Emojis.Note} Make sure PluralKit has the **Manage Messages** permission in the channels in question.\n{Emojis.Note} Also, make sure to blacklist the logging channel itself from the bots in question to prevent conflicts.");
else

View File

@@ -214,9 +214,8 @@ namespace PluralKit.Bot
return;
}
await _db.Execute(c =>
c.ExecuteAsync("update system_guild set proxy_enabled = @newValue where system = @system and guild = @guild",
new {newValue, system = ctx.System.Id, guild = ctx.Guild.Id}));
var patch = new SystemGuildPatch {ProxyEnabled = newValue};
await _db.Execute(conn => conn.UpsertSystemGuild(ctx.System.Id, ctx.Guild.Id, patch));
if (newValue)
await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **enabled** for your system.");