summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-02 01:18:23 +0500
committerilotterytea <iltsu@alright.party>2024-05-02 01:18:23 +0500
commit673f55a39e5e7626ca29becf60ba161f264a8ad1 (patch)
tree9f7d04f54507f872edb5db024daae3d4d415ea98 /src
parent5776100b8a2f5fe568fbd19f8117bfce2cd63a5b (diff)
upd: use client_id and token to get bot creds
Diffstat (limited to 'src')
-rw-r--r--src/irc/client.cpp31
-rw-r--r--src/irc/client.hpp10
-rw-r--r--src/main.cpp2
3 files changed, 33 insertions, 10 deletions
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 <string>
#include <vector>
+#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<std::string>());
+ 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;
diff --git a/src/main.cpp b/src/main.cpp
index ff3e78b..2eb0a96 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
return -1;
}
- bot::irc::Client client(cfg.bot_username, cfg.bot_password);
+ bot::irc::Client client(cfg.bot_client_id, cfg.bot_password);
bot::command::CommandLoader command_loader;
bot::loc::Localization localization("localization");