diff --git a/Discord.C++/Discord.cpp b/Discord.C++/Discord.cpp index be37fbc..47e0247 100644 --- a/Discord.C++/Discord.cpp +++ b/Discord.C++/Discord.cpp @@ -129,7 +129,7 @@ void DiscordCPP::Discord::update_presence(const std::string &status, Activity ac {"since", {}}, // {"status", status}, // {"afk", afk} // - }} // + }} // }; if (activity.get_type() == Activity::NoActivity) @@ -467,8 +467,7 @@ void DiscordCPP::Discord::handle_raw_event(const std::string &event_name, } } - voice_state->endpoint = "wss://" + data.at("endpoint").get(); - voice_state->endpoint += "?v=4"; + voice_state->endpoint = "wss://" + data.at("endpoint").get() + VOICE_GATEWAY_QUERY; voice_state->voice_token = data.at("token").get(); } else if (event_name == "INTERACTION_CREATE") { try { diff --git a/Discord.C++/Gateway.cpp b/Discord.C++/Gateway.cpp index f2b9e85..b475dc5 100644 --- a/Discord.C++/Gateway.cpp +++ b/Discord.C++/Gateway.cpp @@ -93,7 +93,8 @@ DiscordCPP::Gateway::Gateway(std::string token, const std::shared_ptr(std::thread::hardware_concurrency() / num_shards)), _invalid_session(false), - _sequence_number(0), _shard_id(shard_id), _num_shards(num_shards), _intents(intents) { diff --git a/Discord.C++/MainGateway.h b/Discord.C++/MainGateway.h index 0373e86..9bc28a7 100644 --- a/Discord.C++/MainGateway.h +++ b/Discord.C++/MainGateway.h @@ -16,8 +16,6 @@ class MainGateway : public Gateway { std::string _session_id; /// indicator if we have a invalid session bool _invalid_session; - /// current sequence number - unsigned int _sequence_number; /// shard id unsigned int _shard_id; /// total number of shards diff --git a/Discord.C++/VoiceClient.cpp b/Discord.C++/VoiceClient.cpp index 3328e78..5c2f863 100644 --- a/Discord.C++/VoiceClient.cpp +++ b/Discord.C++/VoiceClient.cpp @@ -116,8 +116,8 @@ void DiscordCPP::VoiceClient::select_protocol() { {"address", _my_ip}, // {"port", _my_port}, // {"mode", "xsalsa20_poly1305"} // - }} // - }} // + }} // + }} // }; _voice_ws->send(payload).get(); @@ -139,8 +139,8 @@ void DiscordCPP::VoiceClient::speak(bool speak) { {"speaking", speak}, // {"delay", 0}, // {"ssrc", _ssrc} // - } // - } // + } // + } // }; _voice_ws->send(payload).get(); @@ -154,15 +154,19 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr main_ws, std::string guild_id, std::string channel_id, const std::string& user_id) - : _guild_id(std::move(guild_id)), + : _voice_token(voice_token), + _endpoint(endpoint), + _session_id(session_id), + _guild_id(std::move(guild_id)), _channel_id(std::move(channel_id)), + _user_id(user_id), _main_ws(std::move(main_ws)) { threadpool = std::make_shared(4); _log = Logger("Discord.VoiceClient"); _log.info("connecting to endpoint " + _endpoint); - _voice_ws = std::make_unique(voice_token, session_id, _guild_id, user_id, threadpool); + _voice_ws = std::make_unique(_voice_token, _session_id, _guild_id, _user_id, threadpool); _voice_ws->set_message_handler([this](json data) { switch (data["op"].get()) { @@ -191,7 +195,7 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr main_ws, } }); - _voice_ws->connect(endpoint); + _voice_ws->connect(_endpoint); while (_ready == false) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); @@ -220,7 +224,7 @@ DiscordCPP::SharedFuture DiscordCPP::VoiceClient::disconnect() { {"channel_id", nullptr}, // {"self_mute", true}, // {"self_deaf", true} // - }} // + }} // }; std::shared_ptr> main_future = _main_ws->send(payload).get_future(); std::shared_ptr> voice_future = _voice_ws->close().get_future(); diff --git a/Discord.C++/VoiceGateway.cpp b/Discord.C++/VoiceGateway.cpp index dcf50a1..cff02fe 100644 --- a/Discord.C++/VoiceGateway.cpp +++ b/Discord.C++/VoiceGateway.cpp @@ -3,18 +3,26 @@ #include json DiscordCPP::VoiceGateway::get_heartbeat_payload() { - return {{"op", 3}, {"d", std::to_string(time(nullptr))}}; + return { + {"op", 3}, // + {"d", { + // + {"t", std::to_string(time(nullptr))}, // + {"seq_ack", ((_sequence_number == 0) ? "null" : std::to_string(_sequence_number))} // + }} // + }; } void DiscordCPP::VoiceGateway::identify() { json out_payload = { { "d", { - {"server_id", _guild_id}, // - {"session_id", _session_id}, // - {"token", _token} // - } // - } // + {"server_id", _guild_id}, // + {"session_id", _session_id}, // + {"token", _token}, // + {"seq_ack", ((_sequence_number == 0) ? "null" : std::to_string(_sequence_number))} // + } // + } // }; if (_resume == true) { @@ -40,6 +48,10 @@ void DiscordCPP::VoiceGateway::on_websocket_incoming_message(const std::string& json payload = json::parse(message); int op = payload["op"].get(); + if ((payload.count("seq") > 0) && payload["seq"].is_number_integer()) { + _sequence_number = payload["seq"].get(); + } + switch (op) { case 2: _reconnect_timeout = 0; diff --git a/Discord.C++/static.h b/Discord.C++/static.h index 3834de9..afef299 100644 --- a/Discord.C++/static.h +++ b/Discord.C++/static.h @@ -4,6 +4,7 @@ #define GATEWAY_URL "wss://gateway.discord.gg?v=10&encoding=json&compress=zlib-stream" #define API_PREFIX "/api/v10" #define DISCORD_HOST "discord.com" +#define VOICE_GATEWAY_QUERY "?v=8" #ifdef _WIN32 #define DLL_EXPORT __declspec(dllexport)