An unofficial library for interacting with Discord Webhook aimed at use in the Unity environment
- Send messages (include embeds)
- Send files
- RateLimit support
- Allowed mentions support
- RestProviders (one on
HttpClient
, the other onWebRequest
) - Delete webhook
- Modify webhook
- Get information about webhook
- Manipulating operations at the action level (action queue, callbacks)
Library allows you to fully interact with discord webhook including unity projects that were built with Mono on .NET Framework 4.7.1 (.NET 4.x profile) compatibility.
Note: You should always choose the default provider that comes with the library, a mono-compatible provider has a memory leak, read more on StackOverflow
.
Only if you see such an exception.
FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
This usually happens on unity projects that are compatible with .NET 4.x, it does not include System.Net.Http
, only in this case you should use MonoProvider.
MonoProvider is delivered as a separate package with NuGet, you will need to download it and call DSharp4Webhook.Rest.Mono.MonoProvder.SetupAsDefault()
before you create a webhook.
// creating a webhook provider
var provider = new WebhookProvider("your.id");
// creating a webhook
var webhook = provider.CreateWebhook("webhook url");
// sending a message containing 'my content' and passing the callback,
// callback is called after the action has been performed
webhook.SendMessage("my content").Queue(() => Console.WriteLine("my message has been sent!"));
Files are sent via MessageBuilder
, you just need to add its name and content to the file dictionary.
Example:
// reading the file data in the current directory
var fileContent = File.ReadAllBytes("myFile.txt");
// getting a new message builder
var messageBuilder = ConstructorProvider.GetMessageBuilder();
// adding a file to the dictionary of files such as the file name and its content
// yeah, you can change the file name
messageBuilder.Files.Add("myFile.txt", fileContent);
// building a message
// note that you can't change the message content after building
var message = messageBuilder.Build();
// send a message
webhook.SendMessage(message).Queue();
Each constructor has a Reset()
method, this method resets the constructor to its default preset (on the message builder also resets allowed mentions, but not the permanent one that was built with it), I recommend using this method on every constructor instead of creating.
When creating a constructor, you can select allowed mentions, which are saved for the entire lifecycle of the constructor, its processing: when you reset the constructor to the default preset, it resets the allowed mentions too, and if they were not set during the new build, then a permanent allowed mention is used.
Usually these are the default, which means None
- no one will be mentioned.
By default, all mentions are forbidden, and the webhook provider has the AllowedMention
property, you can assign it and it will be used to create new webhooks, the property AllowedMention
on webhooks is used to send messages that were not configured via message builder.
Newtonsoft.Json