Foundation for Discord Social

// Config: Engine - [], Toolkit - [v1.0.0]

Foundation for Discord Social is a free, open-source (Apache-2.0) layer over Discord’s own Social SDK for Unity. It owns the SDK client through a Game Framework subsystem and gives you authentication with token storage and automatic refresh, rich presence, friends and relationships, lobbies, messaging, voice calls with audio devices and call controls, and a complete data model, with System Log and Game Framework Bus integration built in.

The SDK is yours to install #

Discord does not allow its SDK to be redistributed, so this package does not include it. You install the Discord Social SDK 1.10.0 or newer as the package com.discord.partnersdk, once per project. See Install the Discord Social SDK. Until it is installed nothing in the Foundation compiles and nothing errors; a window tells you what is missing and closes by itself when the SDK arrives.

Requirements #

  • Unity 6000.0 or newer.
  • Discord Social SDK 1.10.0 or newer (com.discord.partnersdk), downloaded from Discord and installed by you.
  • Heathen Game Framework (com.heathen.gameframework), free and Apache-2.0. Heathen Toolbox installs it for you.

What it does #

AreaWhat you get
LifecycleDiscordSocialSubsystem creates, pumps and tears down the SDK client. It starts itself: with your Application Id set in Project Settings, Subsystems, Discord Social, the default Automatic start mode initialises Discord Social at start and no component is needed. DiscordSocialApp.Initialize and the optional InitializeDiscordSocial component remain for manual control.
AuthenticationOAuth, stored token reconnect, provisional tokens for any external provider, a pluggable IDiscordTokenStore, and automatic refresh before expiry that keeps Discord’s rotated refresh token. No credential is ever logged.
Remember Sign InOn by default. PersistentDiscordTokenStore keeps the token encrypted in PlayerPrefs with a key derived from the device, and the subsystem signs the player in again with it on the next launch. That is obfuscation tied to the device, not a vault.
PresenceRich presence from an ActivityData or from a lobby, invites, join requests and accepting an invite.
RelationshipsFriends, pending requests, blocked users, and the request, accept, reject, cancel, remove and block operations.
Lobbies and messagingCreate or join with metadata, members, channel linking, lobby and direct messages, and message history.
Calls and voiceStart and end calls. GetCallParticipants returns each person with their mute, deafen, local mute, volume and speaking state. Audio mode (voice activity or push to talk), push to talk, voice activity threshold, participant volume and local mute are all controllable.
Audio devicesList and choose microphones and speakers, read volumes and self mute or deafen, and react to devices changing or no audio input.
Voice into your own audio engineStartCallWithAudio hands you each participant’s audio (and optionally your microphone) on the SDK’s audio thread, so you can play voices through your own audio, for example in 3D.
Data modelUserData, LobbyData, LobbyMemberData, RelationshipData, MessageData, GuildData, ChannelData, ActivityData, VoiceSettingsData, AudioDeviceData and CallParticipantData.
DiscordUserDataA component that holds one Discord user, with a Local User tick, Bind(UserData) and Bind(ulong), and an onChanged event that follows your own status too. Display components under it update when it changes.
EventsEvery event is a UnityEvent on DiscordSocialApp and a Bus message under Heathen.Discord, including SpeakingChanged, AudioDevicesChanged and NoAudioInput. The DiscordEventTriggers component exposes them in the Inspector.
LoggingSystem Log categories Discord.App, Discord.Auth, Discord.Lobby, Discord.Voice, Discord.Presence, Discord.Messaging, Discord.Relationships and Discord.Guilds.
SteamDiscordSteamLogin signs the player in with their Steam identity. It builds only when Steamworks.NET is present. A variant waits for Foundation for Steamworks to be ready first.

Every operation has a callback form and a Task form. The UniTask forms are in Toolkit for Discord Social.

Quick start #

  1. Install the SDK, see Install the Discord Social SDK.
  2. Open Project Settings, Subsystems, Discord Social and enter your Application Id from the Discord Developer Portal. The start mode is Automatic and Remember Sign In is on by default.
  3. Press Play. Discord Social initialises on its own. Connect, and wait for ready before using the rest of the API.
using Heathen.DiscordSocialIntegration;
using Heathen.DiscordSocialIntegration.API;

DiscordSocialApp.OnReady.AddListener(() =>
{
    var me = DiscordSocialApp.GetMyUserData();
    var activity = ActivityData.Playing("My Game");
    activity.State = "In the menu";
    DiscordSocialApp.SetRichPresence(activity.ToActivity(), result => { });
});

DiscordSocialApp.Connect();

Connect returning no error means a connection was requested, not that the client is connected. Wait for OnReady. The player authorises in the browser the first time; with Remember Sign In on, later launches sign in again with the saved token. A project with no settings asset still waits for DiscordSocialApp.Initialize(appId), and a warning is logged when the mode is Automatic but no Application Id is set.

Remembering the player #

With Remember Sign In on, nothing more is needed. To store the token somewhere of your own, assign your own store before initialising.

DiscordSocialApp.TokenStore = new MyProtectedTokenStore();

A token store holds credentials, so back it with encrypted or platform-protected storage. Discord rotates refresh tokens, so always persist the token you are given by the session, never an older copy.

Voice #

Start a call on a lobby, then use GetCallParticipants, IsSpeaking and OnSpeakingChanged to see who is in it and who is talking. SetAudioMode, SetPushToTalkActive, SetParticipantVolume and SetLocalMute control it. GetInputDevices, GetOutputDevices, SetInputDevice and SetOutputDevice choose the hardware. To play voices through your own audio engine, call DiscordSocialApp.StartCallWithAudio. Its handlers run on the SDK’s audio thread, so do not touch Unity objects from them, and copy the samples if you keep them, because the array is reused. The Toolkit for Discord Social has ready-made voice components, and see Voice for 3D voice.

Voice requires the players to have Discord. It is not a replacement for a full voice product: there are no proximity or team channels built in.

Events #

Each event exists in two forms: a UnityEvent on DiscordSocialApp and a Game Framework Bus message named Heathen.Discord.<Name>. Use the DiscordEventTriggers component to wire them in the Inspector without code.

Status #

Version 1.0.0 compiles and passes its Edit Mode tests. It has been tested live against a Discord application: sign in through the browser, signing in again on the next launch, your own profile, the friends list and statuses, friend requests, changing your own status, sending an invite, creating a lobby, two-way voice between two accounts, joining, leaving and rejoining a call, the participant list, mute, deafen, volumes, input mode, the device lists and the voice setting toggles including noise cancellation. Accepting an invite, joining by code, a lobby with several members and hearing another person’s voice through a 3D voice source have not been confirmed yet, so treat those as unverified and report anything you find. Reading the current state of the noise, echo and gain switches needs the SDK to find the Discord desktop client over RPC, which it cannot do in the Editor or with the Flatpak build of Discord on Linux.

Getting the interface layer #

Toolkit for Discord Social builds ready-made interface on top of this Foundation: user profile, friends lists with search and groups, guild lists, lobby and invite components, voice bar and settings, prefabs, a demo game and UniTask forms of every operation. It is part of the paid Toolkit library.

The source is on Codeberg under Apache-2.0. Your use of the Discord Social SDK is governed by Discord’s own developer terms and policies.

Rate This Article!