You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmbedBuilderExtensions.cs 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. namespace Discord
  3. {
  4. public static class EmbedBuilderExtensions
  5. {
  6. public static EmbedBuilder WithColor(this EmbedBuilder builder, uint rawValue) =>
  7. builder.WithColor(new Color(rawValue));
  8. public static EmbedBuilder WithColor(this EmbedBuilder builder, byte r, byte g, byte b) =>
  9. builder.WithColor(new Color(r, g, b));
  10. public static EmbedBuilder WithColor(this EmbedBuilder builder, int r, int g, int b) =>
  11. builder.WithColor(new Color(r, g, b));
  12. public static EmbedBuilder WithColor(this EmbedBuilder builder, float r, float g, float b) =>
  13. builder.WithColor(new Color(r, g, b));
  14. public static EmbedBuilder WithAuthor(this EmbedBuilder builder, IUser user) =>
  15. builder.WithAuthor($"{user.Username}#{user.Discriminator}", user.GetAvatarUrl());
  16. public static EmbedBuilder WithAuthor(this EmbedBuilder builder, IGuildUser user) =>
  17. builder.WithAuthor($"{user.Nickname ?? user.Username}#{user.Discriminator}", user.GetAvatarUrl());
  18. public static EmbedBuilder ToEmbedBuilder(this IEmbed embed)
  19. {
  20. if (embed.Type != EmbedType.Rich)
  21. throw new InvalidOperationException($"Only {nameof(EmbedType.Rich)} embeds may be built.");
  22. var builder = new EmbedBuilder
  23. {
  24. Author = new EmbedAuthorBuilder
  25. {
  26. Name = embed.Author?.Name,
  27. IconUrl = embed.Author?.IconUrl,
  28. Url = embed.Author?.Url
  29. },
  30. Color = embed.Color ?? Color.Default,
  31. Description = embed.Description,
  32. Footer = new EmbedFooterBuilder
  33. {
  34. Text = embed.Footer?.Text,
  35. IconUrl = embed.Footer?.IconUrl
  36. },
  37. ImageUrl = embed.Image?.Url,
  38. ThumbnailUrl = embed.Thumbnail?.Url,
  39. Timestamp = embed.Timestamp,
  40. Title = embed.Title,
  41. Url = embed.Url
  42. };
  43. foreach (var field in embed.Fields)
  44. builder.AddField(field.Name, field.Value, field.Inline);
  45. return builder;
  46. }
  47. }
  48. }