diff --git a/.travis.xml b/.travis.xml deleted file mode 100644 index 61ee5ea..0000000 --- a/.travis.xml +++ /dev/null @@ -1,24 +0,0 @@ -os: -- linux -- osx - -sudo: required - -language: c++ - -before_install: -- echo $LANG -- echo $LC_ALL -- if [ $TRAVIS_OS_NAME == linux ]; then ./install-all-deps.sh; fi - -compiler: - - clang - - gcc - - g++-4.8 - - g++-7.3 - -script: - - make - -after_script: - - cat log_output \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index f3d8af7..a666df7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,54 @@ "utility": "cpp", "xmemory0": "cpp", "xtr1common": "cpp", - "xutility": "cpp" - } + "xutility": "cpp", + "xstring": "cpp", + "algorithm": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "forward_list": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "map": "cpp", + "new": "cpp", + "numeric": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "valarray": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xfunctional": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xtree": "cpp" + }, + "editor.tabSize": 4 } \ No newline at end of file diff --git a/include/dpp/discord_client.hpp b/include/dpp/discord_client.hpp index 0ab14b7..3066971 100644 --- a/include/dpp/discord_client.hpp +++ b/include/dpp/discord_client.hpp @@ -5,9 +5,19 @@ using namespace std; +class RichEmbed { + public: + string title; + string description; + string url; + int color; +}; + class DiscordClient { public: bool login(string type, string token); + bool SendMessage(string channel, string content); + bool SendEmbed(string channel, RichEmbed embed); private: dpp::discord_http _dh; -}; \ No newline at end of file +}; diff --git a/include/dpp/discord_http.hpp b/include/dpp/discord_http.hpp index f7ef2b7..f14c11a 100644 --- a/include/dpp/discord_http.hpp +++ b/include/dpp/discord_http.hpp @@ -9,14 +9,14 @@ // This is mostly to be used in the future base_client class namespace dpp { - class discord_http { - private: - urler m_u; - public: - discord_http(); - void add_token(std::string t_type, std::string t_token); - nlohmann::json post(std::string t_endpoint, nlohmann::json t_payload); - nlohmann::json get(std::string t_endpoint, std::string t_url_encoded); - // Other methods are to be added - }; + class discord_http { + private: + urler m_u; + public: + discord_http(); + void add_token(std::string t_type, std::string t_token); + nlohmann::json post(std::string t_endpoint, nlohmann::json t_payload); + nlohmann::json get(std::string t_endpoint, std::string t_url_encoded); + // Other methods are to be added + }; } \ No newline at end of file diff --git a/include/dpp/http_res.hpp b/include/dpp/http_res.hpp index 7403ad8..ab883e1 100644 --- a/include/dpp/http_res.hpp +++ b/include/dpp/http_res.hpp @@ -2,8 +2,8 @@ #include namespace dpp { - struct http_res { - std::string body; - std::string base; - }; + struct http_res { + std::string body; + std::string base; + }; } \ No newline at end of file diff --git a/include/dpp/urler.hpp b/include/dpp/urler.hpp index 39ddeb7..6d808f2 100644 --- a/include/dpp/urler.hpp +++ b/include/dpp/urler.hpp @@ -19,22 +19,22 @@ using json = nlohmann::json; namespace dpp { - class urler { - private: - std::string m_method; - unsigned int m_http_ver; - std::string m_path; - std::string m_body; - json m_headers; - - public: - urler(); - void set_method(std::string t_method); - void set_http_ver(unsigned int t_http_ver); - void set_path(std::string t_path); - void set_body(std::string t_body); - void set_header(std::string t_header, std::string t_value); - void delete_header(std::string t_header); - http_res perform(std::string t_url); - }; + class urler { + private: + std::string m_method; + unsigned int m_http_ver; + std::string m_path; + std::string m_body; + json m_headers; + + public: + urler(); + void set_method(std::string t_method); + void set_http_ver(unsigned int t_http_ver); + void set_path(std::string t_path); + void set_body(std::string t_body); + void set_header(std::string t_header, std::string t_value); + void delete_header(std::string t_header); + http_res perform(std::string t_url); + }; } diff --git a/src/comp.sh b/src/comp.sh deleted file mode 100644 index 0bbfcc6..0000000 --- a/src/comp.sh +++ /dev/null @@ -1,4 +0,0 @@ -g++ -c main.cpp -o ../build/main.o -I../include --std=c++11 $(pkg-config --cflags openssl) -g++ -c libs/urler.cpp -o ../build/urler.o -I../include --std=c++11 -Werror $(pkg-config --cflags openssl) -g++ -c libs/discord_http.cpp -o ../build/discord_http.o -I../include --std=c++11 -Werror $(pkg-config --cflags openssl) -g++ ../build/main.o ../build/urler.o ../build/discord_http.o -o ../build/discordpp -Wl,-Bstatic -lboost_thread -lboost_system -Wl,-Bdynamic -lpthread $(pkg-config --libs openssl) diff --git a/src/libs/discord_client.cpp b/src/libs/discord_client.cpp index f2ad778..d39a6ca 100644 --- a/src/libs/discord_client.cpp +++ b/src/libs/discord_client.cpp @@ -7,4 +7,26 @@ bool DiscordClient::login(string type, string token) { return false; } _dh.add_token(type, token); + return true; +} + +bool DiscordClient::SendMessage(string channel, string content) { + nlohmann::json message; + message["content"] = content; + nlohmann::json response = _dh.post("/channels/" + channel + "/messages", message); + cout << response.dump(); +} + +bool DiscordClient::SendEmbed(string channel, RichEmbed embed) { + nlohmann::json rembed; + rembed["title"] = embed.title; + rembed["description"] = embed.title; + rembed["url"] = embed.title; + rembed["title"] = embed.title; + rembed["type"] = "rich"; + nlohmann::json message; + message["embed"] = rembed.dump(); + nlohmann::json response = _dh.post("/channels/" + channel + "/messages", message); + cout << "response:" << endl; + cout << response.dump(); } \ No newline at end of file diff --git a/src/libs/discord_http.cpp b/src/libs/discord_http.cpp index da389e2..706f8be 100644 --- a/src/libs/discord_http.cpp +++ b/src/libs/discord_http.cpp @@ -4,31 +4,31 @@ const std::string discord_url = "discordapp.com"; const std::string discord_api_prefix = "/api"; dpp::discord_http::discord_http() { - m_u.set_http_ver(11); - m_u.set_method("GET"); - m_u.set_header("Host", discord_url); - m_u.set_header("User-Agent", "DiscordBot"); + m_u.set_http_ver(11); + m_u.set_method("GET"); + m_u.set_header("Host", discord_url); + m_u.set_header("User-Agent", "DiscordBot"); } nlohmann::json dpp::discord_http::post(std::string t_endpoint, nlohmann::json t_payload) { - m_u.set_method("POST"); - m_u.set_path(discord_api_prefix + t_endpoint); - m_u.set_body(t_payload.dump()); - m_u.set_header("Content-Type", "application/json"); - std::string res = m_u.perform(discord_url).body; - return nlohmann::json::parse(res); + m_u.set_method("POST"); + m_u.set_path(discord_api_prefix + t_endpoint); + m_u.set_body(t_payload.dump()); + m_u.set_header("Content-Type", "application/json"); + std::string res = m_u.perform(discord_url).body; + return nlohmann::json::parse(res); } nlohmann::json dpp::discord_http::get(std::string t_endpoint, std::string t_url_encoded) { - m_u.set_method("GET"); - m_u.set_path(discord_api_prefix + t_endpoint); - m_u.set_body(t_url_encoded); - m_u.set_header("Content-Type", "application/x-www-form-urlencoded"); - std::string res = m_u.perform(discord_url).body; - return nlohmann::json::parse(res); + m_u.set_method("GET"); + m_u.set_path(discord_api_prefix + t_endpoint); + m_u.set_body(t_url_encoded); + m_u.set_header("Content-Type", "application/x-www-form-urlencoded"); + std::string res = m_u.perform(discord_url).body; + return nlohmann::json::parse(res); } // The next method will be for the Base Client when he logs void dpp::discord_http::add_token(std::string t_type, std::string t_token) { - m_u.set_header("Authorization", t_type + " " + t_token); + m_u.set_header("Authorization", t_type + " " + t_token); } \ No newline at end of file diff --git a/src/libs/urler.cpp b/src/libs/urler.cpp index 36bbdaa..6ab8bda 100644 --- a/src/libs/urler.cpp +++ b/src/libs/urler.cpp @@ -2,10 +2,10 @@ #include // Set common default values in the constructor dpp::urler::urler() { - m_method = "GET"; - m_http_ver = 11; - m_path = "/"; - m_body = ""; + m_method = "GET"; + m_http_ver = 11; + m_path = "/"; + m_body = ""; } void dpp::urler::set_method(std::string t_method) { m_method = t_method; } @@ -14,76 +14,72 @@ void dpp::urler::set_path(std::string t_path) { m_path = t_path; } void dpp::urler::set_body(std::string t_body) { m_body = t_body; } void dpp::urler::set_header(std::string t_header, std::string t_value) { - m_headers[t_header] = t_value; + m_headers[t_header] = t_value; } void dpp::urler::delete_header(std::string t_header) { - m_headers[t_header] = "-no-"; - // Check how to delete properties of a json + m_headers[t_header] = "-no-"; + // Check how to delete properties of a json } dpp::http_res dpp::urler::perform(std::string t_url) { - using json = nlohmann::json; - using tcp = boost::asio::ip::tcp; - namespace ssl = boost::asio::ssl; - namespace http = boost::beast::http; - boost::asio::io_context ioc; + using json = nlohmann::json; + using tcp = boost::asio::ip::tcp; + namespace ssl = boost::asio::ssl; + namespace http = boost::beast::http; + boost::asio::io_context ioc; - ssl::context ctx{ssl::context::sslv23_client}; + ssl::context ctx{ssl::context::sslv23_client}; - load_root_certificates(ctx); // SSL shit + load_root_certificates(ctx); // SSL shit - tcp::resolver resolver{ioc}; - ssl::stream stream{ioc, ctx}; + tcp::resolver resolver{ioc}; + ssl::stream stream{ioc, ctx}; - // Another SSL shit - if(! SSL_set_tlsext_host_name(stream.native_handle(), t_url.c_str())) { - boost::system::error_code ec{static_cast(::ERR_get_error()), boost::asio::error::get_ssl_category()}; - } + // Another SSL shit + if(! SSL_set_tlsext_host_name(stream.native_handle(), t_url.c_str())) { + boost::system::error_code ec{static_cast(::ERR_get_error()), boost::asio::error::get_ssl_category()}; + } + + auto const results = resolver.resolve(t_url, "443"); // Resolving + + boost::asio::connect(stream.next_layer(), results.begin(), results.end()); // Connecting + + stream.handshake(ssl::stream_base::client); // SSL shit again + + http::request req{http::string_to_verb(m_method), m_path.c_str(), m_http_ver}; + for (json::iterator it = m_headers.begin(); it != m_headers.end(); it++) { + if (it.value() != "-no-") { + // Setting of the headers + req.set(it.key(), it.value().get()); + } + } + + if (m_body.size() > 0) { + req.body() = m_body; + req.prepare_payload(); + } + + std::cout << req << std::endl; + + http::write(stream, req); // SENDING + + boost::beast::flat_buffer buffer; + + http::response res; - auto const results = resolver.resolve(t_url, "443"); // Resolving + http::read(stream, buffer, res); - boost::asio::connect(stream.next_layer(), results.begin(), results.end()); // Connecting + http_res ret_res; + std::ostringstream os; + os << boost::beast::buffers(res.body().data()); + ret_res.body = os.str(); - stream.handshake(ssl::stream_base::client); // SSL shit again + ret_res.base = "lol"; // res.base(); - http::request req{http::string_to_verb(m_method), m_path.c_str(), m_http_ver}; - for (json::iterator it = m_headers.begin(); it != m_headers.end(); it++) { - if (it.value() != "-no-") { - // Setting of the headers - req.set(it.key(), it.value().get()); + boost::system::error_code ec; + stream.shutdown(ec); + if(ec == boost::asio::error::eof) { + ec.assign(0, ec.category()); } - } - - if (m_body.size() > 0) { - req.body() = m_body; - req.prepare_payload(); - } - - std::cout << req << std::endl; - - http::write(stream, req); // SENDING - - boost::beast::flat_buffer buffer; - - http::response res; - - http::read(stream, buffer, res); - - http_res ret_res; - std::ostringstream os; - os << boost::beast::buffers(res.body().data()); - ret_res.body = os.str(); - - ret_res.base = "lol"; // res.base(); - - boost::system::error_code ec; - stream.shutdown(ec); - if(ec == boost::asio::error::eof) { - ec.assign(0, ec.category()); - } - return ret_res; - //std::string str_res; - //std::ifstream(&res) >> str_res - // return res; - // If we get here then the connection is closed gracefully + return ret_res; } diff --git a/src/main.cpp b/src/main.cpp index 0efe0d8..cb85193 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,11 +6,13 @@ int main() { DiscordClient client; - client.login("Bot", ""); - // dh.add_token("Bot", ""); - // nlohmann::json message; - // message["content"] = "Message sent from C++"; - // nlohmann::json j = dh.post("/channels/418390319659483137/messages", message); - // std::cout << j.dump(); - return 0; + client.login("Bot", "NDQwOTk4MDYwNjQ3MjUxOTY4.Dcp-dg.00z0ExWE-Jmxk9nMzP2uRR1iJ9A"); + client.SendMessage("370015453176922122", "Yay, DiscordClient class works !"); + RichEmbed embed; + embed.title = "Test title"; + embed.description = "Test description"; + embed.url = "https://www.google.com/"; + client.SendEmbed("370015453176922122", embed); + getchar(); + return 0; } \ No newline at end of file