Extract system/member guild settings and refactor DB access

(also refactor MemberAvatar now that I'm here)
This commit is contained in:
Ske
2020-06-13 16:03:57 +02:00
parent ed511a6236
commit a915ddb41c
13 changed files with 247 additions and 272 deletions

View File

@@ -1,6 +1,10 @@
#nullable enable
using System;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using DSharpPlus;
using DSharpPlus.Entities;
@@ -10,157 +14,145 @@ namespace PluralKit.Bot
{
public class MemberAvatar
{
private IDataStore _data;
private readonly DbConnectionFactory _db;
public MemberAvatar(IDataStore data)
public MemberAvatar(DbConnectionFactory db)
{
_data = data;
}
public async Task Avatar(Context ctx, PKMember target)
{
var guildData = ctx.Guild != null ? await _data.GetMemberGuildSettings(target, ctx.Guild.Id) : null;
if (ctx.Match("clear", "remove", "reset") || ctx.MatchFlag("c", "clear"))
{
if (ctx.System == null) throw Errors.NoSystemError;
if (target.System != ctx.System.Id) throw Errors.NotOwnMemberError;
target.AvatarUrl = null;
await _data.SaveMember(target);
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.");
return;
}
if (ctx.RemainderOrNull() == null && ctx.Message.Attachments.Count == 0)
{
if ((target.AvatarUrl?.Trim() ?? "").Length > 0)
{
var eb = new DiscordEmbedBuilder()
.WithTitle($"{target.Name.SanitizeMentions()}'s avatar")
.WithImageUrl(target.AvatarUrl);
if (target.System == ctx.System?.Id)
eb.WithDescription($"To clear, use `pk;member {target.Hid} avatar -clear`.");
await ctx.Reply(embed: eb.Build());
}
else
{
if (target.System == ctx.System?.Id)
throw new PKSyntaxError($"This member does not have an avatar set. Set one by attaching an image to this command, or by passing an image URL or @mention.");
throw new PKError($"This member does not have an avatar set.");
}
return;
}
var user = await ctx.MatchUser();
if (ctx.System == null) throw Errors.NoSystemError;
if (target.System != ctx.System.Id) throw Errors.NotOwnMemberError;
else if (user != null)
{
if (user.AvatarHash == null) throw Errors.UserHasNoAvatar;
target.AvatarUrl = user.GetAvatarUrl(ImageFormat.Png, size: 256);
await _data.SaveMember(target);
var embed = new DiscordEmbedBuilder().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 member's avatar will need to be re-set.", embed: embed);
}
else if (ctx.RemainderOrNull() is string url)
{
if (url.Length > Limits.MaxUriLength) throw Errors.InvalidUrl(url);
await AvatarUtils.VerifyAvatarOrThrow(url);
target.AvatarUrl = url;
await _data.SaveMember(target);
var embed = new DiscordEmbedBuilder().WithImageUrl(url).Build();
await ctx.Reply($"{Emojis.Success} Member avatar changed.", embed: embed);
}
else if (ctx.Message.Attachments.FirstOrDefault() is DiscordAttachment attachment)
{
await AvatarUtils.VerifyAvatarOrThrow(attachment.Url);
target.AvatarUrl = attachment.Url;
await _data.SaveMember(target);
await ctx.Reply($"{Emojis.Success} Member avatar changed to attached image. 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
_db = db;
}
public async Task ServerAvatar(Context ctx, PKMember target)
private async Task AvatarClear(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? mgs)
{
ctx.CheckGuildContext();
var guildData = await _data.GetMemberGuildSettings(target, ctx.Guild.Id);
if (ctx.Match("clear", "remove", "reset") || ctx.MatchFlag("c", "clear"))
ctx.CheckSystem().CheckOwnMember(target);
await UpdateAvatar(location, ctx, target, null);
if (location == AvatarLocation.Server)
{
if (ctx.System == null) throw Errors.NoSystemError;
if (target.System != ctx.System.Id) throw Errors.NotOwnMemberError;
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.");
return;
}
if (ctx.RemainderOrNull() == null && ctx.Message.Attachments.Count == 0)
else
{
if ((guildData.AvatarUrl?.Trim() ?? "").Length > 0)
if (mgs?.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.");
}
}
private async Task AvatarShow(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? guildData)
{
var field = location == AvatarLocation.Server ? $"server avatar (for {ctx.Guild.Name})" : "avatar";
var cmd = location == AvatarLocation.Server ? "serveravatar" : "avatar";
var currentValue = location == AvatarLocation.Member ? target.AvatarUrl : guildData?.AvatarUrl;
if (string.IsNullOrEmpty(currentValue))
{
if (location == AvatarLocation.Member)
{
var eb = new DiscordEmbedBuilder()
.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());
throw new PKSyntaxError("This member does not have an avatar set. Set one by attaching an image to this command, or by passing an image URL or @mention.");
throw new PKError("This member does not have an avatar set.");
}
else
if (location == AvatarLocation.Server)
throw new PKError($"This member does not have a server avatar set. Type `pk;member {target.Hid} avatar` to see their global avatar.");
return;
}
var user = await ctx.MatchUser();
if (ctx.System == null) throw Errors.NoSystemError;
if (target.System != ctx.System.Id) throw Errors.NotOwnMemberError;
if (user != null)
{
if (user.AvatarHash == null) throw Errors.UserHasNoAvatar;
guildData.AvatarUrl = user.GetAvatarUrl(ImageFormat.Png, size: 256);
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
var eb = new DiscordEmbedBuilder()
.WithTitle($"{target.Name.SanitizeMentions()}'s {field}")
.WithImageUrl(currentValue);
if (target.System == ctx.System?.Id)
eb.WithDescription($"To clear, use `pk;member {target.Hid} {cmd} clear`.");
await ctx.Reply(embed: eb.Build());
}
private async Task AvatarFromUser(AvatarLocation location, Context ctx, PKMember target, DiscordUser user)
{
if (user.AvatarHash == null) throw Errors.UserHasNoAvatar;
var embed = new DiscordEmbedBuilder().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)
{
if (url.Length > Limits.MaxUriLength) throw Errors.InvalidUrl(url);
await AvatarUtils.VerifyAvatarOrThrow(url);
guildData.AvatarUrl = url;
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
var url = user.GetAvatarUrl(ImageFormat.Png, 256);
await UpdateAvatar(location, ctx, target, url);
var embed = new DiscordEmbedBuilder().WithImageUrl(url).Build();
if (location == AvatarLocation.Server)
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 (location == AvatarLocation.Member)
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 member's avatar will need to be re-set.", embed: embed);
}
var embed = new DiscordEmbedBuilder().WithImageUrl(url).Build();
private async Task AvatarFromArg(AvatarLocation location, Context ctx, PKMember target, string url)
{
if (url.Length > Limits.MaxUriLength) throw Errors.InvalidUrl(url);
await AvatarUtils.VerifyAvatarOrThrow(url);
await UpdateAvatar(location, ctx, target, url);
var embed = new DiscordEmbedBuilder().WithImageUrl(url).Build();
if (location == AvatarLocation.Server)
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 DiscordAttachment attachment)
{
await AvatarUtils.VerifyAvatarOrThrow(attachment.Url);
guildData.AvatarUrl = attachment.Url;
await _data.SetMemberGuildSettings(target, ctx.Guild.Id, guildData);
}
private async Task AvatarFromAttachment(AvatarLocation location, Context ctx, PKMember target, DiscordAttachment attachment)
{
await AvatarUtils.VerifyAvatarOrThrow(attachment.Url);
await UpdateAvatar(location, ctx, target, attachment.Url);
if (location == AvatarLocation.Server)
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
else if (location == AvatarLocation.Member)
await ctx.Reply($"{Emojis.Success} Member avatar changed to attached image. Please note that if you delete the message containing the attachment, the avatar will stop working.");
}
public async Task ServerAvatar(Context ctx, PKMember target)
{
ctx.CheckGuildContext();
var guildData = await _db.Execute(c => c.QueryOrInsertMemberGuildConfig(ctx.Guild.Id, target.Id));
await AvatarCommandTree(AvatarLocation.Server, ctx, target, guildData);
}
public async Task Avatar(Context ctx, PKMember target)
{
var guildData = ctx.Guild != null ?
await _db.Execute(c => c.QueryOrInsertMemberGuildConfig(ctx.Guild.Id, target.Id))
: null;
await AvatarCommandTree(AvatarLocation.Member, ctx, target, guildData);
}
private async Task AvatarCommandTree(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? guildData)
{
if (ctx.Match("clear", "remove", "reset") || ctx.MatchFlag("c", "clear"))
await AvatarClear(location, ctx, target, guildData);
else if (ctx.RemainderOrNull() == null && ctx.Message.Attachments.Count == 0)
await AvatarShow(location, ctx, target, guildData);
else if (await ctx.MatchUser() is {} user)
await AvatarFromUser(location, ctx, target, user);
else if (ctx.RemainderOrNull() is {} url)
await AvatarFromArg(location, ctx, target, url);
else if (ctx.Message.Attachments.FirstOrDefault() is {} attachment)
await AvatarFromAttachment(location, ctx, target, attachment);
else throw new Exception("Unexpected condition when parsing avatar command");
}
private Task UpdateAvatar(AvatarLocation location, Context ctx, PKMember target, string? avatar) =>
location switch
{
AvatarLocation.Server => _db.Execute(c =>
c.ExecuteAsync(
"update member_guild set avatar_url = @Avatar where member = @Member and guild = @Guild",
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}")
};
private enum AvatarLocation
{
Member,
Server
}
}
}