99#include " DppClient.h"
1010#include " DppCommandHandler.h"
1111
12- using namespace dpp ;
12+ // #include <iostream>
13+ // using std::cout;
14+ // using std::endl;
15+
1316namespace PokemonAutomation {
1417namespace Integration {
1518namespace DppClient {
1619
20+ using namespace dpp ;
21+
1722
1823Client& Client::instance (){
1924 static Client client;
2025 return client;
2126}
2227
28+
29+
30+ Client::~Client (){
31+ disconnect ();
32+ }
33+
2334bool Client::is_initialized (){
2435 std::lock_guard<std::mutex> lg (m_client_lock);
2536 return m_bot != nullptr ;
@@ -41,7 +52,7 @@ void Client::connect(){
4152 m_bot = std::make_unique<cluster>(token, intents);
4253 m_handler = std::make_unique<commandhandler>(m_bot.get (), false );
4354 m_bot->cache_policy = { cache_policy_setting_t ::cp_lazy, cache_policy_setting_t ::cp_lazy, cache_policy_setting_t ::cp_aggressive };
44- std::thread (&Client::run, this , token). detach ( );
55+ m_start_thread = std::thread (&Client::run, this , token);
4556 }catch (std::exception& e){
4657 Handler::log_dpp (" DPP thew an exception: " + (std::string)e.what (), " connect()" , ll_critical);
4758 }
@@ -50,15 +61,22 @@ void Client::connect(){
5061
5162void Client::disconnect (){
5263 std::lock_guard<std::mutex> lg (m_client_lock);
53- if (m_bot != nullptr && m_is_connected.load (std::memory_order_relaxed)){
54- try {
55- m_bot->shutdown ();
56- m_handler.reset ();
57- m_bot.reset ();
58- m_is_connected.store (false , std::memory_order_release);
59- }catch (std::exception& e){
60- Handler::log_dpp (" DPP thew an exception: " + (std::string)e.what (), " disconnect()" , ll_critical);
61- }
64+ // cout << "Client::disconnect()" << endl;
65+
66+ if (m_start_thread.joinable ()){
67+ m_start_thread.join ();
68+ }
69+
70+ if (m_bot == nullptr || !m_is_connected.load (std::memory_order_relaxed)){
71+ return ;
72+ }
73+ try {
74+ m_bot->shutdown ();
75+ m_handler.reset ();
76+ m_bot.reset ();
77+ m_is_connected.store (false , std::memory_order_release);
78+ }catch (std::exception& e){
79+ Handler::log_dpp (" DPP thew an exception: " + (std::string)e.what (), " disconnect()" , ll_critical);
6280 }
6381}
6482
@@ -149,6 +167,8 @@ void Client::run(const std::string& token){
149167 m_bot.reset ();
150168 m_is_connected.store (false , std::memory_order_release);
151169 }
170+
171+ // cout << "Client::run() - ending" << endl;
152172}
153173
154174
0 commit comments