diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index bbd2a7b2..1ad239b6 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -34,6 +34,7 @@ namespace PluralKit.Bot public static Command MemberProxy = new Command("member proxy", "member proxy [add|remove] [example proxy]", "Changes, adds, or removes a member's proxy tags"); public static Command MemberDelete = new Command("member delete", "member delete", "Deletes a member"); public static Command MemberAvatar = new Command("member avatar", "member avatar [url|@mention|clear]", "Changes a member's avatar"); + public static Command MemberServerAvatar = new Command("member serveravatar", "member serveravatar [url|@mention|clear]", "Changes a member's avatar in the current server"); public static Command MemberDisplayName = new Command("member displayname", "member displayname [display name]", "Changes a member's display name"); public static Command MemberServerName = new Command("member servername", "member servername [server name]", "Changes a member's display name in the current server"); public static Command MemberKeepProxy = new Command("member keepproxy", "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(MemberDelete, m => m.Delete(ctx, target)); else if (ctx.Match("avatar", "profile", "picture", "icon", "image", "pfp", "pic")) await ctx.Execute(MemberAvatar, m => m.Avatar(ctx, target)); + else if (ctx.Match("serveravatar", "servericon", "serverimage", "serverpfp", "serverpic", "savatar", "spic", "guildavatar", "guildpic", "guildicon", "sicon")) + await ctx.Execute(MemberServerAvatar, m => m.ServerAvatar(ctx, target)); else if (ctx.Match("displayname", "dn", "dname", "nick", "nickname")) await ctx.Execute(MemberDisplayName, m => m.DisplayName(ctx, target)); else if (ctx.Match("servername", "sn", "sname", "snick", "snickname", "servernick", "servernickname", "serverdisplayname", "guildname", "guildnick", "guildnickname", "serverdn")) diff --git a/PluralKit.Bot/Commands/MemberAvatar.cs b/PluralKit.Bot/Commands/MemberAvatar.cs index 2498cbcc..e39653bb 100644 --- a/PluralKit.Bot/Commands/MemberAvatar.cs +++ b/PluralKit.Bot/Commands/MemberAvatar.cs @@ -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 + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 383b6823..a5492ee8 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -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); diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index 4e73772e..c3fdda34 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -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); diff --git a/PluralKit.Core/Migrations/4.sql b/PluralKit.Core/Migrations/4.sql new file mode 100644 index 00000000..f4dbde15 --- /dev/null +++ b/PluralKit.Core/Migrations/4.sql @@ -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; \ No newline at end of file diff --git a/PluralKit.Core/Services/IDataStore.cs b/PluralKit.Core/Services/IDataStore.cs index 9529a282..710e2262 100644 --- a/PluralKit.Core/Services/IDataStore.cs +++ b/PluralKit.Core/Services/IDataStore.cs @@ -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 { /// @@ -416,7 +409,5 @@ namespace PluralKit.Core { /// Saves the given guild configuration struct to the data store. /// Task SaveGuildConfig(GuildConfig cfg); - - Task GetAuxillaryProxyInformation(ulong guild, PKSystem system, PKMember member); } } \ No newline at end of file diff --git a/PluralKit.Core/Services/PostgresDataStore.cs b/PluralKit.Core/Services/PostgresDataStore.cs index 80883a0b..4fcb8fc2 100644 --- a/PluralKit.Core/Services/PostgresDataStore.cs +++ b/PluralKit.Core/Services/PostgresDataStore.cs @@ -249,15 +249,15 @@ namespace PluralKit.Core { using var conn = await _conn.Obtain(); return await conn.QuerySingleOrDefaultAsync( "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 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()).Into(), - SystemGuild = await multi.ReadSingleOrDefaultAsync() ?? new SystemGuildSettings(), - MemberGuild = await multi.ReadSingleOrDefaultAsync() ?? new MemberGuildSettings() - }; - } - public async Task GetFirstFronter(PKSystem system) { // TODO: move to extension method since it doesn't rely on internals diff --git a/PluralKit.Core/Services/SchemaService.cs b/PluralKit.Core/Services/SchemaService.cs index 9233c7b6..a1d15741 100644 --- a/PluralKit.Core/Services/SchemaService.cs +++ b/PluralKit.Core/Services/SchemaService.cs @@ -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; diff --git a/docs/2-user-guide.md b/docs/2-user-guide.md index ad03ce75..39775d61 100644 --- a/docs/2-user-guide.md +++ b/docs/2-user-guide.md @@ -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: diff --git a/docs/3-command-list.md b/docs/3-command-list.md index 2e08c3d9..959ae420 100644 --- a/docs/3-command-list.md +++ b/docs/3-command-list.md @@ -36,6 +36,7 @@ Words in \ are *required parameters*. Words in [square brackets] - `pk;member servername ` - Changes the display name of a member, only in the current serve. - `pk;member description [description]` - Changes the description of a member. - `pk;member avatar ` - Changes the avatar of a member. +- `pk;member serveravatar ` - Changes the avatar of a member in a specific server. - `pk;member proxy [tags]` - Changes the proxy tags of a member. use below add/remove commands for members with multiple tag pairs. - `pk;member proxy add [tags]` - Adds a proxy tag pair to a member. - `pk;member proxy remove [tags]` - Removes a proxy tag from a member.