Skip to content

Get Started

Warning

Prerequisites:

How it works

This GDExtension is a wrapper around the C++ SDK, which means that each GDScript method it's just calling the C++ counterpart.

This means that you could read the official C++ documentation and convert the code to GDScript. For example, this is their conclusion code from Getting Started with C++ converted to GDScript:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#define DISCORDPP_IMPLEMENTATION
#include "discordpp.h"
#include <iostream>
#include <thread>
#include <atomic>
#include <string>
#include <functional>
#include <csignal>

// Replace with your Discord Application ID
const uint64_t APPLICATION_ID = 1349146942634065960;

// Create a flag to stop the application
std::atomic<bool> running = true;

// Signal handler to stop the application
void signalHandler(int signum) {
    running.store(false);
}

int main() {
    std::signal(SIGINT, signalHandler);
    std::cout << "🚀 Initializing Discord SDK...\n";

    // Create our Discord Client
    auto client = std::make_shared<discordpp::Client>();

    // Set up logging callback
    client->AddLogCallback([](auto message, auto severity) {
    std::cout << "[" << EnumToString(severity) << "] " << message << std::endl;
    }, discordpp::LoggingSeverity::Info);

    // Set up status callback to monitor client connection
    client->SetStatusChangedCallback([client](discordpp::Client::Status status, discordpp::Client::Error error, int32_t errorDetail) {
    std::cout << "🔄 Status changed: " << discordpp::Client::StatusToString(status) << std::endl;

    if (status == discordpp::Client::Status::Ready) {
        std::cout << "✅ Client is ready! You can now call SDK functions.\n";

        // Access initial relationships data
        std::cout << "👥 Friends Count: " << client->GetRelationships().size() << std::endl;

        // Configure rich presence details
        discordpp::Activity activity;
        activity.SetType(discordpp::ActivityTypes::Playing);
        activity.SetState("In Competitive Match");
        activity.SetDetails("Rank: Diamond II");

        // Update rich presence
        client->UpdateRichPresence(activity, [](discordpp::ClientResult result) {
        if(result.Successful()) {
            std::cout << "🎮 Rich Presence updated successfully!\n";
        } else {
            std::cerr << "❌ Rich Presence update failed";
        }
        });

    } else if (error != discordpp::Client::Error::None) {
        std::cerr << "❌ Connection Error: " << discordpp::Client::ErrorToString(error) << " - Details: " << errorDetail << std::endl;
    }
    });

    // Generate OAuth2 code verifier for authentication
    auto codeVerifier = client->CreateAuthorizationCodeVerifier();

    // Set up authentication arguments
    discordpp::AuthorizationArgs args{};
    args.SetClientId(APPLICATION_ID);
    args.SetScopes(discordpp::Client::GetDefaultPresenceScopes());
    args.SetCodeChallenge(codeVerifier.Challenge());

    // Begin authentication process
    client->Authorize(args, [client, codeVerifier](auto result, auto code, auto redirectUri) {
    if (!result.Successful()) {
        std::cerr << "❌ Authentication Error: " << result.Error() << std::endl;
        return;
    } else {
        std::cout << "✅ Authorization successful! Getting access token...\n";

        // Exchange auth code for access token
        client->GetToken(APPLICATION_ID, code, codeVerifier.Verifier(), redirectUri,
        [client](discordpp::ClientResult result,
        std::string accessToken,
        std::string refreshToken,
        discordpp::AuthorizationTokenType tokenType,
        int32_t expiresIn,
        std::string scope) {
            std::cout << "🔓 Access token received! Establishing connection...\n";
            // Next Step: Update the token and connect
            client->UpdateToken(discordpp::AuthorizationTokenType::Bearer,  accessToken, [client](discordpp::ClientResult result) {
            if(result.Successful()) {
                std::cout << "🔑 Token updated, connecting to Discord...\n";
                client->Connect();
            }
            });
        });
    }
    });

    // Keep application running to allow SDK to receive events and callbacks
    while (running) {
        discordpp::RunCallbacks();
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    return 0;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
extends Control

# Replace with your Discord Application ID
var APPLICATION_ID: int = 1349146942634065960

var client := DiscordClient.new()
var args := DiscordAuthorizationArgs.new()
var code_verifier: DiscordAuthorizationCodeVerifier = null


func _ready() -> void:
    print("🚀 Initializing Discord SDK...")

    client.add_log_callback(_on_log_message, DiscordLoggingSeverity.INFO)
    client.set_status_changed_callback(_on_status_changed)

    code_verifier = client.create_authorization_code_verifier()

    args.set_client_id(APPLICATION_ID)
    args.set_scopes(DiscordClient.get_default_presence_scopes())
    args.set_code_challenge(code_verifier.challenge())
    client.authorize(args, _on_authorized)


func _process(_delta: float) -> void:
    Discord.run_callbacks()


func _on_log_message(message: String, severity: DiscordLoggingSeverity.Enum) -> void:
    print("[%s] %s" % [Discord.enum_to_string(severity, DiscordLoggingSeverity.id), message])


func _on_status_changed(status: DiscordClientStatus.Enum, error: DiscordClientError.Enum, error_detail: int) -> void:
    print("🔄 Status changed: %s" % status)

    if status == DiscordClientStatus.READY:
        print("✅ Client is ready! You can now call SDK functions.")
        print("👥 Friends Count: %s" % client.get_relationships().size())

        var activity := DiscordActivity.new()
        activity.set_type(DiscordActivityTypes.PLAYING)
        activity.set_state("In Competitive Match")
        activity.set_details("Rank: Diamond II")

        client.update_rich_presence(activity, _on_rich_presence_updated)
    elif error != DiscordClientError.NONE:
        print("❌ Connection Error: %s - Details: %s" % [error, error_detail])


func _on_rich_presence_updated(result: DiscordClientResult) -> void:
    if result.successful():
        print("🎮 Rich Presence updated successfully!")
    else:
        print("❌ Rich Presence update failed")


func _on_authorized(result: DiscordClientResult, code: String, redirect_uri: String) -> void:
    if not result.successful():
        print("❌ Authentication Error: %s" % result.error())
    else:
        print("✅ Authorization successful! Getting access token...")

        client.get_token(APPLICATION_ID, code, code_verifier.verifier(), redirect_uri, _on_token_received)


func _on_token_received(
        _result: DiscordClientResult,
        access_token: String,
        _refresh_token: String,
        _token_type: DiscordAuthorizationTokenType.Enum,
        _expires_in: int,
        _scopes: String
) -> void:
    print("🔓 Access token received! Establishing connection...")

    client.update_token(DiscordAuthorizationTokenType.BEARER, access_token, _on_token_updated)


func _on_token_updated(result: DiscordClientResult) -> void:
    if result.successful():
        print("🔑 Token updated, connecting to Discord...")

        client.connect_discord()

The advantage of reading their official documentation and converting C++ to GDScript is that they explain in details their SDK.

Doing the convertion can be annoying, so this documentation includes a bunch of GDScript examples to help you skip this part!

Tip

F1 is your best friend!

You can probably find the class that you are looking for by searching part of it name.


I extracted their official documentation to classes/methods using Doxygen, so you probably don't need to check Discord Social SDK Reference to know how they work.


We attempt to follow GDScript style guide, so you can probably guess the names of methods, properties, enums, methods, params...