2020-04-24 19:50:28 +00:00
using System.Linq ;
2020-02-01 12:03:02 +00:00
using System.Threading.Tasks ;
2020-04-24 19:50:28 +00:00
using DSharpPlus ;
using DSharpPlus.Entities ;
2020-02-01 12:03:02 +00:00
2020-02-12 14:16:19 +00:00
using PluralKit.Core ;
2020-02-01 12:03:02 +00:00
2020-02-12 14:16:19 +00:00
namespace PluralKit.Bot
2020-02-01 12:03:02 +00:00
{
public class MemberAvatar
{
private IDataStore _data ;
2020-02-01 13:40:57 +00:00
public MemberAvatar ( IDataStore data )
2020-02-01 12:03:02 +00:00
{
_data = data ;
}
public async Task Avatar ( Context ctx , PKMember target )
{
2020-02-12 16:42:12 +00:00
var guildData = ctx . Guild ! = null ? await _data . GetMemberGuildSettings ( target , ctx . Guild . Id ) : null ;
2020-03-04 17:13:36 +00:00
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 ;
}
2020-02-01 12:03:02 +00:00
if ( ctx . RemainderOrNull ( ) = = null & & ctx . Message . Attachments . Count = = 0 )
{
if ( ( target . AvatarUrl ? . Trim ( ) ? ? "" ) . Length > 0 )
{
2020-04-24 19:50:28 +00:00
var eb = new DiscordEmbedBuilder ( )
2020-02-01 12:03:02 +00:00
. WithTitle ( $"{target.Name.SanitizeMentions()}'s avatar" )
. WithImageUrl ( target . AvatarUrl ) ;
if ( target . System = = ctx . System ? . Id )
2020-03-04 17:13:36 +00:00
eb . WithDescription ( $"To clear, use `pk;member {target.Hid} avatar -clear`." ) ;
2020-02-01 12:03:02 +00:00
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 ;
}
2020-04-24 19:50:28 +00:00
var user = await ctx . MatchUser ( ) ;
2020-02-01 12:03:02 +00:00
if ( ctx . System = = null ) throw Errors . NoSystemError ;
if ( target . System ! = ctx . System . Id ) throw Errors . NotOwnMemberError ;
2020-04-24 19:50:28 +00:00
else if ( user ! = null )
2020-02-01 12:03:02 +00:00
{
2020-05-01 15:38:16 +00:00
if ( user . AvatarHash = = null ) throw Errors . UserHasNoAvatar ;
2020-02-01 12:03:02 +00:00
target . AvatarUrl = user . GetAvatarUrl ( ImageFormat . Png , size : 256 ) ;
await _data . SaveMember ( target ) ;
2020-04-24 19:50:28 +00:00
var embed = new DiscordEmbedBuilder ( ) . WithImageUrl ( target . AvatarUrl ) . Build ( ) ;
2020-02-01 12:03:02 +00:00
await ctx . Reply (
2020-02-12 16:42:12 +00:00
$"{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 ) ;
2020-02-01 12:03:02 +00:00
}
else if ( ctx . RemainderOrNull ( ) is string url )
{
2020-05-05 14:04:17 +00:00
if ( url . Length > Limits . MaxUriLength ) throw Errors . InvalidUrl ( url ) ;
2020-02-12 14:16:19 +00:00
await AvatarUtils . VerifyAvatarOrThrow ( url ) ;
2020-02-01 12:03:02 +00:00
target . AvatarUrl = url ;
await _data . SaveMember ( target ) ;
2020-04-24 19:50:28 +00:00
var embed = new DiscordEmbedBuilder ( ) . WithImageUrl ( url ) . Build ( ) ;
2020-02-01 12:03:02 +00:00
await ctx . Reply ( $"{Emojis.Success} Member avatar changed." , embed : embed ) ;
}
2020-04-24 19:50:28 +00:00
else if ( ctx . Message . Attachments . FirstOrDefault ( ) is DiscordAttachment attachment )
2020-02-01 12:03:02 +00:00
{
2020-02-12 14:16:19 +00:00
await AvatarUtils . VerifyAvatarOrThrow ( attachment . Url ) ;
2020-02-01 12:03:02 +00:00
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
}
2020-02-12 16:42:12 +00:00
public async Task ServerAvatar ( Context ctx , PKMember target )
{
ctx . CheckGuildContext ( ) ;
var guildData = await _data . GetMemberGuildSettings ( target , ctx . Guild . Id ) ;
2020-03-04 17:13:36 +00:00
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 ;
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." ) ;
}
2020-02-12 16:42:12 +00:00
if ( ctx . RemainderOrNull ( ) = = null & & ctx . Message . Attachments . Count = = 0 )
{
if ( ( guildData . AvatarUrl ? . Trim ( ) ? ? "" ) . Length > 0 )
{
2020-04-24 19:50:28 +00:00
var eb = new DiscordEmbedBuilder ( )
2020-02-12 16:42:12 +00:00
. 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 ;
}
2020-04-24 19:50:28 +00:00
var user = await ctx . MatchUser ( ) ;
2020-02-12 16:42:12 +00:00
if ( ctx . System = = null ) throw Errors . NoSystemError ;
if ( target . System ! = ctx . System . Id ) throw Errors . NotOwnMemberError ;
2020-04-24 19:50:28 +00:00
if ( user ! = null )
2020-02-12 16:42:12 +00:00
{
2020-05-01 15:38:16 +00:00
if ( user . AvatarHash = = null ) throw Errors . UserHasNoAvatar ;
2020-02-12 16:42:12 +00:00
guildData . AvatarUrl = user . GetAvatarUrl ( ImageFormat . Png , size : 256 ) ;
await _data . SetMemberGuildSettings ( target , ctx . Guild . Id , guildData ) ;
2020-04-24 19:50:28 +00:00
var embed = new DiscordEmbedBuilder ( ) . WithImageUrl ( guildData . AvatarUrl ) . Build ( ) ;
2020-02-12 16:42:12 +00:00
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 )
{
2020-05-05 14:04:17 +00:00
if ( url . Length > Limits . MaxUriLength ) throw Errors . InvalidUrl ( url ) ;
2020-02-12 16:42:12 +00:00
await AvatarUtils . VerifyAvatarOrThrow ( url ) ;
guildData . AvatarUrl = url ;
await _data . SetMemberGuildSettings ( target , ctx . Guild . Id , guildData ) ;
2020-04-24 19:50:28 +00:00
var embed = new DiscordEmbedBuilder ( ) . WithImageUrl ( url ) . Build ( ) ;
2020-02-12 16:42:12 +00:00
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 ) ;
}
2020-04-24 19:50:28 +00:00
else if ( ctx . Message . Attachments . FirstOrDefault ( ) is DiscordAttachment attachment )
2020-02-12 16:42:12 +00:00
{
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
}
2020-02-01 12:03:02 +00:00
}
}