mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-07 06:17:55 +00:00
feat(bot): basic webhook username templating
This commit is contained in:
parent
87196e3297
commit
4248b7bc32
16 changed files with 78 additions and 14 deletions
|
|
@ -25,6 +25,7 @@ public class MessageContext
|
|||
public string? SystemTag { get; }
|
||||
public string? SystemGuildTag { get; }
|
||||
public bool TagEnabled { get; }
|
||||
public string? NameFormat { get; }
|
||||
public string? SystemAvatar { get; }
|
||||
public string? SystemGuildAvatar { get; }
|
||||
public bool AllowAutoproxy { get; }
|
||||
|
|
|
|||
|
|
@ -31,17 +31,23 @@ public class ProxyMember
|
|||
public bool AllowAutoproxy { get; }
|
||||
public string? Color { get; }
|
||||
|
||||
// If not set, this formatting will be applied to the proxy name
|
||||
public static string DefaultFormat = "{name} {tag}";
|
||||
|
||||
public static string FormatTag(string template, string tag, string name) => StringUtils.SafeFormat(template, new[] {
|
||||
("{tag}", tag),
|
||||
("{name}", name)
|
||||
});
|
||||
|
||||
public string ProxyName(MessageContext ctx)
|
||||
{
|
||||
// TODO: if tag is null it should still format but only if it appears in the formatting.
|
||||
var memberName = ServerName ?? DisplayName ?? Name;
|
||||
if (!ctx.TagEnabled)
|
||||
var tag = ctx.SystemGuildTag ?? ctx.SystemTag;
|
||||
if (!ctx.TagEnabled || tag == null)
|
||||
return memberName;
|
||||
|
||||
if (ctx.SystemGuildTag != null)
|
||||
return $"{memberName} {ctx.SystemGuildTag}";
|
||||
if (ctx.SystemTag != null)
|
||||
return $"{memberName} {ctx.SystemTag}";
|
||||
return memberName;
|
||||
return FormatTag(ctx.NameFormat ?? DefaultFormat, tag, memberName);
|
||||
}
|
||||
|
||||
public string? ProxyAvatar(MessageContext ctx) => ServerAvatar ?? WebhookAvatar ?? Avatar ?? ctx.SystemGuildAvatar ?? ctx.SystemAvatar;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
create function message_context(account_id bigint, guild_id bigint, channel_id bigint, thread_id bigint)
|
||||
create function message_context(account_id bigint, guild_id bigint, channel_id bigint, thread_id bigint)
|
||||
returns table (
|
||||
allow_autoproxy bool,
|
||||
|
||||
|
|
@ -10,6 +10,7 @@
|
|||
case_sensitive_proxy_tags bool,
|
||||
proxy_error_message_enabled bool,
|
||||
proxy_switch bool,
|
||||
name_format text,
|
||||
|
||||
tag_enabled bool,
|
||||
proxy_enabled bool,
|
||||
|
|
@ -42,6 +43,7 @@ as $$
|
|||
system_config.case_sensitive_proxy_tags as case_sensitive_proxy_tags,
|
||||
system_config.proxy_error_message_enabled as proxy_error_message_enabled,
|
||||
system_config.proxy_switch as proxy_switch,
|
||||
system_config.name_format as name_format,
|
||||
|
||||
-- system_guild table
|
||||
coalesce(system_guild.tag_enabled, true) as tag_enabled,
|
||||
|
|
@ -174,4 +176,4 @@ begin
|
|||
if not exists (select 1 from groups where hid = new_hid) then return new_hid; end if;
|
||||
end loop;
|
||||
end
|
||||
$$ language plpgsql volatile;
|
||||
$$ language plpgsql volatile;
|
||||
6
PluralKit.Core/Database/Migrations/47.sql
Normal file
6
PluralKit.Core/Database/Migrations/47.sql
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
-- database version 47
|
||||
-- add config setting for supplying a custom tag format in names
|
||||
|
||||
alter table system_config add column name_format text;
|
||||
|
||||
update info set schema_version = 47;
|
||||
|
|
@ -9,7 +9,7 @@ namespace PluralKit.Core;
|
|||
internal class DatabaseMigrator
|
||||
{
|
||||
private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files
|
||||
private const int TargetSchemaVersion = 46;
|
||||
private const int TargetSchemaVersion = 47;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public DatabaseMigrator(ILogger logger)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ public class SystemConfigPatch: PatchObject
|
|||
public Partial<bool> ProxyErrorMessageEnabled { get; set; }
|
||||
public Partial<bool> HidDisplaySplit { get; set; }
|
||||
public Partial<bool> HidDisplayCaps { get; set; }
|
||||
public Partial<string?> NameFormat { get; set; }
|
||||
public Partial<SystemConfig.HidPadFormat> HidListPadding { get; set; }
|
||||
public Partial<bool> ProxySwitch { get; set; }
|
||||
|
||||
|
|
@ -40,6 +41,7 @@ public class SystemConfigPatch: PatchObject
|
|||
.With("hid_display_caps", HidDisplayCaps)
|
||||
.With("hid_list_padding", HidListPadding)
|
||||
.With("proxy_switch", ProxySwitch)
|
||||
.With("name_format", NameFormat)
|
||||
);
|
||||
|
||||
public new void AssertIsValid()
|
||||
|
|
@ -107,6 +109,9 @@ public class SystemConfigPatch: PatchObject
|
|||
if (ProxySwitch.IsPresent)
|
||||
o.Add("proxy_switch", ProxySwitch.Value);
|
||||
|
||||
if (NameFormat.IsPresent)
|
||||
o.Add("name_format", NameFormat.Value);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
|
@ -147,6 +152,9 @@ public class SystemConfigPatch: PatchObject
|
|||
if (o.ContainsKey("proxy_switch"))
|
||||
patch.ProxySwitch = o.Value<bool>("proxy_switch");
|
||||
|
||||
if (o.ContainsKey("name_format"))
|
||||
patch.NameFormat = o.Value<string>("name_format");
|
||||
|
||||
return patch;
|
||||
}
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@ public class SystemConfig
|
|||
public bool HidDisplayCaps { get; }
|
||||
public HidPadFormat HidListPadding { get; }
|
||||
public bool ProxySwitch { get; }
|
||||
public string NameFormat { get; }
|
||||
|
||||
public enum HidPadFormat
|
||||
{
|
||||
|
|
@ -54,6 +55,7 @@ public static class SystemConfigExt
|
|||
o.Add("hid_display_caps", cfg.HidDisplayCaps);
|
||||
o.Add("hid_list_padding", cfg.HidListPadding.ToUserString());
|
||||
o.Add("proxy_switch", cfg.ProxySwitch);
|
||||
o.Add("name_format", cfg.NameFormat);
|
||||
|
||||
o.Add("description_templates", JArray.FromObject(cfg.DescriptionTemplates));
|
||||
|
||||
|
|
|
|||
|
|
@ -86,4 +86,10 @@ public static class StringUtils
|
|||
|
||||
return output;
|
||||
}
|
||||
|
||||
// Lightweight formatting that intentionally is very basic to not have silly things like in-template for loops like other templating engines seem to have
|
||||
// Currently doesn't handle escapes which might cause problems
|
||||
public static string SafeFormat(string template, (string pattern, string arg)[] args) =>
|
||||
args
|
||||
.Aggregate(template, (acc, x) => acc.Replace(x.pattern, x.arg));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue