项目作者: Pandoa

项目描述 :
Documentation for the Discord Features plugin available on the Unreal Engine Marketplace.
高级语言:
项目地址: git://github.com/Pandoa/DiscordFeatures.git
创建时间: 2020-08-02T20:39:45Z
项目社区:https://github.com/Pandoa/DiscordFeatures

开源协议:

下载


The documentation has moved here.
<!—

Discord - Features

Table of content

  1. Downloading the SDK

    1.1. With the editor

    1.2. Manually
  2. Configure the SDK
  3. Modules

    3.1. Discord SDK

    3.2. Discord Core

    3.3. Discord Achievement

    3.4. Discord Activity

    3.5. Discord Application

    3.6. Discord Voice

    3.7. Discord Image

    3.8. Discord Lobby

    3.9. Discord Networking

    3.10. Discord Overlay

    3.11. Discord Relationship

    3.12. Discord Storage

    3.13. Discord Store

    3.14. Discord User

    3.15. Discord Gateway

    3.16. Discord HTTP API
  4. Getting started
  5. Examples

    5.1. Blueprint Examples

    5.2. C++ Examples
  6. Discord HTTP API

    6.1. Webhooks
  7. Discord Gateway
  8. Contact

1. Download the SDK

Before using the plugin, you have to download the Discord Game SDK. To do so you have two options:

1.1. Download the SDK from the Editor

1.1.1. Open the configuration window

Start to open the configuration window by clicking on the Discord icon and choosing Configure the Discord SDK:

1.1.2. Download the SDK

In the configuration window, click on Download the SDK:

Wait until the process is over, and it’s already done, the Discord SDK is installed and ready to be used.
| :information_source: |If the installation fails, you’ll have to download the SDK manually. Don’t worry, it’s not that difficult !|
|:—:|—-|

1.2. Download the SDK manually

First, start by downloading the last release of the Discord Game SDK from Discord’s servers: discord_game_sdk.zip.

You then need to extract it to /DiscordFeatures/ThirdParty/DiscordSdk/.

If it is installed correctly, the Discord Configuration Window won’t ask you to download the SDK.

2. Configure the SDK

Open the configuration window by clicking on the Discord icon and choosing Configure the Discord SDK:

In the window, set your application ID with the one of your application. You can create and get your application ID from Discord).
You can set your secret bot token as well if you plan to use the Discord editor tools.
| :warning: |The secret bot token is stored in an Editor-only config file called DiscordSdkEditor.ini. Think to exclude it from source control if needed. |
|:—:|:—-|

3. Modules

The Discord Features Plugin is separated in several modules. Each module contains a set of functionalities.
To use one in C++, you have to add the module to your Build.cs file:

  1. PrivateDependencyModuleNames.Add("DiscordCore"); // Add one module

or to add more than one:

  1. PrivateDependencyModuleNames.AddRange(new string[]
  2. {
  3. "DiscordLobby", // Add one...
  4. "DiscordNetwork", // two...
  5. "DiscordImage" // three modules
  6. });
:information_source: You don’t need to do anything for Blueprints and can just start using these modules.

3.1 Discord SDK

This module handles the Discord SDK and load it correctly. It is done for you internally so you won’t have to use it.

3.2. Discord Core

This is the core of the SDK. This is from where you have access to the managers.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/discord

C++ Module Name: DiscordCore

3.3. Discord Achievement

This is where are handled the achievements.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/achievements

C++ Module Name: DiscordAchievement

3.4. Discord Activity

This is where are handled the users’ activity and the so famous Rich Presence.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/activities

C++ Module Name: DiscordActivity

3.5. Discord Application

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/applications

C++ Module Name: DiscordApplication

3.6. Discord Voice

This is where to look if you want to implement voice chat in your game.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/discord-voice

C++ Module Name: DiscordVoice

3.7. Discord Image

This is where images are handle, including users avatars.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/images

C++ Module Name: DiscordImage

3.8. Discord Lobby

This is where multiplayer and lobbies are handled.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/lobbies

C++ Module Name: DiscordLobby

3.9. Discord Networking

If you want to use Discord’s Networking.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/networking

C++ Module Name: DiscordNetworking

3.10. Discord Overlay

To manage the build-in Discord overlay.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/overlay

C++ Module Name: DiscordOverlay

3.11. Discord Relationship

To manage the relationship between your players.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/relationships

C++ Module Name: DiscordRelationship

3.12. Discord Storage

To store data on Discord’s servers.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/storage

C++ Module Name: DiscordStorage

3.13. Discord Store

For in-app purchases and DLC.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/store

C++ Module Name: DiscordStore

3.14. Discord User

To retrieve and manage user data.

Official Discord Documentation: https://discord.com/developers/docs/game-sdk/users

C++ Module Name: DiscordUser

3.15 Discord Gateway

Used to interact with the Discord Gateway.

Official Discord Documentation: https://discord.com/developers/docs/topics/gateway

C++ Module Name: DiscordGateway
|:information_source:|This module doesn’t require the Discord Game SDK and is compatible with any platform.|
|:—-:|:—-|

3.16 Discord HTTP API

Used to interact with the Discord API.

Official Discord Documentation: https://discord.com/developers/docs/resources/webhook

C++ Module Name: DiscordHttpApi
|:information_source:|This module doesn’t require the Discord Game SDK and is compatible with any platform.|
|:—-:|:—-|

4. Getting started

4.1. About the RunCallbacks function

If you read the Discord Game SDK documentation or you already worked with the Discord Game SDK, you might know that the RunCallbacks should be called every tick. However, you don’t have to worry anymore for it ! Discord Features automatically call the RunCallbacks function of Discord Core as well as the Flush function of Discord Network Manager.

4.2. Blueprints

Discord Features tries to look as much as possible like the official Discord Game SDK documentation). You can refer to it to integrate Discord into your game and to the Blueprint Examples.

Nodes that require a callback as parameter have been implemented as a single asynchronous node with multiple output pins. It allows a faster development and to have a better view of your code:

| :information_source: |The On Error pin is executed if Result is not equal to EDiscordResult::Ok.|
|:—:|:—-|

4.3. C++

4.3.1. Includes

Here is an exhaustive list of includes available:

  1. #include "DiscordCore.h" // For UDiscordCore
  2. #include "DiscordAchievementManager.h" // For UDiscordAchievementManager
  3. #include "DiscordActivityManager.h" // For UDiscordActivityManager
  4. #include "DiscordApplicationManager.h" // For UDiscordApplicationManager
  5. #include "DiscordImageManager.h" // For UDiscordImageManager
  6. #include "DiscordLobbyManager.h" // For UDiscordLobbyManager
  7. #include "DiscordNetworkManager.h" // For DiscordNetworkManager
  8. #include "DiscordOverlayManager.h" // For UDiscordOverlayManager
  9. #include "DiscordRelationshipManager.h" // For UDiscordRelationshipManager
  10. #include "DiscordStorageManager.h" // For UDiscordStorageManager
  11. #include "DiscordStoreManager.h" // For UDiscordStoreManager
  12. #include "DiscordUserManager.h" // For UDiscordUserManager
  13. #include "DiscordVoiceManager.h" // For UDiscordVoiceManager
  14. #include "DiscordGatewaySocket.h" // For UDiscordGatewaySocket
  15. #include "DiscordWebhookLibrary.h" // For FDiscordWebhookLibrary

4.3.2. Getting a Manager

The Discord Game SDK has several managers to handle functionalities. You can get those managers from a Discord Core.
To get a manager in C++, you have to do the following:

  1. UDiscordNetworkManager* Manager = UDiscordNetworkManager::GetNetworkManager(DiscordCore);
  2. if (Manager)
  3. {
  4. // Do stuff with the manager
  5. if (Manager->OpenChannel(456, 123, true) == EDiscordResult::Ok)
  6. {
  7. // ...
  8. }
  9. }

4.3.3. Using the Discord Game SDK

Aside from the specificity of getting a manager, Discord Features is used the exact same way as the official SDK. Each method has the same signature with the types of the Unreal Engine instead: Delegates for Callbacks, FString for string, etc.
This is why you can safely refer to the official Discord Game SDK documentation) and to the C++ examples.

5. Examples

5.1. Blueprint Examples

5.1.1. Creating the Core

Creating the core is the starting point when you plan to integrate Discord in you game.

| :warning: |Make sure the Discord Core doesn’t get garbage collected! The Core keeps a hard reference to the managers. The managers are valid while the Core is not garbage collected.|
|:—:|:—-|

:information_source: If you run this code in editor without Discord in background, the Editor will close and launch Discord.

5.1.2. Rich Presence

Rich Presence is really easy to implement:

5.1.3. Create a Lobby

5.2.4. Send a message with Webhooks

5.2. C++ Examples

5.2.1. Creating the Core

  1. UDiscordCore* DiscordCore = UDiscordCore::CreateDiscordCore(EDiscordCoreCreationFlags::Default);
  2. if (DiscordCore)
  3. {
  4. // We have a valid Discord Core !
  5. // We should keep it as a UPROPERTY() to prevent it
  6. // from being garbage collected...
  7. }

5.2.2. Rich Presence

  1. UDiscordActivityManager* ActivityManager = UDiscordActivityManager::GetActivityManager(DiscordCore);
  2. if (ActivityManager)
  3. {
  4. FDiscordActivity Activity;
  5. Activity.State = TEXT("In a dungeon...");
  6. Activity.Details = TEXT("Slaying monsters !");
  7. ActivityManager->UpdateActivity(Activity, FDiscordResultCallback::CreateLambda([](EDiscordResult Result)
  8. {
  9. if (Result == EDiscordResult::Ok)
  10. {
  11. // Activity has been updated !
  12. }
  13. else
  14. {
  15. // An error occured !
  16. }
  17. }));
  18. }

5.2.3. Create a Lobby

  1. UDiscordLobbyManager* LobbyManager = UDiscordLobbyManager::GetLobbyManager(DiscordCore);
  2. UDiscordUserManager* UserManager = UDiscordUserManager::GetUserManager(DiscordCore);
  3. if (LobbyManager && UserManager)
  4. {
  5. const FDiscordUser User = UserManager->GetCurrentUser();
  6. FDiscordLobbyTransaction Transaction = LobbyManager->GetLobbyCreateTransaction();
  7. Transaction.SetCapacity(10);
  8. Transaction.SetLocked(false);
  9. Transaction.SetMetadata(TEXT("SomeKey"), TEXT("SomeValue"));
  10. Transaction.SetOwner(User.Id);
  11. Transaction.SetType(EDiscordLobbyType::Private);
  12. LobbyManager->CreateLobby(Transaction, FLobbyCallback::CreateLambda([](EDiscordResult Result, FDiscordLobby& Lobby)
  13. {
  14. if (Result == EDiscordResult::Ok)
  15. {
  16. // Lobby created.
  17. }
  18. else
  19. {
  20. // An error occured !
  21. }
  22. }));
  23. }

5.2.4. Send a message with Webhooks

You can send a message to a channel with Webhooks. These messages can contain embed content as well.

  1. #include "DiscordWebhookLibrary.h"
  2. // ...
  3. // Get information about the Webhook we want to execute.
  4. FDiscordWebhookLibrary::GetWebhook
  5. (
  6. 9823290121, // Webhook Snowflake.
  7. // Callback called when the information about the Webhook has been received.
  8. FDiscordGetWebhook::CreateLambda([](const FDiscordWebhook& Webhook) -> void
  9. {
  10. FDiscordWebhookData MessageToSend;
  11. MessageToSend.Username = TEXT("My UE4 Bot"); // Username displayed with the message
  12. MessageToSend.Avatar = TEXT("..."); // Avatar displayed with the message
  13. MessageToSend.Content = TEXT("Hello @everyone."); // Message content.
  14. // MessageToSend.Embeds for embeds, MessageToSend.File for files, ...
  15. // Finally execute the Webhook.
  16. FDiscordWebhookLibrary::ExecuteWebhook
  17. (
  18. Webhook.Id,
  19. Webhook.Token,
  20. MessageToSend,
  21. FDiscordExecuteWebhook::CreateLambda([](const bool bExecuted) -> void
  22. {
  23. if (bExecuted)
  24. {
  25. // Message posted.
  26. }
  27. else
  28. {
  29. // An error occured, see the output log!
  30. }
  31. })
  32. );
  33. })
  34. );

6. Discord HTTP API

6.1. Webhooks

Webhooks are a low-effort way to post messages to channels in Discord. They do not require a bot user or authentication to use.
Discord - Features offers a library to easily interact with the Discord HTTP API without needing to touch HTTP requests.

6.1.1. Methods available

Name Description
CreateWebhook Create a new WebHook.
GetChannelWebhooks Returns a list of channel webhook objects. Requires the MANAGE_WEBHOOKS permission.
GetGuildWebhooks Returns a list of channel webhook objects. Requires the MANAGE_WEBHOOKS permission.
GetWebhook Returns the webhook object for the given id.
GetWebhookWithToken Returns the webhook object for the given id.
ModifyWebhook Modify a webhook. Requires the MANAGE_WEBHOOKS permission. Returns the updated webhook object on success.
ModifyWebhookWithToken Modify a webhook. Requires the MANAGE_WEBHOOKS permission. Returns the updated webhook object on success.
DeleteWebhook Delete a webhook permanently.
DeleteWebhookWithToken Delete a webhook permanently.
ExecuteWebhook Execute the Webhook.

6.1.2. Configuration

To use the Webhooks, you need to have enough credentials to do so. An easy way to do it is to create a bot and get a secret bot token. Copy the secret Bot Token to the Discord HTTP API settings window located in Project Settings > Plugins > Discord HTTP API.
Discord HTTP API config window
| :warning: |This method should be used for Dedicated Servers. Be careful that your token doesn’t get distributed with your game to the players. The token is stored in DiscordHttpApi.ini, see Unreal Engine Config System to make per-build config files. |
|:—:|:—-|

7. Discord Gateway

The Discord Gateway can be used to establish a real-time communication with the Discord API over secure WebSockets. This plugin offers an interface to interact with the Discord Gateway.

You can connect to the Gateway with the Connect To Discord Gateway node:

Blueprints Connect to Gateway example

Once you have a UDiscordGatewaySocket object, you can start sending and receiving commands to/from the Discord Gateway.

8. Contact

If you need help, spotted a bug, have a feature request or experience troubles, please contact us at DiscordFeatures@gmail.com?subject=DiscordFeatures%20-%20"">pandores.marketplace@gmail.com.

—>