forked from rcxpony/sparkle
cmake flags, some fixes in api
This commit is contained in:
parent
d7511fda92
commit
0f05501ea3
@ -31,11 +31,11 @@ endif()
|
|||||||
|
|
||||||
if(CMAKE_BUILD_TYPE)
|
if(CMAKE_BUILD_TYPE)
|
||||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
set(CMAKE_CXX_FLAGS "-march=native -O0 -pipe")
|
set(CMAKE_CXX_FLAGS "-march=native -O0 -pipe -Wall -Werror")
|
||||||
add_definitions(-DDEBUG)
|
add_definitions(-DDEBUG)
|
||||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
|
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
add_definitions(-DRELEASE)
|
add_definitions(-DRELEASE)
|
||||||
set(CMAKE_CXX_FLAGS "-march=native -O2 -pipe")
|
set(CMAKE_CXX_FLAGS "-march=native -O2 -pipe -Wall -Werror")
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
@ -43,15 +43,11 @@ message(STATUS "${CMAKE_BUILD_TYPE}")
|
|||||||
|
|
||||||
add_executable(${PROJECT_NAME} ${SOURCE})
|
add_executable(${PROJECT_NAME} ${SOURCE})
|
||||||
#add_library(sparkles STATIC ${SOURCE})
|
#add_library(sparkles STATIC ${SOURCE})
|
||||||
#add_executable(tests ${TESTS}/tests.cpp)
|
add_executable(tests ${TESTS}/tests.cpp)
|
||||||
|
|
||||||
#enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
add_test(NAME network COMMAND tests)
|
add_test(NAME NetworkManagerTest COMMAND tests)
|
||||||
|
|
||||||
#target_include_directories(sparkles PRIVATE ${LIBS} ${INCLUDE} ${IXWEBSOCKET_INCLUDE_DIR} ${CURL_INCLUDE_DIRS})
|
|
||||||
|
|
||||||
#target_link_libraries(sparkles PRIVATE ${IXWEBSOCKET_LIBRARIES} ${CURL_LIBRARIES})
|
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE
|
target_include_directories(${PROJECT_NAME} PRIVATE
|
||||||
${LIBS}
|
${LIBS}
|
||||||
@ -65,15 +61,15 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
|
|||||||
${CURL_LIBRARIES}
|
${CURL_LIBRARIES}
|
||||||
)
|
)
|
||||||
|
|
||||||
#target_include_directories(tests PRIVATE
|
target_include_directories(tests PRIVATE
|
||||||
# ${LIBS}
|
${LIBS}
|
||||||
# ${INCLUDE}
|
${INCLUDE}
|
||||||
# ${IXWEBSOCKET_INCLUDE_DIR}
|
${IXWEBSOCKET_INCLUDE_DIR}
|
||||||
# ${CURL_INCLUDE_DIRS}
|
${CURL_INCLUDE_DIRS}
|
||||||
#)
|
)
|
||||||
|
|
||||||
#target_link_libraries(tests gtest
|
target_link_libraries(tests gtest
|
||||||
# ${IXWEBSOCKET_LIBRARIES}
|
${IXWEBSOCKET_LIBRARIES}
|
||||||
# ${CURL_LIBRARIES}
|
${CURL_LIBRARIES}
|
||||||
# gtest_main
|
gtest_main
|
||||||
#)
|
)
|
@ -9,6 +9,8 @@ private:
|
|||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
NetworkManager& req;
|
NetworkManager& req;
|
||||||
public:
|
public:
|
||||||
|
const std::string channel_id, global_name, id, content, avatar, guild_id, discriminator, message_id;
|
||||||
|
bool isPinned, isBot;
|
||||||
Author(const nlohmann::json& data) :
|
Author(const nlohmann::json& data) :
|
||||||
data(data),
|
data(data),
|
||||||
d(data["d"]),
|
d(data["d"]),
|
||||||
@ -18,26 +20,12 @@ public:
|
|||||||
global_name(functions::isNull(d["author"]["global_name"])),
|
global_name(functions::isNull(d["author"]["global_name"])),
|
||||||
id(d["author"]["id"]),
|
id(d["author"]["id"]),
|
||||||
content(d["content"]),
|
content(d["content"]),
|
||||||
isBot(d["author"].contains("bot") ? d["author"]["bot"].get<bool>() : false)
|
avatar(data["d"]["author"]["avatar"]),
|
||||||
{
|
guild_id(data["d"]["guild_id"]),
|
||||||
};
|
discriminator(data["d"]["author"]["discriminator"]),
|
||||||
const std::string channel_id, global_name, id, content;
|
message_id(data["d"]["id"]),
|
||||||
bool isBot;
|
isPinned(data["d"]["pinned"]),
|
||||||
std::string discriminator() const {
|
isBot(d["author"].contains("bot") ? d["author"]["bot"].get<bool>() : false) {};
|
||||||
return data["d"]["author"]["discriminator"];
|
|
||||||
}
|
|
||||||
std::string guild_id() {
|
|
||||||
return data["d"]["guild_id"];
|
|
||||||
}
|
|
||||||
std::string msg_id() {
|
|
||||||
return data["d"]["id"];
|
|
||||||
}
|
|
||||||
bool isPinned() {
|
|
||||||
return data["d"]["pinned"];
|
|
||||||
}
|
|
||||||
std::string avatar() {
|
|
||||||
return data["d"]["author"]["avatar"];
|
|
||||||
}
|
|
||||||
std::string send(const nlohmann::json& msg) {
|
std::string send(const nlohmann::json& msg) {
|
||||||
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump());
|
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + data["d"]["channel_id"].get<std::string>() + "/messages", msg.dump());
|
||||||
}
|
}
|
||||||
|
@ -18,35 +18,35 @@ public:
|
|||||||
}
|
}
|
||||||
EmbedBuilder& addTitle(const std::string& title) {
|
EmbedBuilder& addTitle(const std::string& title) {
|
||||||
if (embed["embeds"].size() == 0) {
|
if (embed["embeds"].size() == 0) {
|
||||||
embed["embeds"].push_back(nlohmann::json::object());
|
embed["embeds"].emplace_back(nlohmann::json::object());
|
||||||
}
|
}
|
||||||
embed["embeds"].back()["title"] = title;
|
embed["embeds"].back()["title"] = title;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
EmbedBuilder& addDescription(const std::string& desc) {
|
EmbedBuilder& addDescription(const std::string& desc) {
|
||||||
if (embed["embeds"].size() == 0) {
|
if (embed["embeds"].size() == 0) {
|
||||||
embed["embeds"].push_back(nlohmann::json::object());
|
embed["embeds"].emplace_back(nlohmann::json::object());
|
||||||
}
|
}
|
||||||
embed["embeds"].back()["description"] = desc;
|
embed["embeds"].back()["description"] = desc;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
EmbedBuilder& addColor(const std::string& color) {
|
EmbedBuilder& addColor(const std::string& color) {
|
||||||
if (embed["embeds"].size() == 0) {
|
if (embed["embeds"].size() == 0) {
|
||||||
embed["embeds"].push_back(nlohmann::json::object());
|
embed["embeds"].emplace_back(nlohmann::json::object());
|
||||||
}
|
}
|
||||||
embed["embeds"].back()["color"] = color;
|
embed["embeds"].back()["color"] = color;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
EmbedBuilder& addUrl(const std::string& url) {
|
EmbedBuilder& addUrl(const std::string& url) {
|
||||||
if (embed["embeds"].size() == 0) {
|
if (embed["embeds"].size() == 0) {
|
||||||
embed["embeds"].push_back(nlohmann::json::object());
|
embed["embeds"].emplace_back(nlohmann::json::object());
|
||||||
}
|
}
|
||||||
embed["embeds"].back()["url"] = url;
|
embed["embeds"].back()["url"] = url;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
EmbedBuilder& addImage(const std::string& imageurl) {
|
EmbedBuilder& addImage(const std::string& imageurl) {
|
||||||
if (embed["embeds"].size() == 0) {
|
if (embed["embeds"].size() == 0) {
|
||||||
embed["embeds"].push_back(nlohmann::json::object());
|
embed["embeds"].emplace_back(nlohmann::json::object());
|
||||||
}
|
}
|
||||||
embed["embeds"].back()["image"] = { {"url", imageurl} };
|
embed["embeds"].back()["image"] = { {"url", imageurl} };
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -8,7 +8,7 @@ private:
|
|||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
NetworkManager& req;
|
NetworkManager& req;
|
||||||
public:
|
public:
|
||||||
Message(const nlohmann::json& data) : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {};
|
Message(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {};
|
||||||
std::string send(const std::string& id, const nlohmann::json& msg) {
|
std::string send(const std::string& id, const nlohmann::json& msg) {
|
||||||
std::cout << id << msg << std::endl;
|
std::cout << id << msg << std::endl;
|
||||||
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump());
|
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump());
|
||||||
@ -28,5 +28,16 @@ public:
|
|||||||
nlohmann::json pack(const nlohmann::json& key, const std::string& value) const {
|
nlohmann::json pack(const nlohmann::json& key, const std::string& value) const {
|
||||||
return {{ key, value }};
|
return {{ key, value }};
|
||||||
}
|
}
|
||||||
|
struct Api {
|
||||||
|
nlohmann::json data;
|
||||||
|
NetworkManager& req;
|
||||||
|
Api(const nlohmann::json& json = "") : data(json), req(NetworkManager::getInstance()) {};
|
||||||
|
unsigned latency() const {
|
||||||
|
return req.getLatency();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::string str() const {
|
||||||
|
return std::string(data);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
@ -2,7 +2,6 @@
|
|||||||
#define GATEWAY_WEBSOCKET_HPP_
|
#define GATEWAY_WEBSOCKET_HPP_
|
||||||
#include <includes.h>
|
#include <includes.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
|
||||||
#include <ixwebsocket/IXNetSystem.h>
|
#include <ixwebsocket/IXNetSystem.h>
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
@ -105,12 +104,12 @@ public:
|
|||||||
int getIntents() const {
|
int getIntents() const {
|
||||||
return WebSocket::intents;
|
return WebSocket::intents;
|
||||||
}
|
}
|
||||||
void on(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
|
void on(const std::string&& event, std::function<void(const nlohmann::json&)> handler) {
|
||||||
eventHandlers[event] = [handler](const nlohmann::json& message) {
|
eventHandlers[event] = [handler](const nlohmann::json& message) {
|
||||||
handler(message.get<nlohmann::json>());
|
handler(message.get<nlohmann::json>());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
void once(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
|
void once(const std::string&& event, std::function<void(const nlohmann::json&)> handler) {
|
||||||
eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable {
|
eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable {
|
||||||
isCalled == false ? isCalled = true : 0, handler(message.get<nlohmann::json>());
|
isCalled == false ? isCalled = true : 0, handler(message.get<nlohmann::json>());
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,7 @@ private:
|
|||||||
CURL* curl;
|
CURL* curl;
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
|
std::chrono::duration<double, std::milli> duration;
|
||||||
NetworkManager& operator=(const NetworkManager&) = delete;
|
NetworkManager& operator=(const NetworkManager&) = delete;
|
||||||
NetworkManager(const NetworkManager&) = delete;
|
NetworkManager(const NetworkManager&) = delete;
|
||||||
NetworkManager() : web(WebSocket::getInstance()) {
|
NetworkManager() : web(WebSocket::getInstance()) {
|
||||||
@ -35,13 +36,17 @@ public:
|
|||||||
static NetworkManager instance;
|
static NetworkManager instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
unsigned long getLatency() const {
|
||||||
|
return duration.count();
|
||||||
|
}
|
||||||
std::string request(const std::string& method, const std::string& path, const std::string& data = "") {
|
std::string request(const std::string& method, const std::string& path, const std::string& data = "") {
|
||||||
std::string response;
|
std::string response;
|
||||||
if (curl) {
|
if (curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
|
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
||||||
curl_slist* headers = curl_slist_append(headers, "Accept: application/json");
|
curl_slist* headers = nullptr;
|
||||||
|
headers = curl_slist_append(headers, "Accept: application/json");
|
||||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||||
headers = curl_slist_append(headers, ("Authorization: " + web.getToken()).c_str());
|
headers = curl_slist_append(headers, ("Authorization: " + web.getToken()).c_str());
|
||||||
headers = curl_slist_append(headers, "User-Agent: DiscordBot");
|
headers = curl_slist_append(headers, "User-Agent: DiscordBot");
|
||||||
@ -49,8 +54,10 @@ public:
|
|||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
|
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
|
||||||
|
auto start = std::chrono::steady_clock::now();
|
||||||
if ((res = curl_easy_perform(curl)) != 0) Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res)));
|
if ((res = curl_easy_perform(curl)) != 0) Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res)));
|
||||||
curl_slist_free_all(headers);
|
curl_slist_free_all(headers);
|
||||||
|
duration = std::chrono::steady_clock::now() - start;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Log::create(INFO, NETWORK, response);
|
Log::create(INFO, NETWORK, response);
|
||||||
|
@ -6,9 +6,10 @@ int main(int argc, char* argv[]) {
|
|||||||
bot->on(GatewayEvents::READY, [](const Discord<Message, User>& a) {
|
bot->on(GatewayEvents::READY, [](const Discord<Message, User>& a) {
|
||||||
std::cout << DiscordEndpoints::details::latest << std::endl;
|
std::cout << DiscordEndpoints::details::latest << std::endl;
|
||||||
});
|
});
|
||||||
bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord<Message, User, Author>& msg) {
|
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author, Message::Api>& msg) {
|
||||||
if (msg.get<2>().isBot == false) {
|
if (msg.get<2>().isBot == false) {
|
||||||
msg.get<0>().send("939957962972229634", msg.get<0>().pack("content", msg.get<2>().global_name));
|
msg.get<0>().send("939957962972229634", msg.get<0>().pack("content", std::to_string(msg.get<3>().latency()) + "ms"));
|
||||||
|
msg.get<0>().send("939957962972229634", msg.get<0>().pack("content", msg.get<2>().avatar));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
bot->start();
|
bot->start();
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
int add(int a, int b) {
|
#include <includes.h>
|
||||||
return a + b;
|
#include <net.h>
|
||||||
}
|
TEST(NetworkManagerTest, RequestReturnsExpectedValue) {
|
||||||
TEST(AddTest, PositiveNumbers) {
|
NetworkManager& networkManager = NetworkManager::getInstance();
|
||||||
EXPECT_EQ(add(1, 2), 3);
|
EXPECT_FALSE(networkManager.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/939957962972229634/messages", { "content", "test" }).empty());
|
||||||
EXPECT_EQ(add(2, 3), 5);
|
|
||||||
}
|
|
||||||
TEST(AddTest, NegativeNumbers) {
|
|
||||||
EXPECT_EQ(add(-1, -1), -2);
|
|
||||||
EXPECT_EQ(add(-1, 1), 0);
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user