diff --git a/packages/builders/src/mixins/Refineable.ts b/packages/builders/src/mixins/Refineable.ts new file mode 100644 index 000000000..192a0c97a --- /dev/null +++ b/packages/builders/src/mixins/Refineable.ts @@ -0,0 +1,26 @@ +/** + * Mixin used to provide {@link Refineable.refine} + */ +export class Refineable { + /** + * Refines this builder by applying the provided function to itself. + * Useful for conditionally modifying the builder without breaking the method chain, while also isolating + * the scope of temporary variables. + * + * @example + * ```ts + * const builder = new EmbedBuilder() + * .setTitle('Hello World') + * .setDescription('This is a description') + * .refine((b) => { + * if (externalVariable) { + * b.setColor('Red'); + * } + * }); + * ``` + */ + public refine(fn: (builder: this) => void): this { + fn(this); + return this; + } +}