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
7453void 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
11179DiscordInstance::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
13599DiscordInstance::~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-
146105void DiscordInstance::update () {
147106 discordpp::RunCallbacks ();
148107}
149108
150109
151110void 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
0 commit comments