Migrate guild objects to the patch system
This commit is contained in:
parent
467ce78522
commit
0598c53f62
@ -123,10 +123,10 @@ namespace PluralKit.Bot
|
|||||||
return eb.Build();
|
return eb.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember) =>
|
private Task UpdateAutoproxy(Context ctx, AutoproxyMode autoproxyMode, MemberId? autoproxyMember)
|
||||||
_db.Execute(c =>
|
{
|
||||||
c.ExecuteAsync(
|
var patch = new SystemGuildPatch {AutoproxyMode = autoproxyMode, AutoproxyMember = autoproxyMember};
|
||||||
"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",
|
return _db.Execute(conn => conn.UpsertSystemGuild(ctx.System.Id, ctx.Guild.Id, patch));
|
||||||
new {autoproxyMode, autoproxyMember, guild = ctx.Guild.Id, system = ctx.System.Id}));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -139,19 +139,20 @@ namespace PluralKit.Bot
|
|||||||
else throw new Exception("Unexpected condition when parsing avatar command");
|
else throw new Exception("Unexpected condition when parsing avatar command");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task UpdateAvatar(AvatarLocation location, Context ctx, PKMember target, string? avatar) =>
|
private Task UpdateAvatar(AvatarLocation location, Context ctx, PKMember target, string? avatar)
|
||||||
location switch
|
|
||||||
{
|
{
|
||||||
AvatarLocation.Server => _db.Execute(c =>
|
switch (location)
|
||||||
c.ExecuteAsync(
|
{
|
||||||
"insert into member_guild(member, guild, avatar_url) values (@Member, @Guild, @Avatar) on conflict (member, guild) do update set avatar_url = @Avatar",
|
case AvatarLocation.Server:
|
||||||
new {Avatar = avatar, Guild = ctx.Guild.Id, Member = target.Id})),
|
var serverPatch = new MemberGuildPatch { AvatarUrl = avatar };
|
||||||
AvatarLocation.Member => _db.Execute(c =>
|
return _db.Execute(c => c.UpsertMemberGuild(target.Id, ctx.Guild.Id, serverPatch));
|
||||||
c.ExecuteAsync(
|
case AvatarLocation.Member:
|
||||||
"update members set avatar_url = @Avatar where id = @Member",
|
var memberPatch = new MemberPatch { AvatarUrl = avatar };
|
||||||
new {Avatar = avatar, Member = target.Id})),
|
return _db.Execute(c => c.UpdateMember(target.Id, memberPatch));
|
||||||
_ => throw new ArgumentOutOfRangeException($"Unknown avatar location {location}")
|
default:
|
||||||
};
|
throw new ArgumentOutOfRangeException($"Unknown avatar location {location}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private enum AvatarLocation
|
private enum AvatarLocation
|
||||||
{
|
{
|
||||||
|
@ -319,9 +319,8 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
CheckEditMemberPermission(ctx, target);
|
CheckEditMemberPermission(ctx, target);
|
||||||
|
|
||||||
await _db.Execute(c =>
|
var patch = new MemberGuildPatch {DisplayName = null};
|
||||||
c.ExecuteAsync("update member_guild set display_name = null where member = @member and guild = @guild",
|
await _db.Execute(conn => conn.UpsertMemberGuild(target.Id, ctx.Guild.Id, patch));
|
||||||
new {member = target.Id, guild = ctx.Guild.Id}));
|
|
||||||
|
|
||||||
if (target.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}).");
|
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}).");
|
||||||
@ -342,9 +341,8 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
var newServerName = ctx.RemainderOrNull();
|
var newServerName = ctx.RemainderOrNull();
|
||||||
|
|
||||||
await _db.Execute(c =>
|
var patch = new MemberGuildPatch {DisplayName = newServerName};
|
||||||
c.ExecuteAsync("insert into member_guild(member, guild, display_name) values (@member, @guild, @newServerName) on conflict (member, guild) do update set display_name = @newServerName",
|
await _db.Execute(conn => conn.UpsertMemberGuild(target.Id, ctx.Guild.Id, patch));
|
||||||
new {member = target.Id, guild = ctx.Guild.Id, 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}).");
|
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}).");
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Dapper;
|
|
||||||
|
|
||||||
using DSharpPlus;
|
using DSharpPlus;
|
||||||
using DSharpPlus.Entities;
|
using DSharpPlus.Entities;
|
||||||
|
|
||||||
@ -30,10 +28,8 @@ namespace PluralKit.Bot
|
|||||||
channel = await ctx.MatchChannel() ?? throw new PKSyntaxError("You must pass a #channel to set.");
|
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!");
|
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")
|
var patch = new GuildPatch {LogChannel = channel?.Id};
|
||||||
.Constant("id", "@Id")
|
await _db.Execute(conn => conn.UpsertGuild(ctx.Guild.Id, patch));
|
||||||
.Variable("log_channel", "@LogChannel")
|
|
||||||
.Build(), new {Id = ctx.Guild.Id, LogChannel = channel?.Id}));
|
|
||||||
|
|
||||||
if (channel != null)
|
if (channel != null)
|
||||||
await ctx.Reply($"{Emojis.Success} Proxy logging channel set to #{channel.Name}.");
|
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));
|
blacklist.ExceptWith(affectedChannels.Select(c => c.Id));
|
||||||
else
|
else
|
||||||
blacklist.UnionWith(affectedChannels.Select(c => c.Id));
|
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(
|
await ctx.Reply(
|
||||||
@ -98,8 +95,9 @@ namespace PluralKit.Bot
|
|||||||
blacklist.UnionWith(affectedChannels.Select(c => c.Id));
|
blacklist.UnionWith(affectedChannels.Select(c => c.Id));
|
||||||
else
|
else
|
||||||
blacklist.ExceptWith(affectedChannels.Select(c => c.Id));
|
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.");
|
await ctx.Reply($"{Emojis.Success} Channels {(shouldAdd ? "added to" : "removed from")} the proxy blacklist.");
|
||||||
@ -131,8 +129,8 @@ namespace PluralKit.Bot
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _db.Execute(c => c.ExecuteAsync("update servers set log_cleanup_enabled = @Value where id = @Id",
|
var patch = new GuildPatch {LogCleanupEnabled = newValue};
|
||||||
new {ctx.Guild.Id, Value = newValue}));
|
await _db.Execute(conn => conn.UpsertGuild(ctx.Guild.Id, patch));
|
||||||
|
|
||||||
if (newValue)
|
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.");
|
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.");
|
||||||
|
@ -214,9 +214,8 @@ namespace PluralKit.Bot
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _db.Execute(c =>
|
var patch = new SystemGuildPatch {ProxyEnabled = newValue};
|
||||||
c.ExecuteAsync("update system_guild set proxy_enabled = @newValue where system = @system and guild = @guild",
|
await _db.Execute(conn => conn.UpsertSystemGuild(ctx.System.Id, ctx.Guild.Id, patch));
|
||||||
new {newValue, system = ctx.System.Id, guild = ctx.Guild.Id}));
|
|
||||||
|
|
||||||
if (newValue)
|
if (newValue)
|
||||||
await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **enabled** for your system.");
|
await ctx.Reply($"Message proxying in this server ({ctx.Guild.Name.EscapeMarkdown()}) is now **enabled** for your system.");
|
||||||
|
16
PluralKit.Core/Models/Patch/GuildPatch.cs
Normal file
16
PluralKit.Core/Models/Patch/GuildPatch.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
namespace PluralKit.Core
|
||||||
|
{
|
||||||
|
public class GuildPatch: PatchObject
|
||||||
|
{
|
||||||
|
public Partial<ulong?> LogChannel { get; set; }
|
||||||
|
public Partial<ulong[]> LogBlacklist { get; set; }
|
||||||
|
public Partial<ulong[]> Blacklist { get; set; }
|
||||||
|
public Partial<bool> LogCleanupEnabled { get; set; }
|
||||||
|
|
||||||
|
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||||
|
.With("log_channel", LogChannel)
|
||||||
|
.With("log_blacklist", LogBlacklist)
|
||||||
|
.With("blacklist", Blacklist)
|
||||||
|
.With("log_cleanup_enabled", LogCleanupEnabled);
|
||||||
|
}
|
||||||
|
}
|
13
PluralKit.Core/Models/Patch/MemberGuildPatch.cs
Normal file
13
PluralKit.Core/Models/Patch/MemberGuildPatch.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#nullable enable
|
||||||
|
namespace PluralKit.Core
|
||||||
|
{
|
||||||
|
public class MemberGuildPatch: PatchObject
|
||||||
|
{
|
||||||
|
public Partial<string?> DisplayName { get; set; }
|
||||||
|
public Partial<string?> AvatarUrl { get; set; }
|
||||||
|
|
||||||
|
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||||
|
.With("display_name", DisplayName)
|
||||||
|
.With("avatar_url", AvatarUrl);
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,7 @@ namespace PluralKit.Core
|
|||||||
{
|
{
|
||||||
public static Task<PKSystem> UpdateSystem(this IPKConnection conn, SystemId id, SystemPatch patch)
|
public static Task<PKSystem> UpdateSystem(this IPKConnection conn, SystemId id, SystemPatch patch)
|
||||||
{
|
{
|
||||||
var (query, pms) = patch.Apply(new UpdateQueryBuilder("systems", "id = @id"))
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("systems", "id = @id"))
|
||||||
.WithConstant("id", id)
|
.WithConstant("id", id)
|
||||||
.Build("returning *");
|
.Build("returning *");
|
||||||
return conn.QueryFirstAsync<PKSystem>(query, pms);
|
return conn.QueryFirstAsync<PKSystem>(query, pms);
|
||||||
@ -24,7 +24,7 @@ namespace PluralKit.Core
|
|||||||
|
|
||||||
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
|
public static Task<PKMember> UpdateMember(this IPKConnection conn, MemberId id, MemberPatch patch)
|
||||||
{
|
{
|
||||||
var (query, pms) = patch.Apply(new UpdateQueryBuilder("members", "id = @id"))
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("members", "id = @id"))
|
||||||
.WithConstant("id", id)
|
.WithConstant("id", id)
|
||||||
.Build("returning *");
|
.Build("returning *");
|
||||||
return conn.QueryFirstAsync<PKMember>(query, pms);
|
return conn.QueryFirstAsync<PKMember>(query, pms);
|
||||||
@ -32,5 +32,33 @@ namespace PluralKit.Core
|
|||||||
|
|
||||||
public static Task DeleteMember(this IPKConnection conn, MemberId id) =>
|
public static Task DeleteMember(this IPKConnection conn, MemberId id) =>
|
||||||
conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
|
conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
|
||||||
|
|
||||||
|
public static Task UpsertSystemGuild(this IPKConnection conn, SystemId system, ulong guild,
|
||||||
|
SystemGuildPatch patch)
|
||||||
|
{
|
||||||
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Upsert("system_guild", "system, guild"))
|
||||||
|
.WithConstant("system", system)
|
||||||
|
.WithConstant("guild", guild)
|
||||||
|
.Build();
|
||||||
|
return conn.ExecuteAsync(query, pms);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task UpsertMemberGuild(this IPKConnection conn, MemberId member, ulong guild,
|
||||||
|
MemberGuildPatch patch)
|
||||||
|
{
|
||||||
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Upsert("member_guild", "member, guild"))
|
||||||
|
.WithConstant("member", member)
|
||||||
|
.WithConstant("guild", guild)
|
||||||
|
.Build();
|
||||||
|
return conn.ExecuteAsync(query, pms);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task UpsertGuild(this IPKConnection conn, ulong guild, GuildPatch patch)
|
||||||
|
{
|
||||||
|
var (query, pms) = patch.Apply(UpdateQueryBuilder.Upsert("servers", "id"))
|
||||||
|
.WithConstant("id", guild)
|
||||||
|
.Build();
|
||||||
|
return conn.ExecuteAsync(query, pms);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
PluralKit.Core/Models/Patch/SystemGuildPatch.cs
Normal file
15
PluralKit.Core/Models/Patch/SystemGuildPatch.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#nullable enable
|
||||||
|
namespace PluralKit.Core
|
||||||
|
{
|
||||||
|
public class SystemGuildPatch: PatchObject
|
||||||
|
{
|
||||||
|
public Partial<bool> ProxyEnabled { get; set; }
|
||||||
|
public Partial<AutoproxyMode> AutoproxyMode { get; set; }
|
||||||
|
public Partial<MemberId?> AutoproxyMember { get; set; }
|
||||||
|
|
||||||
|
public override UpdateQueryBuilder Apply(UpdateQueryBuilder b) => b
|
||||||
|
.With("proxy_enabled", ProxyEnabled)
|
||||||
|
.With("autoproxy_mode", AutoproxyMode)
|
||||||
|
.With("autoproxy_member", AutoproxyMember);
|
||||||
|
}
|
||||||
|
}
|
@ -71,7 +71,7 @@ namespace PluralKit.Core
|
|||||||
if (Type == QueryType.Update && _condition != null)
|
if (Type == QueryType.Update && _condition != null)
|
||||||
query.Append($" where {_condition}");
|
query.Append($" where {_condition}");
|
||||||
|
|
||||||
if (suffix != null)
|
if (!string.IsNullOrEmpty(suffix))
|
||||||
query.Append($" {suffix}");
|
query.Append($" {suffix}");
|
||||||
query.Append(";");
|
query.Append(";");
|
||||||
|
|
||||||
|
@ -6,34 +6,29 @@ namespace PluralKit.Core
|
|||||||
{
|
{
|
||||||
public class UpdateQueryBuilder
|
public class UpdateQueryBuilder
|
||||||
{
|
{
|
||||||
private readonly string _table;
|
private readonly QueryBuilder _qb;
|
||||||
private readonly string _condition;
|
|
||||||
private readonly DynamicParameters _params = new DynamicParameters();
|
private readonly DynamicParameters _params = new DynamicParameters();
|
||||||
|
|
||||||
private bool _hasFields = false;
|
private UpdateQueryBuilder(QueryBuilder qb)
|
||||||
private readonly StringBuilder _setClause = new StringBuilder();
|
|
||||||
|
|
||||||
public UpdateQueryBuilder(string table, string condition)
|
|
||||||
{
|
{
|
||||||
_table = table;
|
_qb = qb;
|
||||||
_condition = condition;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static UpdateQueryBuilder Insert(string table) => new UpdateQueryBuilder(QueryBuilder.Insert(table));
|
||||||
|
public static UpdateQueryBuilder Update(string table, string condition) => new UpdateQueryBuilder(QueryBuilder.Update(table, condition));
|
||||||
|
public static UpdateQueryBuilder Upsert(string table, string conflictField) => new UpdateQueryBuilder(QueryBuilder.Upsert(table, conflictField));
|
||||||
|
|
||||||
public UpdateQueryBuilder WithConstant<T>(string name, T value)
|
public UpdateQueryBuilder WithConstant<T>(string name, T value)
|
||||||
{
|
{
|
||||||
_params.Add(name, value);
|
_params.Add(name, value);
|
||||||
|
_qb.Constant(name, $"@{name}");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UpdateQueryBuilder With<T>(string columnName, T value)
|
public UpdateQueryBuilder With<T>(string columnName, T value)
|
||||||
{
|
{
|
||||||
_params.Add(columnName, value);
|
_params.Add(columnName, value);
|
||||||
|
_qb.Variable(columnName, $"@{columnName}");
|
||||||
if (_hasFields)
|
|
||||||
_setClause.Append(", ");
|
|
||||||
else _hasFields = true;
|
|
||||||
|
|
||||||
_setClause.Append($"{columnName} = @{columnName}");
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +37,9 @@ namespace PluralKit.Core
|
|||||||
return partialValue.IsPresent ? With(columnName, partialValue.Value) : this;
|
return partialValue.IsPresent ? With(columnName, partialValue.Value) : this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public (string Query, DynamicParameters Parameters) Build(string append = "")
|
public (string Query, DynamicParameters Parameters) Build(string suffix = "")
|
||||||
{
|
{
|
||||||
var query = $"update {_table} set {_setClause} where {_condition} {append}";
|
return (_qb.Build(suffix), _params);
|
||||||
return (query, _params);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user