send chat messages

This commit is contained in:
ouwou
2020-08-21 22:25:23 -04:00
parent 6f11aa4dae
commit 6b72931ba7
8 changed files with 36 additions and 8 deletions

View File

@@ -117,6 +117,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;USE_LOCAL_PROXY;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;USE_LOCAL_PROXY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@@ -132,6 +133,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>

View File

@@ -152,6 +152,10 @@ void Abaddon::ActionListChannelItemClick(Snowflake id) {
} }
} }
void Abaddon::ActionChatInputSubmit(std::string msg, Snowflake channel) {
m_discord.SendChatMessage(msg, channel);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
Gtk::Main::init_gtkmm_internals(); // why??? Gtk::Main::init_gtkmm_internals(); // why???
Abaddon abaddon; Abaddon abaddon;

View File

@@ -24,6 +24,7 @@ public:
void ActionMoveGuildUp(Snowflake id); void ActionMoveGuildUp(Snowflake id);
void ActionMoveGuildDown(Snowflake id); void ActionMoveGuildDown(Snowflake id);
void ActionListChannelItemClick(Snowflake id); void ActionListChannelItemClick(Snowflake id);
void ActionChatInputSubmit(std::string msg, Snowflake channel);
std::string GetDiscordToken() const; std::string GetDiscordToken() const;
bool IsDiscordActive() const; bool IsDiscordActive() const;

View File

@@ -136,9 +136,10 @@ bool ChatWindow::on_key_press_event(GdkEventKey *e) {
return false; return false;
auto text = buffer->get_text(); auto text = buffer->get_text();
buffer->set_text(""); buffer->set_text("");
m_abaddon->ActionChatInputSubmit(text, m_active_channel);
return true; return true;
} }

View File

@@ -117,6 +117,14 @@ const MessageData *DiscordClient::GetMessage(Snowflake id) const {
return &m_messages.at(id); return &m_messages.at(id);
} }
void DiscordClient::SendChatMessage(std::string content, Snowflake channel) {
// @([^@#]{1,32})#(\\d{4})
CreateMessageObject obj;
obj.Content = content;
nlohmann::json j = obj;
m_http.MakePOST("/channels/" + std::to_string(channel) + "/messages", j.dump(), [](auto) {});
}
void DiscordClient::UpdateToken(std::string token) { void DiscordClient::UpdateToken(std::string token) {
m_token = token; m_token = token;
m_http.SetAuth(token); m_http.SetAuth(token);
@@ -173,7 +181,7 @@ void DiscordClient::HandleGatewayReady(const GatewayMessage &msg) {
m_user_settings = data.UserSettings; m_user_settings = data.UserSettings;
} }
void DiscordClient::HandleGatewayMessageCreate(const GatewayMessage& msg) { void DiscordClient::HandleGatewayMessageCreate(const GatewayMessage &msg) {
MessageData data = msg.Data; MessageData data = msg.Data;
StoreMessage(data.ID, data); StoreMessage(data.ID, data);
m_abaddon->DiscordNotifyMessageCreate(data.ID); m_abaddon->DiscordNotifyMessageCreate(data.ID);
@@ -442,6 +450,10 @@ void to_json(nlohmann::json &j, const HeartbeatMessage &m) {
j["d"] = m.Sequence; j["d"] = m.Sequence;
} }
void to_json(nlohmann::json &j, const CreateMessageObject &m) {
j["content"] = m.Content;
}
Snowflake::Snowflake() Snowflake::Snowflake()
: m_num(Invalid) {} : m_num(Invalid) {}

View File

@@ -9,7 +9,7 @@
// bruh // bruh
#ifdef GetMessage #ifdef GetMessage
#undef GetMessage #undef GetMessage
#endif #endif
struct Snowflake { struct Snowflake {
@@ -341,6 +341,12 @@ struct HeartbeatMessage : GatewayMessage {
friend void to_json(nlohmann::json &j, const HeartbeatMessage &m); friend void to_json(nlohmann::json &j, const HeartbeatMessage &m);
}; };
struct CreateMessageObject {
std::string Content;
friend void to_json(nlohmann::json &j, const CreateMessageObject &m);
};
// https://stackoverflow.com/questions/29775153/stopping-long-sleep-threads/29775639#29775639 // https://stackoverflow.com/questions/29775153/stopping-long-sleep-threads/29775639#29775639
class HeartbeatWaiter { class HeartbeatWaiter {
public: public:
@@ -389,6 +395,8 @@ public:
void FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<MessageData> &)> cb); void FetchMessagesInChannel(Snowflake id, std::function<void(const std::vector<MessageData> &)> cb);
const MessageData *GetMessage(Snowflake id) const; const MessageData *GetMessage(Snowflake id) const;
void SendChatMessage(std::string content, Snowflake channel);
void UpdateToken(std::string token); void UpdateToken(std::string token);
private: private:

View File

@@ -37,20 +37,20 @@ void HTTPClient::MakePOST(std::string path, std::string payload, std::function<v
}; };
auto body = cpr::Body { payload }; auto body = cpr::Body { payload };
#ifdef USE_LOCAL_PROXY #ifdef USE_LOCAL_PROXY
m_futures.push_back(cpr::GetCallback( m_futures.push_back(cpr::PostCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body, url, headers, body,
cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } }, cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
cpr::VerifySsl { false })); cpr::VerifySsl { false }));
#else #else
m_futures.push_back(cpr::PatchCallback( m_futures.push_back(cpr::PostCallback(
std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb), std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
url, headers, body)); url, headers, body));
#endif #endif
} }
void HTTPClient::MakeGET(std::string path, std::function<void(cpr::Response r)> cb) { void HTTPClient::MakeGET(std::string path, std::function<void(cpr::Response r)> cb) {
printf("POST %s\n", path.c_str()); printf("GET %s\n", path.c_str());
auto url = cpr::Url { m_api_base + path }; auto url = cpr::Url { m_api_base + path };
auto headers = cpr::Header { auto headers = cpr::Header {
{ "Authorization", m_authorization }, { "Authorization", m_authorization },

View File

@@ -5,8 +5,8 @@ MainWindow::MainWindow()
: m_main_box(Gtk::ORIENTATION_VERTICAL) : m_main_box(Gtk::ORIENTATION_VERTICAL)
, m_content_box(Gtk::ORIENTATION_HORIZONTAL) , m_content_box(Gtk::ORIENTATION_HORIZONTAL)
, m_chan_chat_paned(Gtk::ORIENTATION_HORIZONTAL) { , m_chan_chat_paned(Gtk::ORIENTATION_HORIZONTAL) {
set_default_size(800, 600); set_default_size(1200, 800);
m_menu_discord.set_label("Discord"); m_menu_discord.set_label("Discord");
m_menu_discord.set_submenu(m_menu_discord_sub); m_menu_discord.set_submenu(m_menu_discord_sub);
m_menu_discord_connect.set_label("Connect"); m_menu_discord_connect.set_label("Connect");