1+ #include " Subsystem/Discord.h"
2+ #define DISCORDPP_IMPLEMENTATION
3+ #include < Discord/discordpp.h>
4+
5+ Discord* Discord::m_instance;
6+ std::atomic<bool > running = true ;
7+
8+ Discord::Discord () {
9+ this ->m_appId = 0 ;
10+ }
11+
12+ void Discord::ClientThread () {
13+ auto client = std::make_shared<discordpp::Client>();
14+ spdlog::info (" Discord Social SDK: Running on a detached thread" );
15+
16+ client->AddLogCallback ([](std::string message, auto severity) {
17+ std::string cleanMessage = message;
18+ cleanMessage.erase (std::remove (cleanMessage.begin (), cleanMessage.end (), ' \n ' ), cleanMessage.end ());
19+ spdlog::debug (" Discord Social SDK: {0}" , cleanMessage);
20+ }, discordpp::LoggingSeverity::Info);
21+
22+ client->SetStatusChangedCallback ([&client](discordpp::Client::Status status, discordpp::Client::Error error, int32_t errorDetail) {
23+ spdlog::debug (" Discord Social SDK: Status changed to {0}" , discordpp::Client::StatusToString (status));
24+
25+ if (status == discordpp::Client::Status::Ready) {
26+ spdlog::debug (" Discord Social SDK: Client ready!" );
27+
28+ discordpp::Activity activity;
29+ activity.SetType (discordpp::ActivityTypes::Playing);
30+ activity.SetState (" Exeon Game Engine v0.1" );
31+ activity.SetDetails (" Editing Fiwa" );
32+
33+ // Update rich presence
34+ client->UpdateRichPresence (activity, [](discordpp::ClientResult result) {
35+ if (result.Successful ()) {
36+ spdlog::debug (" Discord Social SDK: Rich Presence updated" );
37+ } else {
38+ spdlog::error (" Discord Social SDK: Error updating Rich Presence" );
39+ }
40+ });
41+ } else if (error != discordpp::Client::Error::None) {
42+ spdlog::debug (" Discord Social SDK: Client disconnected!" );
43+ }
44+ });
45+
46+ auto codeVerifier = client->CreateAuthorizationCodeVerifier ();
47+
48+ discordpp::AuthorizationArgs args{};
49+ args.SetClientId (this ->m_appId );
50+ args.SetScopes (discordpp::Client::GetDefaultPresenceScopes ());
51+ args.SetCodeChallenge (codeVerifier.Challenge ());
52+
53+ client->Authorize (args, [client, codeVerifier, this ](auto result, auto code, auto redirectUri) {
54+ if (!result.Successful ()) {
55+ spdlog::error (" Discord Social SDK: Authorization failed!" );
56+ } else {
57+ spdlog::debug (" Discord Social SDK: Authorization successful!" );
58+
59+ // Exchange auth code for access token
60+ client->GetToken (this ->m_appId , code, codeVerifier.Verifier (), redirectUri,
61+ [client](discordpp::ClientResult result,
62+ std::string accessToken,
63+ std::string refreshToken,
64+ discordpp::AuthorizationTokenType tokenType,
65+ int32_t expiresIn,
66+ std::string scope) {
67+ spdlog::debug (" Discord Social SDK: Establishing connection" );
68+ client->UpdateToken (discordpp::AuthorizationTokenType::Bearer, accessToken, [client](discordpp::ClientResult result) {
69+ if (result.Successful ()) {
70+ spdlog::info (" Discord Social SDK: Token updated" );
71+ client->Connect ();
72+ }
73+ });
74+
75+ });
76+ }
77+ });
78+
79+ while (running) {
80+ discordpp::RunCallbacks ();
81+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
82+ }
83+ }
84+
85+
86+ void Discord::Init () {
87+ this ->m_appId = 1374036377322258544 ;
88+ std::thread discordThread (&Discord::ClientThread, this );
89+ discordThread.detach ();
90+ }
91+
92+ Discord* Discord::GetInstance () {
93+ if (Discord::m_instance == nullptr )
94+ Discord::m_instance = new Discord ();
95+ return Discord::m_instance;
96+ }
0 commit comments