Unity-DiscordWebhook is a library that allows you to easily send messages and files to Discord channels using webhooks. It provides a simple API for sending text messages, capturing and attaching screenshots, and even compressing and attaching log files for bug reports.
- Sending messages and create posts/threads to text channels and forums.
- Builder pattern for creating a message.
- Awaitable API with UniTask
- Unity Coroutine and EditorCoroutine API. (IEnumerator and callback)
- A few Discord Bot API to query the server and channel information.
- See DiscordBotApi.cs.
- Helper methods for creating a bug report.
- (Optional) Cysharp/UniTask
- We recommend using UniTask for asynchronous programming. However, you can use the library without it, we provide Unity Coroutine API as well.
- If your UniTask is not installed from Unity Package Manager, add the scripting define symbol
DISCORD_WEBHOOK_UNITASK_SUPPORT
to Project Settings.
Install via Unity Package Manager.
https://github.com/qwe321qwe321qwe321/Unity-DiscordWebhook.git?path=src/DiscordWebhook/Assets/DiscordWebhook
WebhookResponseResult result = await WebhookBuilder.CreateTextChannel(textChannelWebhookUrl)
.SetUsername("MyBot") // username is optional.
.SetContent("Hello WORLD") // content is required.
.ExecuteAsync();
// handle the result.
WebhookBuilder.CreateTextChannel(textChannelWebhookUrl)
.SetUsername("MyBot") // username is optional.
.SetContent("Hello WORLD") // content is required.
.ExecuteCoroutine(this, (result) => {
// handle the result with callback.
}));
WebhookBuilder.CreateTextChannel(textChannelWebhookUrl)
.SetUsername("MyBot") // username is optional.
.SetContent("Hello WORLD") // content is required.
.ExecuteEditorCoroutine((result) => { // No need a MonoBehaviour to run.
// handle the result with callback.
}));
private async UniTaskVoid CreateBugReportTheadToForum() {
string markdownContent = "# Bug report from user\nHere is the description.\n" + SystemInfoHelper.GetSystemInfoInMarkdownList();
WebhookResponseResult result = await WebhookBuilder.CreateForum(forumWebhookUrl)
.SetThreadName("TITLE")
.SetContent(markdownContent)
.AddTags(forumTagIds) // Add tags to the thread. (You have to get the tag IDs by DiscordBotApi upfront.)
.SetCaptureScreenshot(true) // capture screenshot and attach it.
.AddFile(Application.consoleLogPath) // add log file.
.AddFile("systemInfo.txt", Encoding.UTF8.GetBytes(SystemInfoHelper.GetSystemInfoInMarkdownList())) // add system info.
.SetCompressAllFilesToZip(true, "LogFiles") // compress the all files to a zip named "LogFiles.zip"
.ExecuteAsync();
if (result.isSuccess) {
Debug.Log($"Success! {result.response}");
string url = result.GetMessageURL(serverId);
Debug.Log(url);
if (url != null) {
// Directly open the message in the browser.
Application.OpenURL(url);
}
}
}
private void CreateBugReportTheadToForum() {
string markdownContent = "# Bug report from user\nHere is the description.\n" + SystemInfoHelper.GetSystemInfoInMarkdownList();
WebhookBuilder.CreateForum(forumWebhookUrl)
.SetThreadName("TITLE")
.SetContent(markdownContent)
.AddTags(forumTagIds) // Add tags to the thread. (You have to get the tag IDs by DiscordBotApi upfront.)
.SetCaptureScreenshot(true) // capture screenshot and attach it.
.AddFile(Application.consoleLogPath) // add log file.
.AddFile("systemInfo.txt", Encoding.UTF8.GetBytes(SystemInfoHelper.GetSystemInfoInMarkdownList())) // add system info.
.SetCompressAllFilesToZip(true, "LogFiles") // compress the all files to a zip named "LogFiles.zip"
.ExecuteCoroutine(this, OnComplete);
void OnComplete(WebhookResponseResult result) {
if (result.isSuccess) {
Debug.Log($"Success! {result.response}");
string url = result.GetMessageURL(serverId);
Debug.Log(url);
if (url != null) {
Application.OpenURL(url);
}
}
}
}
Turn on the Developer Mode and right-click on them.
(See More: https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID)
How to find the Tag ID from forums?
CAVEAT: You should never directly expose your webhook token in a public product, as this poses a risk of allowing anyone to send any message through the webhook. The use cases here are either a limited scope of confidential users or implementing thier own backends to prevent from exposing the token to clients.
If you want your use case added to or removed from this list just open an issue.
Bug reporter for the in-house level editor.
In-game UI | Discord |
---|---|
In-game bug reporter for beta test.
In-game UI | Discord |
---|---|
24BBED0A00001.mp4 |
In-game bug reporter with their own backend.
Patch notes informer in Unity Editor.
I actually wrote an article that introduces the implementation of bug reports using Discord webhooks, covering its advantages and caveats, but it's in Chinese..