|
| 1 | +using DSharpPlus.Entities; |
| 2 | + |
| 3 | +namespace EmbedBot.Extensions; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Extension methods for <see cref="DiscordEmbedBuilder" />. |
| 7 | +/// </summary> |
| 8 | +internal static class DiscordEmbedBuilderExtensions |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Adds a field of any value type to the embed. |
| 12 | + /// </summary> |
| 13 | + /// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param> |
| 14 | + /// <param name="name">The name of the embed field.</param> |
| 15 | + /// <param name="value">The value of the embed field.</param> |
| 16 | + /// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param> |
| 17 | + /// <typeparam name="T">The type of <paramref name="value" />.</typeparam> |
| 18 | + /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns> |
| 19 | + /// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception> |
| 20 | + public static DiscordEmbedBuilder AddField<T>( |
| 21 | + this DiscordEmbedBuilder builder, |
| 22 | + string name, |
| 23 | + T? value, |
| 24 | + bool inline = false) |
| 25 | + { |
| 26 | + if (builder is null) |
| 27 | + { |
| 28 | + throw new ArgumentNullException(nameof(builder)); |
| 29 | + } |
| 30 | + |
| 31 | + return builder.AddField(name, value?.ToString(), inline); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Conditionally adds a field to the embed. |
| 36 | + /// </summary> |
| 37 | + /// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param> |
| 38 | + /// <param name="condition">The condition whose value is used to determine whether the field will be added.</param> |
| 39 | + /// <param name="name">The name of the embed field.</param> |
| 40 | + /// <param name="value">The value of the embed field.</param> |
| 41 | + /// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param> |
| 42 | + /// <typeparam name="T">The type of <paramref name="value" />.</typeparam> |
| 43 | + /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns> |
| 44 | + /// <exception cref="ArgumentNullException"><paramref name="builder" /> is <see langword="null" />.</exception> |
| 45 | + public static DiscordEmbedBuilder AddFieldIf<T>( |
| 46 | + this DiscordEmbedBuilder builder, |
| 47 | + bool condition, |
| 48 | + string name, |
| 49 | + T? value, |
| 50 | + bool inline = false) |
| 51 | + { |
| 52 | + if (builder is null) |
| 53 | + { |
| 54 | + throw new ArgumentNullException(nameof(builder)); |
| 55 | + } |
| 56 | + |
| 57 | + if (condition) |
| 58 | + { |
| 59 | + builder.AddField(name, value?.ToString(), inline); |
| 60 | + } |
| 61 | + |
| 62 | + return builder; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Conditionally adds a field to the embed. |
| 67 | + /// </summary> |
| 68 | + /// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param> |
| 69 | + /// <param name="predicate">The predicate whose return value is used to determine whether the field will be added.</param> |
| 70 | + /// <param name="name">The name of the embed field.</param> |
| 71 | + /// <param name="value">The value of the embed field.</param> |
| 72 | + /// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param> |
| 73 | + /// <typeparam name="T">The type of <paramref name="value" />.</typeparam> |
| 74 | + /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns> |
| 75 | + /// <exception cref="ArgumentNullException"> |
| 76 | + /// <para><paramref name="builder" /> is <see langword="null" />.</para> |
| 77 | + /// -or- |
| 78 | + /// <para><paramref name="predicate" /> is <see langword="null" />.</para> |
| 79 | + /// </exception> |
| 80 | + public static DiscordEmbedBuilder AddFieldIf<T>( |
| 81 | + this DiscordEmbedBuilder builder, |
| 82 | + Func<bool> predicate, |
| 83 | + string name, |
| 84 | + T? value, |
| 85 | + bool inline = false) |
| 86 | + { |
| 87 | + if (builder is null) |
| 88 | + { |
| 89 | + throw new ArgumentNullException(nameof(builder)); |
| 90 | + } |
| 91 | + |
| 92 | + if (predicate is null) |
| 93 | + { |
| 94 | + throw new ArgumentNullException(nameof(predicate)); |
| 95 | + } |
| 96 | + |
| 97 | + if (predicate.Invoke()) |
| 98 | + { |
| 99 | + builder.AddField(name, value?.ToString(), inline); |
| 100 | + } |
| 101 | + |
| 102 | + return builder; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Conditionally adds a field to the embed. |
| 107 | + /// </summary> |
| 108 | + /// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param> |
| 109 | + /// <param name="predicate">The predicate whose return value is used to determine whether the field will be added.</param> |
| 110 | + /// <param name="name">The name of the embed field.</param> |
| 111 | + /// <param name="valueFactory">The delegate whose return value will be used as the value of the embed field.</param> |
| 112 | + /// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param> |
| 113 | + /// <typeparam name="T">The return type of <paramref name="valueFactory" />.</typeparam> |
| 114 | + /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns> |
| 115 | + /// <exception cref="ArgumentNullException"> |
| 116 | + /// <para><paramref name="builder" /> is <see langword="null" />.</para> |
| 117 | + /// -or- |
| 118 | + /// <para><paramref name="predicate" /> is <see langword="null" />.</para> |
| 119 | + /// -or- |
| 120 | + /// <para><paramref name="valueFactory" /> is <see langword="null" />.</para> |
| 121 | + /// </exception> |
| 122 | + public static DiscordEmbedBuilder AddFieldIf<T>( |
| 123 | + this DiscordEmbedBuilder builder, |
| 124 | + Func<bool> predicate, |
| 125 | + string name, |
| 126 | + Func<T?> valueFactory, |
| 127 | + bool inline = false) |
| 128 | + { |
| 129 | + if (builder is null) |
| 130 | + { |
| 131 | + throw new ArgumentNullException(nameof(builder)); |
| 132 | + } |
| 133 | + |
| 134 | + if (predicate is null) |
| 135 | + { |
| 136 | + throw new ArgumentNullException(nameof(predicate)); |
| 137 | + } |
| 138 | + |
| 139 | + if (valueFactory is null) |
| 140 | + { |
| 141 | + throw new ArgumentNullException(nameof(valueFactory)); |
| 142 | + } |
| 143 | + |
| 144 | + if (predicate.Invoke()) |
| 145 | + { |
| 146 | + builder.AddField(name, valueFactory.Invoke()?.ToString(), inline); |
| 147 | + } |
| 148 | + |
| 149 | + return builder; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Conditionally adds a field to the embed. |
| 154 | + /// </summary> |
| 155 | + /// <param name="builder">The <see cref="DiscordEmbedBuilder" /> to modify.</param> |
| 156 | + /// <param name="condition">The condition whose value is used to determine whether the field will be added.</param> |
| 157 | + /// <param name="name">The name of the embed field.</param> |
| 158 | + /// <param name="valueFactory">The delegate whose return value will be used as the value of the embed field.</param> |
| 159 | + /// <param name="inline"><see langword="true" /> to display this field inline; otherwise, <see langword="false" />.</param> |
| 160 | + /// <typeparam name="T">The return type of <paramref name="valueFactory" />.</typeparam> |
| 161 | + /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />; that is, <paramref name="builder" />.</returns> |
| 162 | + /// <exception cref="ArgumentNullException"> |
| 163 | + /// <para><paramref name="builder" /> is <see langword="null" />.</para> |
| 164 | + /// -or- |
| 165 | + /// <para><paramref name="valueFactory" /> is <see langword="null" />.</para> |
| 166 | + /// </exception> |
| 167 | + public static DiscordEmbedBuilder AddFieldIf<T>( |
| 168 | + this DiscordEmbedBuilder builder, |
| 169 | + bool condition, |
| 170 | + string name, |
| 171 | + Func<T?> valueFactory, |
| 172 | + bool inline = false) |
| 173 | + { |
| 174 | + if (builder is null) |
| 175 | + { |
| 176 | + throw new ArgumentNullException(nameof(builder)); |
| 177 | + } |
| 178 | + |
| 179 | + if (valueFactory is null) |
| 180 | + { |
| 181 | + throw new ArgumentNullException(nameof(valueFactory)); |
| 182 | + } |
| 183 | + |
| 184 | + if (condition) |
| 185 | + { |
| 186 | + builder.AddField(name, valueFactory.Invoke()?.ToString(), inline); |
| 187 | + } |
| 188 | + |
| 189 | + return builder; |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary> |
| 193 | + /// Sets the embed's author. |
| 194 | + /// </summary> |
| 195 | + /// <param name="builder">The embed builder to modify.</param> |
| 196 | + /// <param name="user">The author.</param> |
| 197 | + /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />.</returns> |
| 198 | + public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder builder, DiscordUser user) |
| 199 | + { |
| 200 | + if (builder is null) |
| 201 | + { |
| 202 | + throw new ArgumentNullException(nameof(builder)); |
| 203 | + } |
| 204 | + |
| 205 | + if (user is null) |
| 206 | + { |
| 207 | + throw new ArgumentNullException(nameof(user)); |
| 208 | + } |
| 209 | + |
| 210 | + return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl); |
| 211 | + } |
| 212 | +} |
0 commit comments