diff --git a/include/neko/discord/api/shard.hpp b/include/neko/discord/api/shard.hpp index f215996..ba56551 100644 --- a/include/neko/discord/api/shard.hpp +++ b/include/neko/discord/api/shard.hpp @@ -41,7 +41,7 @@ public: enum class State { kReady, // We recieved our READY event - kNearly, // We sent our IDENTITY, waiting for ready + kHello, // We sent our IDENTITY, waiting for ready kConnecting, // Trying to connect to the socket kReconnecting, // Got disconnected, trying again kDisconnected, // We are not connected at all diff --git a/include/neko/discord/client.hpp b/include/neko/discord/client.hpp index 40a7efe..37d872a 100644 --- a/include/neko/discord/client.hpp +++ b/include/neko/discord/client.hpp @@ -22,13 +22,15 @@ #include #include "api/rest.hpp" -#include "api/shard.hpp" #include "presence.hpp" #include "snowflake.hpp" namespace neko::discord { namespace json = rapidjson; +namespace api { +class Shard; +} class Guild; class GuildMember; class Message; @@ -41,6 +43,8 @@ public: BaseClient(); void Login(std::string_view token, int num_shards = 1); void Disconnect(); + void GetToken(); + // Events protected: virtual void onReady(){} @@ -49,29 +53,28 @@ protected: virtual void onChannelCreate(Channel*){} virtual void onGuildUpdate(Guild*) {} virtual void onDisconnect() {} + // Info retrieval public: const User* FetchClientUser(); - //Invite FetchInvite(std::string_view); User* FetchUser(Snowflake id, bool cache = true); // Whether or not to use the cache Guild* FetchGuild(Snowflake id, bool cache = true); Emoji* FetchEmoji(Snowflake id); Channel* FetchChannel(Snowflake id); - //std::vector FetchVoiceRegions(); - //Webhook FetchWebhook(Snowflake id, std::string_view = std::string_view()); - //std::string GenerateInvite() - //void SyncGuilds(); + // This fetch user takes in data and caches it + User* FetchUser(Snowflake, const json::Value&); + User* FetchUser(const json::Value& v); + Guild* FetchGuild(Snowflake, const json::Value&); + Guild* FetchGuild(const json::Value& v); // Internal events private: + // Cache void onReady(const json::Value&); void onGuildCreate(const json::Value&); void onMessageCreate(const json::Value&); void onChannelCreate(const json::Value&); void onGuildUpdate(const json::Value&); - - -// Cache User* user; std::unordered_map channels; std::unordered_map emojis; @@ -79,28 +82,18 @@ private: std::unordered_map presences; std::unordered_map users; - friend Guild; // So they can fill the cache themselves - friend Channel; - friend DMChannel; - friend GroupDMChannel; - friend Emoji; - friend GuildMember; - friend Message; - // This fetch user takes in data and caches it - User* FetchUser(Snowflake, const json::Value&); - User* FetchUser(const json::Value& v); - Guild* FetchGuild(Snowflake, const json::Value&); - Guild* FetchGuild(const json::Value& v); - -// Connection info + // Connection info std::string token; - friend api::RestAPI; api::RestAPI http; - friend api::Shard; std::vector shards; - void EmitEvent(std::string_view event, const json::Value& msg); + void EmitEvent(std::string_view event, const json::Value& data); + // easy access + friend Channel; + friend Guild; + friend Emoji; + friend GuildMember; }; } diff --git a/src/api/shard.cpp b/src/api/shard.cpp index 1394ded..c0d03a6 100644 --- a/src/api/shard.cpp +++ b/src/api/shard.cpp @@ -22,6 +22,8 @@ #include "client.hpp" +#include "api/shard.hpp" + namespace neko::discord::api { using namespace std::string_view_literals; using namespace std::chrono_literals; @@ -156,7 +158,7 @@ void Shard::Send(const json::Value& msg) { void Shard::Heartbeat() { // Check for acknowledge - if (!this->last_heartbeat_ack) { + if (this->state == State::kReady && !this->last_heartbeat_ack) { std::cerr << "Shard: Heartbeat not acknowledged, reconnecting!" << std::endl; this->Disconnect(true); return; @@ -252,13 +254,14 @@ void Shard::RecieveMessage(std::string_view raw) { }, hb_int); } - this->state = State::kNearly; + this->state = State::kHello; if(!this->session_id.empty()) { this->Resume(); } else { this->Identify(); } + this->last_heartbeat_ack = true; this->Heartbeat(); break; } diff --git a/src/client.cpp b/src/client.cpp index 36a1459..a886df4 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -22,6 +22,7 @@ #include #include +#include "api/shard.hpp" #include "guild/guild.hpp" #include "channel.hpp" #include "message.hpp"