2017-11-16 14:56:39 +00:00
|
|
|
using System;
|
2017-07-17 19:42:36 +00:00
|
|
|
using Discord;
|
2017-07-08 12:20:06 +00:00
|
|
|
using NadekoBot.Extensions;
|
2017-02-06 19:09:47 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using NLog;
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
namespace NadekoBot.Common
|
2017-02-06 19:09:47 +00:00
|
|
|
{
|
|
|
|
public class CREmbed
|
|
|
|
{
|
|
|
|
private static readonly Logger _log;
|
|
|
|
public string PlainText { get; set; }
|
|
|
|
public string Title { get; set; }
|
|
|
|
public string Description { get; set; }
|
2017-11-17 03:04:04 +00:00
|
|
|
public string Url { get; set; }
|
2017-02-06 19:09:47 +00:00
|
|
|
public CREmbedFooter Footer { get; set; }
|
|
|
|
public string Thumbnail { get; set; }
|
|
|
|
public string Image { get; set; }
|
|
|
|
public CREmbedField[] Fields { get; set; }
|
|
|
|
public uint Color { get; set; } = 7458112;
|
|
|
|
|
|
|
|
static CREmbed()
|
|
|
|
{
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsValid =>
|
|
|
|
!string.IsNullOrWhiteSpace(Title) ||
|
|
|
|
!string.IsNullOrWhiteSpace(Description) ||
|
2017-11-17 03:04:04 +00:00
|
|
|
!string.IsNullOrWhiteSpace(Url) ||
|
2017-02-06 19:09:47 +00:00
|
|
|
!string.IsNullOrWhiteSpace(Thumbnail) ||
|
|
|
|
!string.IsNullOrWhiteSpace(Image) ||
|
|
|
|
(Footer != null && (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl))) ||
|
|
|
|
(Fields != null && Fields.Length > 0);
|
|
|
|
|
|
|
|
public EmbedBuilder ToEmbed()
|
|
|
|
{
|
2017-07-07 13:45:21 +00:00
|
|
|
var embed = new EmbedBuilder();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Title))
|
|
|
|
embed.WithTitle(Title);
|
|
|
|
if (!string.IsNullOrWhiteSpace(Description))
|
|
|
|
embed.WithDescription(Description);
|
2017-11-17 03:04:04 +00:00
|
|
|
if (Url != null && Uri.IsWellFormedUriString(Url, UriKind.Absolute))
|
|
|
|
embed.WithUrl(Url);
|
2017-07-07 13:45:21 +00:00
|
|
|
embed.WithColor(new Discord.Color(Color));
|
2017-02-06 19:09:47 +00:00
|
|
|
if (Footer != null)
|
2017-07-07 22:05:11 +00:00
|
|
|
embed.WithFooter(efb =>
|
|
|
|
{
|
|
|
|
efb.WithText(Footer.Text);
|
|
|
|
if (Uri.IsWellFormedUriString(Footer.IconUrl, UriKind.Absolute))
|
|
|
|
efb.WithIconUrl(Footer.IconUrl);
|
|
|
|
});
|
2017-07-07 13:45:21 +00:00
|
|
|
|
|
|
|
if (Thumbnail != null && Uri.IsWellFormedUriString(Thumbnail, UriKind.Absolute))
|
|
|
|
embed.WithThumbnailUrl(Thumbnail);
|
|
|
|
if(Image != null && Uri.IsWellFormedUriString(Image, UriKind.Absolute))
|
|
|
|
embed.WithImageUrl(Image);
|
2017-02-06 19:09:47 +00:00
|
|
|
|
|
|
|
if (Fields != null)
|
|
|
|
foreach (var f in Fields)
|
|
|
|
{
|
2017-07-07 13:45:21 +00:00
|
|
|
if(!string.IsNullOrWhiteSpace(f.Name) && !string.IsNullOrWhiteSpace(f.Value))
|
|
|
|
embed.AddField(efb => efb.WithName(f.Name).WithValue(f.Value).WithIsInline(f.Inline));
|
2017-02-06 19:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return embed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool TryParse(string input, out CREmbed embed)
|
|
|
|
{
|
|
|
|
embed = null;
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var crembed = JsonConvert.DeserializeObject<CREmbed>(input);
|
2017-07-08 12:20:06 +00:00
|
|
|
|
|
|
|
if(crembed.Fields != null && crembed.Fields.Length > 0)
|
|
|
|
foreach (var f in crembed.Fields)
|
|
|
|
{
|
|
|
|
f.Name = f.Name.TrimTo(256);
|
|
|
|
f.Value = f.Value.TrimTo(1024);
|
|
|
|
}
|
2017-02-06 19:09:47 +00:00
|
|
|
if (!crembed.IsValid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
embed = crembed;
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-10 10:16:30 +00:00
|
|
|
catch
|
2017-02-06 19:09:47 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CREmbedField
|
|
|
|
{
|
|
|
|
public string Name { get; set; }
|
|
|
|
public string Value { get; set; }
|
|
|
|
public bool Inline { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CREmbedFooter {
|
|
|
|
public string Text { get; set; }
|
|
|
|
public string IconUrl { get; set; }
|
|
|
|
}
|
|
|
|
}
|