Skip to content

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.

License

Notifications You must be signed in to change notification settings

qwe321qwe321qwe321/Unity-DiscordWebhook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Unity-DiscordWebhook

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.

Features

Table of Contents

Setup

Requirement

  • (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.

Installation

Install via Unity Package Manager.

https://github.com/qwe321qwe321qwe321/Unity-DiscordWebhook.git?path=src/DiscordWebhook/Assets/DiscordWebhook

Getting Started

Send a message to a text channel

UniTaskSample.cs

WebhookResponseResult result = await WebhookBuilder.CreateTextChannel(textChannelWebhookUrl)
	.SetUsername("MyBot") // username is optional.
	.SetContent("Hello WORLD") // content is required.
	.ExecuteAsync();
// handle the result.

CoroutineSample.cs

WebhookBuilder.CreateTextChannel(textChannelWebhookUrl)
	.SetUsername("MyBot") // username is optional.
	.SetContent("Hello WORLD") // content is required.
	.ExecuteCoroutine(this, (result) => {
    	// handle the result with callback.
	}));

EditorCoroutineSample.cs

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.
	}));

Create a post in a forum with screenshots and log files

UniTaskSample.cs

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);
		}
	}
}

CoroutineSample.cs

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);
			}
		}
	}
}

Where can I find my User/Server/Message/Tag ID?

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?

😜right-click on it.

Use Cases

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
bb-ui bb-dc

In-game bug reporter for beta test.

In-game UI Discord
24BBED0A00001.mp4
mbu-dc

In-game bug reporter with their own backend.

image

Patch notes informer in Unity Editor.

image


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..

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages