Skip to content

Commit

Permalink
change voice gateway for v8 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Aug 29, 2024
1 parent 9693b40 commit 3427596
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
5 changes: 2 additions & 3 deletions Discord.C++/Discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -467,8 +467,7 @@ void DiscordCPP::Discord::handle_raw_event(const std::string &event_name,
}
}

voice_state->endpoint = "wss://" + data.at("endpoint").get<std::string>();
voice_state->endpoint += "?v=4";
voice_state->endpoint = "wss://" + data.at("endpoint").get<std::string>() + VOICE_GATEWAY_QUERY;
voice_state->voice_token = data.at("token").get<std::string>();
} else if (event_name == "INTERACTION_CREATE") {
try {
Expand Down
3 changes: 2 additions & 1 deletion Discord.C++/Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ DiscordCPP::Gateway::Gateway(std::string token, const std::shared_ptr<Threadpool
_heartbeat_interval(0),
_keepalive(true),
_reconnect_timeout(0),
_connected(false) {
_connected(false),
_sequence_number(0) {
_log = Logger("Discord.Gateway");

ssl_context.set_verify_mode(ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert);
Expand Down
2 changes: 2 additions & 0 deletions Discord.C++/Gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Gateway {
std::thread _heartbeat_task;
/// indicator if Gateway is connected
bool _connected;
/// current sequence number
unsigned int _sequence_number;
/// logging instance
Logger _log;
/// the message handler set by using set_message_handler
Expand Down
5 changes: 2 additions & 3 deletions Discord.C++/MainGateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ void DiscordCPP::MainGateway::identify() {
{"intents", _intents.getIntents()}, //
{"shard", {_shard_id, _num_shards}}, //
{"large_threshold", 250} //
} //
} //
} //
} //
};

#ifdef _WIN32
Expand Down Expand Up @@ -196,7 +196,6 @@ DiscordCPP::MainGateway::MainGateway(std::string token,
: Gateway::Gateway(std::move(token),
std::make_shared<Threadpool>(std::thread::hardware_concurrency() / num_shards)),
_invalid_session(false),
_sequence_number(0),
_shard_id(shard_id),
_num_shards(num_shards),
_intents(intents) {
Expand Down
2 changes: 0 additions & 2 deletions Discord.C++/MainGateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions Discord.C++/VoiceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ void DiscordCPP::VoiceClient::select_protocol() {
{"address", _my_ip}, //
{"port", _my_port}, //
{"mode", "xsalsa20_poly1305"} //
}} //
}} //
}} //
}} //
};

_voice_ws->send(payload).get();
Expand All @@ -139,8 +139,8 @@ void DiscordCPP::VoiceClient::speak(bool speak) {
{"speaking", speak}, //
{"delay", 0}, //
{"ssrc", _ssrc} //
} //
} //
} //
} //
};

_voice_ws->send(payload).get();
Expand All @@ -154,15 +154,19 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr<MainGateway> 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<Threadpool>(4);

_log = Logger("Discord.VoiceClient");
_log.info("connecting to endpoint " + _endpoint);

_voice_ws = std::make_unique<VoiceGateway>(voice_token, session_id, _guild_id, user_id, threadpool);
_voice_ws = std::make_unique<VoiceGateway>(_voice_token, _session_id, _guild_id, _user_id, threadpool);

_voice_ws->set_message_handler([this](json data) {
switch (data["op"].get<int>()) {
Expand Down Expand Up @@ -191,7 +195,7 @@ DiscordCPP::VoiceClient::VoiceClient(std::shared_ptr<MainGateway> main_ws,
}
});

_voice_ws->connect(endpoint);
_voice_ws->connect(_endpoint);

while (_ready == false) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
Expand Down Expand Up @@ -220,7 +224,7 @@ DiscordCPP::SharedFuture<void> DiscordCPP::VoiceClient::disconnect() {
{"channel_id", nullptr}, //
{"self_mute", true}, //
{"self_deaf", true} //
}} //
}} //
};
std::shared_ptr<DiscordCPP::Future<void>> main_future = _main_ws->send(payload).get_future();
std::shared_ptr<DiscordCPP::Future<void>> voice_future = _voice_ws->close().get_future();
Expand Down
24 changes: 18 additions & 6 deletions Discord.C++/VoiceGateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
#include <utility>

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) {
Expand All @@ -40,6 +48,10 @@ void DiscordCPP::VoiceGateway::on_websocket_incoming_message(const std::string&
json payload = json::parse(message);
int op = payload["op"].get<int>();

if ((payload.count("seq") > 0) && payload["seq"].is_number_integer()) {
_sequence_number = payload["seq"].get<int>();
}

switch (op) {
case 2:
_reconnect_timeout = 0;
Expand Down
1 change: 1 addition & 0 deletions Discord.C++/static.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3427596

Please sign in to comment.