Add server-specific member avatars

This commit is contained in:
Ske 2020-02-12 17:42:12 +01:00
parent 6d5004bf54
commit d0d3579b17
10 changed files with 108 additions and 42 deletions

View File

@ -34,6 +34,7 @@ namespace PluralKit.Bot
public static Command MemberProxy = new Command("member proxy", "member <member> proxy [add|remove] [example proxy]", "Changes, adds, or removes a member's proxy tags");
public static Command MemberDelete = new Command("member delete", "member <member> delete", "Deletes a member");
public static Command MemberAvatar = new Command("member avatar", "member <member> avatar [url|@mention|clear]", "Changes a member's avatar");
public static Command MemberServerAvatar = new Command("member serveravatar", "member <member> serveravatar [url|@mention|clear]", "Changes a member's avatar in the current server");
public static Command MemberDisplayName = new Command("member displayname", "member <member> displayname [display name]", "Changes a member's display name");
public static Command MemberServerName = new Command("member servername", "member <member> servername [server name]", "Changes a member's display name in the current server");
public static Command MemberKeepProxy = new Command("member keepproxy", "member <member> keepproxy [on|off]", "Sets whether to include a member's proxy tags when proxying");
@ -273,6 +274,8 @@ namespace PluralKit.Bot
await ctx.Execute<MemberEdit>(MemberDelete, m => m.Delete(ctx, target));
else if (ctx.Match("avatar", "profile", "picture", "icon", "image", "pfp", "pic"))
await ctx.Execute<MemberAvatar>(MemberAvatar, m => m.Avatar(ctx, target));
else if (ctx.Match("serveravatar", "servericon", "serverimage", "serverpfp", "serverpic", "savatar", "spic", "guildavatar", "guildpic", "guildicon", "sicon"))
await ctx.Execute<MemberAvatar>(MemberServerAvatar, m => m.ServerAvatar(ctx, target));
else if (ctx.Match("displayname", "dn", "dname", "nick", "nickname"))
await ctx.Execute<MemberEdit>(MemberDisplayName, m => m.DisplayName(ctx, target));
else if (ctx.Match("servername", "sn", "sname", "snick", "snickname", "servernick", "servernickname", "serverdisplayname", "guildname", "guildnick", "guildnickname", "serverdn"))

View File

@ -18,6 +18,8 @@ namespace PluralKit.Bot
public async Task Avatar(Context ctx, PKMember target)
{
var guildData = ctx.Guild != null ? await _data.GetMemberGuildSettings(target, ctx.Guild.Id) : null;
if (ctx.RemainderOrNull() == null && ctx.Message.Attachments.Count == 0)
{
if ((target.AvatarUrl?.Trim() ?? "").Length > 0)
@ -42,11 +44,15 @@ namespace PluralKit.Bot
if (ctx.System == null) throw Errors.NoSystemError;
if (target.System != ctx.System.Id) throw Errors.NotOwnMemberError;
if (ctx.Match("clear", "remove"))
if (ctx.Match("clear", "remove", "reset"))
{
target.AvatarUrl = null;
await _data.SaveMember(target);
await ctx.Reply($"{Emojis.Success} Member avatar cleared.");
if (guildData?.AvatarUrl != null)
await ctx.Reply($"{Emojis.Success} Member avatar cleared. Note that this member has a server-specific avatar set here, type `pk;member {target.Hid} serveravatar clear` if you wish to clear that too.");
else
await ctx.Reply($"{Emojis.Success} Member avatar cleared.");
}
else if (await ctx.MatchUser() is IUser user)
{
@ -57,8 +63,7 @@ namespace PluralKit.Bot
var embed = new EmbedBuilder().WithImageUrl(target.AvatarUrl).Build();
await ctx.Reply(
$"{Emojis.Success} Member avatar changed to {user.Username}'s avatar! {Emojis.Warn} Please note that if {user.Username} changes their avatar, the webhook's avatar will need to be re-set.", embed: embed);
$"{Emojis.Success} Member avatar changed to {user.Username}'s avatar! {Emojis.Warn} Please note that if {user.Username} changes their avatar, the member's avatar will need to be re-set.", embed: embed);
}
else if (ctx.RemainderOrNull() is string url)
{
@ -79,5 +84,70 @@ namespace PluralKit.Bot
}
// No-arguments no-attachment case covered by conditional at the very top
}
public async Task ServerAvatar(Context ctx, PKMember target)
{
ctx.CheckGuildContext();
var guildData = await _data.GetMemberGuildSettings(target, ctx.Guild.Id);
if (ctx.RemainderOrNull() == null && ctx.Message.Attachments.Count == 0)
{
if ((guildData.AvatarUrl?.Trim() ?? "").Length > 0)
{
var eb = new EmbedBuilder()
.WithTitle($"{target.Name.SanitizeMentions()}'s server avatar (for {ctx.Guild.Name})")
.WithImageUrl(guildData.AvatarUrl);
if (target.System == ctx.System?.Id)
eb.WithDescription($"To clear, use `pk;member {target.Hid} serveravatar clear`.");
await ctx.Reply(embed: eb.Build());
}
else
throw new PKError($"This member does not have a server avatar set. Type `pk;member {target.Hid} avatar` to see their global avatar.");
return;
}
if (ctx.System == null) throw Errors.NoSystemError;
if (target.System != ctx.System.Id) throw Errors.NotOwnMemberError;
if (ctx.Match("clear", "remove", "reset"))
{
guildData.AvatarUrl = null;
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
if (target.AvatarUrl != null)
await ctx.Reply($"{Emojis.Success} Member server avatar cleared. This member will now use the global avatar in this server (**{ctx.Guild.Name}**).");
else
await ctx.Reply($"{Emojis.Success} Member server avatar cleared. This member now has no avatar.");
}
else if (await ctx.MatchUser() is IUser user)
{
if (user.AvatarId == null) throw Errors.UserHasNoAvatar;
guildData.AvatarUrl = user.GetAvatarUrl(ImageFormat.Png, size: 256);
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
var embed = new EmbedBuilder().WithImageUrl(guildData.AvatarUrl).Build();
await ctx.Reply(
$"{Emojis.Success} Member server avatar changed to {user.Username}'s avatar! This avatar will now be used when proxying in this server (**{ctx.Guild.Name}**). {Emojis.Warn} Please note that if {user.Username} changes their avatar, the member's server avatar will need to be re-set.", embed: embed);
}
else if (ctx.RemainderOrNull() is string url)
{
await AvatarUtils.VerifyAvatarOrThrow(url);
guildData.AvatarUrl = url;
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
var embed = new EmbedBuilder().WithImageUrl(url).Build();
await ctx.Reply($"{Emojis.Success} Member server avatar changed. This avatar will now be used when proxying in this server (**{ctx.Guild.Name}**).", embed: embed);
}
else if (ctx.Message.Attachments.FirstOrDefault() is Attachment attachment)
{
await AvatarUtils.VerifyAvatarOrThrow(attachment.Url);
guildData.AvatarUrl = attachment.Url;
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
await ctx.Reply($"{Emojis.Success} Member server avatar changed to attached image. This avatar will now be used when proxying in this server (**{ctx.Guild.Name}**). Please note that if you delete the message containing the attachment, the avatar will stop working.");
}
// No-arguments no-attachment case covered by conditional at the very top
}
}
}

View File

@ -92,21 +92,28 @@ namespace PluralKit.Bot {
var messageCount = await _data.GetMemberMessageCount(member);
string guildDisplayName = null;
if (guild != null)
guildDisplayName = (await _data.GetMemberGuildSettings(member, guild.Id)).DisplayName;
var guildSettings = guild != null ? await _data.GetMemberGuildSettings(member, guild.Id) : null;
var guildDisplayName = guildSettings?.DisplayName;
var avatar = guildSettings?.AvatarUrl ?? member.AvatarUrl;
var proxyTagsStr = string.Join('\n', member.ProxyTags.Select(t => $"`{t.ProxyString}`"));
var eb = new EmbedBuilder()
// TODO: add URL of website when that's up
.WithAuthor(name, member.AvatarUrl)
.WithAuthor(name, avatar)
.WithColor(member.MemberPrivacy.CanAccess(ctx) ? color : Color.Default)
.WithFooter($"System ID: {system.Hid} | Member ID: {member.Hid} | Created on {DateTimeFormats.ZonedDateTimeFormat.Format(member.Created.InZone(system.Zone))}");
if (member.MemberPrivacy == PrivacyLevel.Private) eb.WithDescription("*(this member is private)*");
var description = "";
if (member.MemberPrivacy == PrivacyLevel.Private) description += "*(this member is private)*\n";
if (guildSettings?.AvatarUrl != null)
if (member.AvatarUrl != null)
description += $"*(this member has a server-specific avatar set; [click here]({member.AvatarUrl}) to see the global avatar)*\n";
else
description += "*(this member has a server-specific avatar set)*\n";
if (description != "") eb.WithDescription(description);
if (member.AvatarUrl != null) eb.WithThumbnailUrl(member.AvatarUrl);
if (avatar != null) eb.WithThumbnailUrl(avatar);
if (member.DisplayName != null) eb.AddField("Display Name", member.DisplayName.Truncate(1024), true);
if (guild != null && guildDisplayName != null) eb.AddField($"Server Nickname (for {guild.Name})", guildDisplayName.Truncate(1024), true);

View File

@ -121,7 +121,7 @@ namespace PluralKit.Bot
// Get variables in order and all
var proxyName = match.Member.ProxyName(match.System.Tag, memberSettingsForGuild.DisplayName);
var avatarUrl = match.Member.AvatarUrl ?? match.System.AvatarUrl;
var avatarUrl = memberSettingsForGuild.AvatarUrl ?? match.Member.AvatarUrl ?? match.System.AvatarUrl;
// If the name's too long (or short), bail
if (proxyName.Length < 2) throw Errors.ProxyNameTooShort(proxyName);

View File

@ -0,0 +1,3 @@
-- SCHEMA VERSION 4: 2020-02-12
alter table member_guild add column avatar_url text;
update info set schema_version = 4;

View File

@ -83,15 +83,8 @@ namespace PluralKit.Core {
public int Member { get; set; }
public ulong Guild { get; set; }
public string DisplayName { get; set; }
public string AvatarUrl { get; set; }
}
public class AuxillaryProxyInformation
{
public GuildConfig Guild { get; set; }
public SystemGuildSettings SystemGuild { get; set; }
public MemberGuildSettings MemberGuild { get; set; }
}
public interface IDataStore
{
/// <summary>
@ -416,7 +409,5 @@ namespace PluralKit.Core {
/// Saves the given guild configuration struct to the data store.
/// </summary>
Task SaveGuildConfig(GuildConfig cfg);
Task<AuxillaryProxyInformation> GetAuxillaryProxyInformation(ulong guild, PKSystem system, PKMember member);
}
}

View File

@ -249,15 +249,15 @@ namespace PluralKit.Core {
using var conn = await _conn.Obtain();
return await conn.QuerySingleOrDefaultAsync<MemberGuildSettings>(
"select * from member_guild where member = @Member and guild = @Guild", new { Member = member.Id, Guild = guild})
?? new MemberGuildSettings();
?? new MemberGuildSettings { Guild = guild, Member = member.Id };
}
public async Task SetMemberGuildSettings(PKMember member, ulong guild, MemberGuildSettings settings)
{
using var conn = await _conn.Obtain();
await conn.ExecuteAsync(
"insert into member_guild (member, guild, display_name) values (@Member, @Guild, @DisplayName) on conflict (member, guild) do update set display_name = @Displayname",
new {Member = member.Id, Guild = guild, DisplayName = settings.DisplayName});
"insert into member_guild (member, guild, display_name, avatar_url) values (@Member, @Guild, @DisplayName, @AvatarUrl) on conflict (member, guild) do update set display_name = @DisplayName, avatar_url = @AvatarUrl",
settings);
await _cache.InvalidateSystem(member.System);
}
@ -397,23 +397,6 @@ namespace PluralKit.Core {
_cache.InvalidateGuild(cfg.Id);
}
public async Task<AuxillaryProxyInformation> GetAuxillaryProxyInformation(ulong guild, PKSystem system, PKMember member)
{
using var conn = await _conn.Obtain();
var args = new {Guild = guild, System = system.Id, Member = member.Id};
var multi = await conn.QueryMultipleAsync(@"
select servers.* from servers where id = @Guild;
select * from system_guild where guild = @Guild and system = @System;
select * from member_guild where guild = @Guild and member = @Member", args);
return new AuxillaryProxyInformation
{
Guild = (await multi.ReadSingleOrDefaultAsync<DatabaseCompatibleGuildConfig>()).Into(),
SystemGuild = await multi.ReadSingleOrDefaultAsync<SystemGuildSettings>() ?? new SystemGuildSettings(),
MemberGuild = await multi.ReadSingleOrDefaultAsync<MemberGuildSettings>() ?? new MemberGuildSettings()
};
}
public async Task<PKMember> GetFirstFronter(PKSystem system)
{
// TODO: move to extension method since it doesn't rely on internals

View File

@ -11,7 +11,7 @@ using Serilog;
namespace PluralKit.Core {
public class SchemaService
{
private const int TargetSchemaVersion = 3;
private const int TargetSchemaVersion = 4;
private DbConnectionFactory _conn;
private ILogger _logger;

View File

@ -217,6 +217,14 @@ To preview the current avatar (if one is set), use the command with no arguments
To clear your avatar, use the subcommand `avatar clear` (eg. `pk;member John avatar clear`).
### Member server avatar
You can also set an avatar for a specific server. This will "override" the normal avatar, and will be used when proxying messages and looking up member cards in that server. To do so, use the `pk;member serveravatar` command, in the same way as the normal avatar command above:
pk;member John serveravatar
pk;member John serveravatar http://placebeard.it/512.jpg
pk;member "Craig Johnson" serveravatar (with an attached image)
pk;member John serveravatar clear
### Member pronouns
If you want to list a member's preferred pronouns, you can use the pronouns field on a member profile. This is a free text field, so you can put whatever you'd like in there (with a 100 character limit), like so:

View File

@ -36,6 +36,7 @@ Words in \<angle brackets> are *required parameters*. Words in [square brackets]
- `pk;member <name> servername <new server name>` - Changes the display name of a member, only in the current serve.
- `pk;member <name> description [description]` - Changes the description of a member.
- `pk;member <name> avatar <avatar url|@mention|clear>` - Changes the avatar of a member.
- `pk;member <name> serveravatar <avatar url|@mention|clear>` - Changes the avatar of a member in a specific server.
- `pk;member <name> proxy [tags]` - Changes the proxy tags of a member. use below add/remove commands for members with multiple tag pairs.
- `pk;member <name> proxy add [tags]` - Adds a proxy tag pair to a member.
- `pk;member <name> proxy remove [tags]` - Removes a proxy tag from a member.