What is this library? It adds convenience to the Remora library by offering abstraction to some particularly cumbersome methods.
This is done by adding various 'builders' for use, which introduce fluent APIs to manipulate them. Examples are listed below, but the main goal of this library is to either flatten certain method calls, and clean up some otherwise unweildy code.
var builder =
new MessageBuilder()
.WithContent($"Hello there, <@{user.ID}>!")
.AddAttachment(new FileData("coolAttachment.jpeg", myAttachmentStream))
.WithAllowedMentions(new AllowedMentions(new [] { MentionType.User }, default, new[] { user.ID });
When creating the message, the same method is used, however the builder is passed to the API instead of the data.
var result = await _channelAPI.CreateMessageAsync(channelID, builder, ct: this.CancellationToken);
There's an optional parameter that allows for validating the data within the builder before making an API call. This can be useful especially when dealing with user-provided inputs.
The main reason this library was created was because of Discord's horrendous implementation of interactions. They're complicated to work with when essentially dealing with the raw API, so builders are especially handy in this situation.
To create an interaction response (e.g. a message):
var builder =
new InteractionBuilder()
.AsEphemeral()
.WithType(InteractionCallbackType.ChannelMessageWithSource)
.WithContent("This is an example of the fluent API! Pretty cool.")
.AddAttachment(myImageStream, "funny.png")
.AddEmbed(someEmbed);
var result = await _interactionAPI.CreateResponseAsync(interactionID, interactionToken, builder, validate: true, ct: ct);
Compared to:
var result = await _interactionAPI.CreateInteractionResponseAsync
(
interactionID,
interactionToken,
new InteractionResponse
(
InteractionCallbackType.ChannelMessageWithSource,
new(OneOf<IInteractionMessageCallbackData, IInteractionAutocompleteCallbackData, IInteractionModalCallbackData>>.FromT0
(
new InteractionMessageCallbackData
(
Content: "This is not a fluent API! Very uncool.",
Embeds: new[] { someEmbed },
Flags: MessageFlags.Ephemeral
),
attachments: new[] { OneOf<FileData, IPartialAttachment>.FromT0(new("funny.png", myImageStream))
)
);
The latter is rather obtuse in every manner, and doesn't even do anything complex! I'm not here to slander Remora; it's a good library, and these decisions aren't entirely of Jax's vindication. Discord's API is mostly what drives these insane looking methods. Here's Discord's Documentation
There's also other builders like WebhookBuilder
and ModalBuilder
. The latter comes with TextInputBuilder
for convenience's sake.
This library is available to download on NuGet.