From 673f55a39e5e7626ca29becf60ba161f264a8ad1 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Thu, 2 May 2024 01:18:23 +0500 Subject: upd: use client_id and token to get bot creds --- src/irc/client.cpp | 31 +++++++++++++++++++++++++------ src/irc/client.hpp | 10 +++++++--- 2 files changed, 32 insertions(+), 9 deletions(-) (limited to 'src/irc') diff --git a/src/irc/client.cpp b/src/irc/client.cpp index d6bf60f..e318bf0 100644 --- a/src/irc/client.cpp +++ b/src/irc/client.cpp @@ -9,18 +9,37 @@ #include #include +#include "cpr/api.h" +#include "cpr/cprtypes.h" +#include "cpr/response.h" #include "message.hpp" +#include "nlohmann/json.hpp" using namespace bot::irc; -Client::Client(std::string username, std::string password) { - this->username = username; - this->password = password; +Client::Client(std::string client_id, std::string token) { + this->client_id = client_id; + this->token = token; this->host = "wss://irc-ws.chat.twitch.tv"; this->port = "443"; this->websocket.setUrl(this->host + ":" + this->port); + + // getting token owner + cpr::Response response = cpr::Get( + cpr::Url{"https://api.twitch.tv/helix/users"}, cpr::Bearer{this->token}, + cpr::Header{{"Client-Id", this->client_id}}); + + if (response.status_code != 200) { + std::cout << "* Failed to get bot username from Twitch API!\n"; + } else { + nlohmann::json j = nlohmann::json::parse(response.text); + + auto d = j["data"][0]; + this->id = std::stoi(d["id"].get()); + this->username = d["login"]; + } } void Client::run() { @@ -117,14 +136,14 @@ void Client::raw(const std::string &raw_message) { } void Client::authorize() { - if (this->username.empty() || this->password.empty()) { - std::cout << "Bot username and password must be set!\n"; + if (this->username.empty() || this->token.empty()) { + std::cout << "Bot username and token must be set!\n"; return; } std::cout << "Authorizing on Twitch IRC servers...\n"; - this->raw("PASS oauth:" + this->password); + this->raw("PASS oauth:" + this->token); this->raw("NICK " + this->username); this->raw("CAP REQ :twitch.tv/membership"); this->raw("CAP REQ :twitch.tv/commands"); diff --git a/src/irc/client.hpp b/src/irc/client.hpp index 36d7382..cff867f 100644 --- a/src/irc/client.hpp +++ b/src/irc/client.hpp @@ -11,7 +11,7 @@ namespace bot { namespace irc { class Client { public: - Client(std::string username, std::string password); + Client(std::string client_id, std::string token); ~Client() = default; void run(); @@ -31,15 +31,19 @@ namespace bot { } } + const std::string &get_bot_username() const { return this->username; }; + const int &get_bot_id() const { return this->id; } + private: void authorize(); - std::string username; - std::string password; + std::string client_id, token, username; std::string host; std::string port; + int id; + ix::WebSocket websocket; bool is_connected = false; -- cgit v1.2.3