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.7 kB

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