Authenticating with Discord #

In this section, we’ll log into the platform with Discord. This method is appropriate for use in production. A Discord account is required to complete this section.

Complete Setting Up the Unity SDK and Logging Into the Platform before starting this section.

Discord Developer Portal Setup #

  1. Log into the Discord Developer Portal.
  2. Click on the Applications tab, then click the New Application button.
If the application has already been created, then you can click on it under My Applications.

  1. Name your application, then click the Create button.

  1. After your application has been created, click on the Bot section in the left sidebar, then click the Add Bot button. Confirm that you wish to create a bot.

  1. Click on OAuth2 in the left sidebar, then click the Add Redirect button. Paste the following URL: http://localhost:11000/v1/account/discord-redirect. Click the Save Changes button.

  1. In the left sidebar, click URL Generator which is nested under OAuth2. Tick the identify and email boxes, then choose http://localhost:11000/v1/account/discord-redirect under the Select Redirect URL dropdown.

Configure the Pragma Platform Discord Identity Provider #

  1. Obtain your clientSecret and clientId from the OAuth2 General tab of your application in the Discord Developer Portal. If you have not yet been issued a Client Secret, click the Reset Secret button.
Be sure to securely record your Client Secret, as it will not be shown again.

  1. Navigate to the pragma-engine/platform/5-ext/config directory and open LocalConfig.yml.

  2. Insert the following code while making sure to edit in your clientSecret and clientId:

Be sure to nest AccountService.discordIdentityProvider under pluginConfigs if you already have other identity providers configured.
social:
  pluginConfigs:
    AccountService.discordIdentityProvider:
      class: "pragma.account.DiscordIdentityProviderPlugin"
      config:
        clientSecret: "*YOUR CLIENT SECRET"
        clientId: "*YOUR CLIENT ID*"
        redirectUri: "http://localhost:11000/v1/account/discord-redirect"
  1. Run make run to start Pragma Engine. Run this in a terminal with platform as the working directory.
  • Alternatively, Pragma Engine can be run from IntelliJ. From the IntelliJ toolbar in the upper right, ensure MainKt - LocalConfigured is selected, then click the play button.

  • Once the engine has successfully started it prints the message [main] INFO main - Pragma server startup complete.

Import the Discord SDK Into Unity #

  1. Download the Discord SDK from the Discord Developer site.
  2. Follow the directions on the Discord developer site to import the Discord SDK into your Unity project.
  3. Remove the x86 folder from your Unity Project’s Assets/Plugins directory.
If you’re building for a 32-bit platform, then remove the x86-64 folder instead.
  1. Open your game project in Unity Editor.

Integrate your game with Discord #

  1. In the Project panel, navigate to your Scripts directory and create a new DiscordController C# script.

  2. Replace the contents of DiscordController with the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Discord;

public class DiscordController : MonoBehaviour
{

    public Discord.Discord discord;

    [SerializeField] private PragmaManager pragmaManager;

    // Use this for initialization
    void Start()
    {
        pragmaManager = this.GetComponent<PragmaManager>();
        discord = new Discord.Discord(
            "**INSERT YOUR DISCORD APPLICATION CLIENT ID HERE**",
            (System.UInt64)Discord.CreateFlags.Default
        );
    }

    // Update is called once per frame
    void Update()
    {
        discord.RunCallbacks();
    }

    public void DiscordLogin()
    {
        var appManager = discord.GetApplicationManager();

        // Retrieve the token
        appManager.GetOAuth2Token((Discord.Result result, ref Discord.OAuth2Token token) =>
        {
            Debug.Log(token.AccessToken);
            // Now send that token off to your server to do your own validation!
            pragmaManager.LoginViaDiscord(token.AccessToken);
            return;
        });
    }
}
  1. Open the PragmaManager script and add the following method to the class:
public void LoginViaDiscord(string discordToken)
    {
        session.Account.LogIn(IdProvider.Discord, discordToken, loginResult =>
        {
            if (loginResult.IsSuccessful)
            {
                // Success is handled in_session.Connection.OnInitiallyConnected above.
                // No reason you couldn't handle it here though.
                
                return;
            }
            else
            {
                //Something went wrong
            }
        });
    }
  1. In Unity Editor, locate the DiscordController script in the Project panel under Assets/Scripts. Drag the DiscordController script onto PragmaManager in the Hierarchy panel.

  1. Select the Login button in the Hierarchy panel. In the Inspector panel, change the On Click () function to DiscordController.DiscordLogin ().

  1. Ensure that you have Discord running and that you’re logged in.

  2. In Unity Editor, click the play button, then click the Login button. You may need to authorize the login.

  3. Navigate to your local Pragma Platform’s portal then sign in via Pragma Unsafe using the test04 account.

  4. Confirm that you see both the test04 account and your Discord account’s display name in the list of logged-in players.

  5. Click on the Discord account’s display name. Under the Identity providers section, verify that the account’s identity provider is Discord.