Used for sending webhook messages to Discord using a Discord Webhook URL - Also completely dependency free!
var webhookObject = new WebhookObject();
webhookObject.AddEmbed(builder =>
{
builder.WithTitle("Discord-Webhook lib")
.WithDescription("Building embed with 'AddEmbed(Action<EmbedBuilder> embedBuilderFunction)'")
.WithUrl("https://github.com/ToshiroZ/Discord-Webhook")
.WithThumbnail("https://www.thegatewaypundit.com/wp-content/uploads/trump-mugshot-1.jpg")
.WithColor(Colors.Magenta)
.WithImage("https://i.imgur.com/ZGPxFN2.jpg")
.AddField("New Field", "This is a new field")
.WithFooter("DiscordUser",
"https://www.thegatewaypundit.com/wp-content/uploads/trump-mugshot-1.jpg");
});
await new Webhook("my-webhook-url").SendAsync(webhookObject);
await new Webhook("my-webhook-url", "username", "avatar url").SendAsync(webhookObject);
⚠️ Ensure that your webhook URL is targeting a forum channel, any other channel type will throw an exception!
Since v1.0.9
, the library now supports threads, you can set your messages to be targeted to a thread.
await new Webhook("my-webhook-url", "username", "avatar url")
.SendAsync(webhookObject);
Since v1.0.9
, you can send webhook messages easier by using a static method in the Webhook
class called SendAsync
This method is used to simplify the above call even further, although this is realistically only superior when you're only sending a single webhook message and not multiple calls.
await Webhook.SendAsync("my-webhook-url", webhookObject);
... and you can add an avatar URL, username, etc in the same way as before:
await Webhook.SendAsync("my-webhook-url", webhookObject, "username", "avatar url");
await Webhook.SendAsync("my-webhook-url", webhookObject, "username", "avatar url", "thread name"); // For threads
Since v1.0.9
, the library now handles exceptions from Discord's API to allow for easier debugging and handling on an application-level. All exceptions thrown by Discord are handled with a DiscordWebhookException
in the Discord.Webhook.Exceptions
namespace. You can handle exceptions like this:
try
{
var webhookObject = new WebhookObject();
webhookObject.AddEmbed(builder =>
{
builder.WithTitle("Discord-Webhook lib")
.WithDescription("Building embed with 'AddEmbed(Action<EmbedBuilder> embedBuilderFunction)'")
.WithUrl("https://github.com/ToshiroZ/Discord-Webhook")
.WithThumbnail("https://www.thegatewaypundit.com/wp-content/uploads/trump-mugshot-1.jpg")
.WithColor(Colors.Magenta)
.WithImage("https://i.imgur.com/ZGPxFN2.jpg")
.AddField("New Field", "This is a new field")
.WithFooter("DiscordUser",
"https://www.thegatewaypundit.com/wp-content/uploads/trump-mugshot-1.jpg");
});
await new Webhook("my-webhook-url").SendAsync(webhookObject);
}
catch (DiscordWebhookException ex)
{
Console.WriteLine($"Exception from Discord: {ex.Message}");
}