Skip to content

Commit 8ff2018

Browse files
committed
fix: correctly handle discord connection
- that is remove the Connect call and the status, all needed functionality can be called, without connecting to the gateway, the ipc connection is done implicitly on creation
1 parent 4de61e0 commit 8ff2018

File tree

3 files changed

+19
-99
lines changed

3 files changed

+19
-99
lines changed

src/discord/core.cpp

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
}
1717
}
1818

19-
DiscordInstance::DiscordInstance()
20-
: m_current_user{ discordpp::UserHandle::nullobj },
21-
m_status{ DiscordStatus::Starting },
22-
m_current_activity{ std::nullopt } {
19+
20+
DiscordInstance::DiscordInstance() : m_current_user{ discordpp::UserHandle::nullobj } {
21+
22+
m_client.SetApplicationId(constants::discord::application_id);
2323

2424
m_client.AddLogCallback(
2525
[](std::string message, discordpp::LoggingSeverity severity) -> void {
@@ -47,28 +47,7 @@ DiscordInstance::DiscordInstance()
4747
#endif
4848
);
4949

50-
51-
m_client.SetStatusChangedCallback(
52-
[this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t error_detail) -> void {
53-
if (error != discordpp::Client::Error::None) {
54-
this->m_status = DiscordStatus::Error;
55-
spdlog::error(
56-
"Connection Error: {} - Details: {}", discordpp::Client::ErrorToString(error), error_detail
57-
);
58-
return;
59-
}
60-
61-
if (status == discordpp::Client::Status::Ready) {
62-
this->m_status = DiscordStatus::Ok;
63-
this->after_ready();
64-
return;
65-
}
66-
}
67-
);
68-
69-
m_client.SetApplicationId(constants::discord::application_id);
70-
71-
m_client.Connect();
50+
after_ready();
7251
}
7352

7453
void DiscordInstance::after_ready() {
@@ -78,15 +57,8 @@ void DiscordInstance::after_ready() {
7857
[this](const discordpp::ClientResult& result, std::optional<discordpp::UserHandle> user) -> void {
7958
if (result.Successful() and user.has_value()) {
8059

81-
auto user_handle = m_client.GetUser(user->Id());
82-
if (not user_handle.has_value()) {
83-
spdlog::error("Current Connected User Error: Can't get userhandle from id: {}", user->Id());
84-
85-
return;
86-
}
87-
88-
this->m_current_user = user_handle.value();
89-
spdlog::info("Current user updated: {}", user_handle->Username());
60+
this->m_current_user = user.value();
61+
spdlog::info("Current user updated: {}", user->Username());
9062

9163
return;
9264
}
@@ -101,20 +73,14 @@ void DiscordInstance::after_ready() {
10173
if (not result) {
10274
spdlog::warn("Discord: Failed to Register Launch Command");
10375
}
104-
105-
if (m_current_activity.has_value()) {
106-
set_activity_internal();
107-
}
10876
}
10977

11078

11179
DiscordInstance::DiscordInstance(DiscordInstance&& old) noexcept
11280
: m_client{ std::move(old.m_client) },
113-
m_current_user{ std::move(old.m_current_user) },
114-
m_status{ old.m_status } {
81+
m_current_user{ std::move(old.m_current_user) } {
11582
old.m_client = discordpp::Client{};
11683
old.m_current_user = discordpp::UserHandle::nullobj;
117-
old.m_status = DiscordStatus::Error;
11884
}
11985

12086

@@ -123,71 +89,41 @@ DiscordInstance& DiscordInstance::operator=(DiscordInstance&& other) noexcept {
12389

12490
m_client = std::move(other.m_client);
12591
m_current_user = std::move(other.m_current_user);
126-
m_status = other.m_status;
12792

12893
other.m_client = discordpp::Client{};
12994
other.m_current_user = discordpp::UserHandle::nullobj;
130-
other.m_status = DiscordStatus::Error;
13195
}
13296
return *this;
13397
};
13498

13599
DiscordInstance::~DiscordInstance() {
136100
if (m_client.operator bool()) {
137101
clear_activity();
138-
m_client.Disconnect();
139102
}
140103
}
141104

142-
[[nodiscard]] DiscordStatus DiscordInstance::get_status() {
143-
return m_status;
144-
}
145-
146105
void DiscordInstance::update() {
147106
discordpp::RunCallbacks();
148107
}
149108

150109

151110
void DiscordInstance::set_activity(DiscordActivityWrapper activity) {
152111

153-
if (m_status == DiscordStatus::Error) {
154-
// don't set activity
155-
return;
156-
}
157112

158-
m_current_activity = std::move(activity);
159-
160-
if (m_status == DiscordStatus::Starting) {
161-
// return after we stored the current activity, the after_ready callback will set the activity
162-
return;
163-
}
164-
165-
set_activity_internal();
166-
}
167-
168-
void DiscordInstance::set_activity_internal() {
169-
170-
if (not m_current_activity.has_value()) {
171-
return;
172-
}
173-
174-
175-
const auto& raw_activity = m_current_activity.value().get_raw();
113+
const auto& raw_activity = activity.get_raw();
176114

177115
if (not raw_activity.operator bool()) {
178116
spdlog::error("Tried to set an invalid Discord Activity!");
179117
return;
180118
}
181119

182120
// Update rich presence
183-
m_client.UpdateRichPresence(raw_activity, [this](const discordpp::ClientResult& result) {
121+
m_client.UpdateRichPresence(raw_activity, [](const discordpp::ClientResult& result) {
184122
if (result.Successful()) {
185123
spdlog::info("Rich Presence updated successfully");
186124
} else {
187125
spdlog::error("Rich Presence update failed: {}", result.ToString());
188126
}
189-
190-
this->m_current_activity = std::nullopt;
191127
});
192128
}
193129

src/discord/core.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,14 @@ struct DiscordActivityWrapper {
129129
[[nodiscard]] OOPETRIS_GRAPHICS_EXPORTED const discordpp::Activity& get_raw() const;
130130
};
131131

132-
enum class DiscordStatus : u8 { Starting = 0, Ok, Error };
133-
134132
struct DiscordInstance {
135133
private:
136134
discordpp::Client m_client;
137135
discordpp::UserHandle m_current_user;
138-
DiscordStatus m_status;
139-
std::optional<DiscordActivityWrapper> m_current_activity;
140-
141136

142137
public:
143138
OOPETRIS_GRAPHICS_EXPORTED explicit DiscordInstance();
144139

145-
[[nodiscard]] OOPETRIS_GRAPHICS_EXPORTED DiscordStatus get_status();
146-
147140
OOPETRIS_GRAPHICS_EXPORTED DiscordInstance(DiscordInstance&& old) noexcept;
148141

149142
OOPETRIS_GRAPHICS_EXPORTED DiscordInstance& operator=(DiscordInstance&& other) noexcept;
@@ -159,8 +152,6 @@ struct DiscordInstance {
159152
OOPETRIS_GRAPHICS_EXPORTED void set_activity(DiscordActivityWrapper activity);
160153

161154
private:
162-
void set_activity_internal();
163-
164155
void after_ready();
165156
void clear_activity();
166157
};

src/executables/game/application.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,11 @@ void Application::run() {
202202
return;
203203
#else
204204

205-
while (m_is_running
205+
while (
206+
m_is_running
206207

207208
#if defined(__CONSOLE__)
208-
and console::inMainLoop()
209+
and console::inMainLoop()
209210
#endif
210211

211212
) {
@@ -383,7 +384,8 @@ void Application::update() {
383384
},
384385
[this](const scenes::Scene::Push& push) {
385386
spdlog::info("pushing back scene {}", magic_enum::enum_name(push.target_scene));
386-
m_scene_stack.push_back(scenes::create_scene(*this, push.target_scene, push.layout)
387+
m_scene_stack.push_back(
388+
scenes::create_scene(*this, push.target_scene, push.layout)
387389
);
388390
},
389391
[this](scenes::Scene::RawPush& raw_push) {
@@ -432,17 +434,7 @@ void Application::update() {
432434
#if defined(_HAVE_DISCORD_SOCIAL_SDK)
433435

434436
if (m_discord_instance.has_value()) {
435-
436-
switch (m_discord_instance->get_status()) {
437-
case DiscordStatus::Error:
438-
m_discord_instance = std::nullopt;
439-
spdlog::warn("Error initializing the discord instance, it might not be running, destroying client!");
440-
break;
441-
case DiscordStatus::Starting:
442-
case DiscordStatus::Ok:
443-
DiscordInstance::update();
444-
break;
445-
}
437+
DiscordInstance::update();
446438
}
447439

448440
#endif
@@ -542,9 +534,10 @@ void Application::initialize() {
542534
emscripten_set_main_loop_arg(c_loop_entry, this, selected_fps, true);
543535
UNREACHABLE();
544536
#else
545-
while ((not m_loading_info->m_finished_loading) and m_is_running
537+
while (
538+
(not m_loading_info->m_finished_loading) and m_is_running
546539
#if defined(__CONSOLE__)
547-
and console::inMainLoop()
540+
and console::inMainLoop()
548541
#endif
549542
) {
550543
load_loop();

0 commit comments

Comments
 (0)