Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. namespace Larpl.Extensions
  2. {
  3. public static class DiscordExtensions
  4. {
  5. static void ValidateEmbed(dynamic d, out bool has_fields, out bool has_timestamp, out bool has_color, out bool has_description)
  6. {
  7. try { has_fields = !(d.fields is null); }
  8. catch { has_fields = false; }
  9.  
  10. try { has_timestamp = !(d.timestamp is null); }
  11. catch { has_timestamp = false; }
  12.  
  13. try { has_color = !(d.color is null); }
  14. catch { has_color = false; }
  15.  
  16. try { has_description = !(d.description is null); }
  17. catch { has_description = false; }
  18. }
  19.  
  20. public static DiscordEmbedBuilder CreateNewDynamicEmbed(dynamic d)
  21. {
  22. var temp = new DiscordEmbedBuilder();
  23.  
  24. if (d is null)
  25. return temp;
  26.  
  27. ValidateEmbed(d, out bool has_fields,
  28. out bool has_timestamp,
  29. out bool has_color,
  30. out bool has_description);
  31.  
  32. if (has_fields)
  33. {
  34. foreach(var field in d.fields)
  35. {
  36. string name = default;
  37. string value = default;
  38. bool is_inline = default;
  39.  
  40. if (Dynamic_IsValidEmbedField(field, ref name, ref value, ref is_inline))
  41. {
  42. temp.AddField($"{ZERO_WIDTH_SPACE}{name}", $"{ZERO_WIDTH_SPACE}{value}", is_inline);
  43. }
  44. }
  45. }
  46.  
  47. if(has_color)
  48. {
  49. if (d.color is string hex)
  50. temp.WithColor(new DiscordColor(hex));
  51. else if(d.color is int value)
  52. temp.WithColor(new DiscordColor(value));
  53. else if(d.color is int[] ivec && ivec.Length >= 3)
  54. {
  55. var r = ivec[0];
  56. var g = ivec[1];
  57. var b = ivec[2];
  58. temp.WithColor(new DiscordColor(r, g, b));
  59. }
  60. else if(d.color is float[] fvec && fvec.Length >= 3)
  61. {
  62. var r = fvec[0];
  63. var g = fvec[1];
  64. var b = fvec[2];
  65. temp.WithColor(new DiscordColor(r, g, b));
  66. }
  67. }
  68.  
  69. if(has_timestamp)
  70. {
  71. if (d.timestamp is DateTime dt)
  72. temp.WithTimestamp(dt);
  73. else if(d.timestamp is DateTimeOffset dto)
  74. temp.WithTimestamp(dto);
  75. else if (d.timestamp is ulong unix)
  76. temp.WithTimestamp(unix);
  77. }
  78.  
  79. if(has_description)
  80. {
  81. temp.WithDescription($"{ZERO_WIDTH_SPACE}{Convert.ToString(d.description)}");
  82. }
  83.  
  84. return temp;
  85. }
  86.  
  87. static bool Dynamic_IsValidEmbedField(dynamic field, ref string name, ref string value, ref bool inline)
  88. {
  89. var state = !(field is null) && !(field.name is null) && !(field.value is null);
  90.  
  91. if (state)
  92. {
  93. name = Convert.ToString(field.name);
  94. value = Convert.ToString(field.value);
  95.  
  96. try
  97. {
  98. inline = field.inline;
  99. }
  100. catch
  101. {
  102. inline = false;
  103. }
  104. }
  105.  
  106. return state;
  107. }
  108.  
  109. public static async Task<DiscordMessage> DynamicRespondAsync(this CommandContext ctx, string content = default, dynamic embed = default)
  110. {
  111. return await ctx.RespondAsync(content, false, CreateNewDynamicEmbed(embed));
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement