Last active
July 14, 2019 15:41
-
-
Save nathan130200/ba1d3b69a9c4ee74ceb9c2349d1ebb24 to your computer and use it in GitHub Desktop.
Respond command context with dynamic embed creation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
using DSharpPlus.CommandsNext; | |
using DSharpPlus.Entities; | |
namespace DSharpPlus | |
{ | |
public static class DSharpPlusExtensions | |
{ | |
public static Task<DiscordMessage> RespondEmbedAsync(this CommandContext ctx, string content, object embed) | |
{ | |
if (embed == null) | |
throw new ArgumentNullException(nameof(embed)); | |
var result = new DiscordEmbedBuilder(); | |
foreach (var xproperty in embed.GetType().GetTypeInfo().DeclaredProperties) | |
{ | |
switch (xproperty.Name.ToLowerInvariant()) | |
{ | |
case "author": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is string s) | |
result.WithAuthor(s); | |
else if (xvalue is object o) | |
{ | |
string name = null; | |
string url = null; | |
string icon_url = null; | |
foreach (var xauthor in o.GetType().GetTypeInfo().DeclaredProperties) | |
{ | |
switch (xauthor.Name.ToLowerInvariant()) | |
{ | |
case "name": name = xauthor.GetValue(o).ToString(); break; | |
case "url": url = xauthor.GetValue(o).ToString(); break; | |
case "icon": | |
case "icon_url": icon_url = xauthor.GetValue(o).ToString(); break; | |
} | |
} | |
result.WithAuthor(name, url, icon_url); | |
} | |
} | |
break; | |
case "description": | |
result.WithDescription(xproperty.GetValue(embed).ToString()); | |
break; | |
case "title": | |
result.WithTitle(xproperty.GetValue(embed).ToString()); | |
break; | |
case "url": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is Uri u) | |
result.WithUrl(u); | |
else | |
result.WithUrl(xvalue.ToString()); | |
} | |
break; | |
case "color": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is DiscordColor c) | |
result.WithColor(c); | |
else if (xvalue is int iValue) | |
result.WithColor(new DiscordColor(iValue)); | |
else if (xvalue is string hex) | |
result.WithColor(new DiscordColor(hex)); | |
} | |
break; | |
case "timestamp": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is DateTime dt) | |
result.WithTimestamp(dt); | |
else if (xvalue is DateTimeOffset dto) | |
result.WithTimestamp(dto); | |
else if (xvalue is ulong snowflake) | |
result.WithTimestamp(snowflake); | |
} | |
break; | |
case "image": | |
case "image_url": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is Uri u) | |
result.WithImageUrl(u); | |
else | |
result.WithImageUrl(xvalue.ToString()); | |
} | |
break; | |
case "thumbnail_url": | |
case "thumbnail": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is Uri u) | |
result.WithThumbnailUrl(u); | |
else | |
result.WithThumbnailUrl(xvalue.ToString()); | |
} | |
break; | |
case "footer": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if (xvalue is object xfooter) | |
{ | |
string text = null; | |
string icon_url = null; | |
foreach (var footer in xfooter.GetType().GetTypeInfo().DeclaredProperties) | |
{ | |
switch (footer.Name.ToLowerInvariant()) | |
{ | |
case "text": | |
text = footer.GetValue(xfooter).ToString(); | |
break; | |
case "icon": | |
case "icon_url": | |
icon_url = footer.GetValue(xfooter).ToString(); | |
break; | |
} | |
} | |
} | |
else | |
result.WithFooter(xvalue.ToString()); | |
} | |
break; | |
case "fields": | |
{ | |
var xvalue = xproperty.GetValue(embed); | |
if(xvalue is object[] fields) | |
{ | |
foreach(var field in fields) | |
{ | |
string name = null; | |
string value = null; | |
bool inline = false; | |
foreach(var xfield in field.GetType().GetTypeInfo().DeclaredProperties) | |
{ | |
switch (xfield.Name.ToLowerInvariant()) | |
{ | |
case "name": | |
name = xfield.GetValue(field).ToString(); | |
break; | |
case "value": | |
value = xfield.GetValue(field).ToString(); | |
break; | |
case "inline": | |
{ | |
var xinline = xfield.GetValue(field); | |
if (xinline is int iValue) | |
inline = iValue > 0 ? true : false; | |
else if (xinline is bool bValue) | |
inline = bValue; | |
} | |
break; | |
} | |
} | |
result.AddField("\u200b" + name, "\u200b" + value, inline); | |
} | |
} | |
} | |
break; | |
} | |
} | |
return ctx.RespondAsync(content, false, result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment