Authenticate 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 Set Up the Unity SDK and Log Into the Platform before starting this section.
Discord admin panel setup #
- Log into the Discord Admin Panel.
- 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.
- Name your application, then click the Create button.
- (optional) After your application has been created, you can click on the Bot section in the left sidebar, then click the Add Bot button. Confirm that you wish to create a bot. Once the bot has been created, click Reset Token and save the generated token–you can use this later in your config as the
botToken
.
- 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.
- In the left sidebar, click URL Generator which is nested under OAuth2. Tick the
identify
andemail
boxes, then choosehttp://localhost:11000/v1/account/discord-redirect
under the Select Redirect URL dropdown.
Configure the Pragma Engine Discord Identity Provider #
- Obtain your
clientSecret
andclientId
from the OAuth2 General tab of your application in the Discord Admin Panel. If you have not yet been issued a Client Secret, click the Reset Secret button.
The fieldsbotToken
,guildId
, andauthorizedRoleId
are only required if you plan on using a bot to automatically authorize users from your Discord server who have a specific role.
Open the User Settings cog in your Discord client, then scroll down to Advanced and toggle on Developer Mode.
Access your
guildId
by right-clicking on a server and selecting Copy ID.Access your
authorizedRoleId
by opening Server Settings within a server, opening the Roles page, right-clicking on the desired Role, and selecting Copy ID.Navigate to the
pragma-engine/platform/<PROJECT>/config/
directory and openlocal-dev.yml
.Insert the following code while making sure to edit in your
clientId
,clientSecret
, andredirectUri
:
social:
pluginConfigs:
AccountService.identityProviderPlugins:
plugins:
Discord:
class: "pragma.account.DiscordIdentityProviderPlugin"
config:
clientId: "${discordClientId}"
clientSecret: "${discordClientSecret}"
redirectUri: "http://localhost:11000/v1/account/discord-redirect"
botToken: "${discordBotToken}"
guildId: "${guildId}"
allowedRoleIds:
1: "${RoleId1}"
2: "${RoleId2}"
playerLoginEnabled: true
operatorLoginEnabled: false
accountLinkingEnabled: true
showPortalLoginButton: false
visibleToOtherPlayers: false
- Run Pragma Engine via one of the following methods.
Once the engine has started successfully, it prints the message INFO main - Pragma server startup complete
.
Import the Discord SDK into Unity #
- Download the Discord SDK from the Discord Developer site.
- Follow the directions on the Discord developer site to import the Discord SDK into your Unity project.
- Remove the
x86
folder from your Unity Project’sAssets/Plugins
directory.
If you’re building for a 32-bit platform, then remove the x86-64
folder instead.
- Open your game project in Unity Editor.
Integrate your game with Discord #
In the Project panel, navigate to your
Scripts
directory and create a newDiscordController
C# script.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;
});
}
}
- Open the
PragmaManager
script and add the following method to the class:
public void LoginViaDiscord(string discordToken)
{
player.LogIn(IdProvider.Discord, discordToken, HandleLoggedIn);
}
- In Unity Editor, locate the
DiscordController
script in the Project panel underAssets/Scripts
. Drag theDiscordController
script ontoPragmaManager
in the Hierarchy panel.
- Select the
Login
button in the Hierarchy panel. In the Inspector panel, change the On Click () function toDiscordController.DiscordLogin ()
.
Ensure that you have Discord running and that you’re logged in.
In Unity Editor, click the play button, then click the Login button. You may need to authorize the login.
Navigate to your local Pragma Portal then sign in via Pragma Unsafe using the
test04
account.Confirm that you see both the
test04
account and your Discord account’s display name in the list of logged-in players.Click on the Discord account’s display name. Under the Identity providers section, verify that the account’s identity provider is Discord.