fixes
This commit is contained in:
parent
2e54a17b5d
commit
d75860b300
@ -6,20 +6,37 @@ project(sparkle)
|
||||
|
||||
include(GoogleTest)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
#set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
set(SOURCE sources/main.cpp)
|
||||
set(LIBS ${CMAKE_SOURCE_DIR}/libs/)
|
||||
set(INCLUDE ${CMAKE_SOURCE_DIR}/include/)
|
||||
set(TESTS ${CMAKE_SOURCE_DIR}/tests)
|
||||
set(ADDITIONAL_CXX_FLAGS_DEBUG "-march=native -pipe -Wall -Wextra -fsanitize=address -O0")
|
||||
set(ADDITIONAL_CXX_FLAGS_RELEASE "-march=native -pipe -Wall -Wextra -O2 -flto")
|
||||
set(ADDITIONAL_CXX_FLAGS_DEBUG "-march=native -O0 -pipe -Wall -Wextra -Wpedantic -Werror -Wconversion -Wuninitialized -Wsign-conversion -Wshadow -fsanitize=address")
|
||||
set(ADDITIONAL_CXX_FLAGS_RELEASE "-march=native -O2 -pipe -Wall -Wextra -Wpedantic -Werror -Wconversion -Wuninitialized -Wsign-conversion -Wshadow -flto=full")
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${ADDITIONAL_CXX_FLAGS_RELEASE}")
|
||||
message(STATUS "cflags: ${CMAKE_CXX_FLAGS_RELEASE}")
|
||||
endif()
|
||||
|
||||
if(CMAKE_BUILD_TYPE)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_definitions(-DDEBUG)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${ADDITIONAL_CXX_FLAGS_DEBUG}")
|
||||
message(STATUS "cflags: ${CMAKE_CXX_FLAGS_DEBUG}")
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
add_definitions(-DRELEASE)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${ADDITIONAL_CXX_FLAGS_RELEASE}")
|
||||
message(STATUS "cflags: ${CMAKE_CXX_FLAGS_RELEASE}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
file(GLOB NETWORK_SOURCES libs/network/*.cpp)
|
||||
file(GLOB WEBSOCKET_SOURCES libs/gateway/*.cpp)
|
||||
|
||||
|
||||
find_package(CURL REQUIRED)
|
||||
find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket)
|
||||
find_library(IXWEBSOCKET_LIBRARIES ixwebsocket)
|
||||
@ -32,22 +49,8 @@ if(NOT CURL_INCLUDE_DIRS OR NOT CURL_LIBRARIES)
|
||||
message(FATAL_ERROR "curl not found")
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
|
||||
endif()
|
||||
|
||||
if(CMAKE_BUILD_TYPE)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
set(CMAKE_CXX_FLAGS "${ADDITIONAL_CXX_FLAGS_DEBUG}")
|
||||
add_definitions(-DDEBUG)
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
add_definitions(-DRELEASE)
|
||||
set(CMAKE_CXX_FLAGS "${ADDITIONAL_CXX_FLAGS_RELEASE}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
message(STATUS "Current compiler: ${CMAKE_CXX_COMPILER}")
|
||||
message(STATUS "${CMAKE_BUILD_TYPE}")
|
||||
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCE} ${NETWORK_SOURCES} ${WEBSOCKET_SOURCES})
|
||||
#add_library(sparkles STATIC ${SOURCE})
|
||||
|
@ -1,10 +1,11 @@
|
||||
#ifndef INCLUDE_INCLUDES_HPP_
|
||||
#define INCLUDE_INCLUDES_HPP_
|
||||
//#include <utils/types.hpp>
|
||||
#include <utils/types.hpp>
|
||||
#include <utils/json.hpp>
|
||||
#include <utils/log.hpp>
|
||||
#include <utils/enums.hpp>
|
||||
#include <utils/functions.hpp>
|
||||
#include <utils/cxxopts.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#endif
|
@ -8,6 +8,7 @@ private:
|
||||
WebSocket& web;
|
||||
NetworkManager& req;
|
||||
public:
|
||||
__deprecated_t("requires update")
|
||||
struct Channel {
|
||||
std::string channel_id;
|
||||
std::string global_name;
|
||||
|
@ -9,7 +9,8 @@ private:
|
||||
public:
|
||||
Discord(const nlohmann::json& data) : data(data), net(std::make_tuple(std::make_unique<Args>(data)...)) {}
|
||||
template<class T>
|
||||
[[deprecated("Use ctx() instead")]] auto& get() const {
|
||||
__deprecated_t("Use ctx() instead")
|
||||
auto& get() const {
|
||||
return *std::get<std::unique_ptr<T>>(net);
|
||||
}
|
||||
const auto& ctx() const {
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
const nlohmann::json& data;
|
||||
NetworkManager& req;
|
||||
Api(const nlohmann::json& json) : data(json), req(NetworkManager::getInstance()) {};
|
||||
unsigned latency() const {
|
||||
unsigned long latency() const {
|
||||
return req.getLatency();
|
||||
}
|
||||
};
|
||||
|
@ -1,7 +1,8 @@
|
||||
#include "websocket.hpp"
|
||||
void EventEmitter::emit(const std::string& event, const nlohmann::json& data) {
|
||||
if (handlers.find(event) != handlers.end()) {
|
||||
for (const auto& handler : handlers[event]) {
|
||||
auto it = handlers.find(event);
|
||||
if (it != handlers.end()) {
|
||||
for (const auto& handler : it->second) {
|
||||
handler(data);
|
||||
}
|
||||
}
|
||||
@ -13,22 +14,23 @@ void EventEmitter::getEvents() {
|
||||
}
|
||||
Log::create(CRITICAL, WEBSOCKET, "End of list");
|
||||
}
|
||||
void EventEmitter::start() {
|
||||
void EventEmitter::start() const {
|
||||
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
void EventEmitter::on(const std::string& event, eventHandlers handler) {
|
||||
handlers[event].emplace_back(handler);
|
||||
handlers[event].emplace_back(std::move(handler));
|
||||
}
|
||||
void EventEmitter::once(const std::string& event, eventHandlers handler) {
|
||||
auto wrappedHandler = [this, event, handler](const nlohmann::json& data) {
|
||||
handler(data);
|
||||
off(event, handler);
|
||||
};
|
||||
handlers[event].emplace_back(wrappedHandler);
|
||||
handlers[event].emplace_back(std::move(wrappedHandler));
|
||||
}
|
||||
void EventEmitter::off(const std::string& event, eventHandlers handler) {
|
||||
if (handlers.find(event) != handlers.end()) {
|
||||
auto& vec = handlers[event];
|
||||
auto it = handlers.find(event);
|
||||
if (it != handlers.end()) {
|
||||
auto& vec = it->second;
|
||||
vec.erase(std::remove_if(vec.begin(), vec.end(), [&handler](const eventHandlers& h) {
|
||||
return h.target<eventHandlers>() == handler.target<eventHandlers>();
|
||||
}), vec.end());
|
||||
@ -110,12 +112,12 @@ WebSocket::~WebSocket() {
|
||||
ix::uninitNetSystem();
|
||||
}
|
||||
std::string WebSocket::getToken() const {
|
||||
return isBot ? std::string("Bot " + WebSocket::token) : WebSocket::token;
|
||||
return this->isBot ? std::string("Bot " + this->token) : this->token;
|
||||
}
|
||||
int WebSocket::getIntents() const {
|
||||
return WebSocket::intents;
|
||||
return this->intents;
|
||||
}
|
||||
[[maybe_unused]]
|
||||
__maybe_unused
|
||||
void WebSocket::sendPresenceUpdate(const char* statusType, const std::string& activityName, const int& activityType) {
|
||||
nlohmann::json prsUpdate = {
|
||||
{"op", 3},
|
||||
|
@ -11,7 +11,7 @@ protected:
|
||||
void emit(const std::string& event, const nlohmann::json& data);
|
||||
public:
|
||||
void getEvents();
|
||||
void start();
|
||||
void start() const;
|
||||
void on(const std::string& event, eventHandlers handler);
|
||||
void once(const std::string& event, eventHandlers handler);
|
||||
void off(const std::string& event, eventHandlers handler);
|
||||
@ -29,7 +29,7 @@ private:
|
||||
WebSocket(const WebSocket&) = delete;
|
||||
WebSocket(const std::string& token, const int& intents, const bool& isBot);
|
||||
public:
|
||||
[[maybe_unused]]
|
||||
__maybe_unused
|
||||
void sendPresenceUpdate(const char* statusType, const std::string& activityName, const int& activityType);
|
||||
static WebSocket& getInstance(const std::string& token = "", const int& intents = 0, const bool& bot = true);
|
||||
~WebSocket();
|
||||
|
@ -5,11 +5,13 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <ctime>
|
||||
#include <utils/types.hpp>
|
||||
enum level { INFO, WARNING, ERROR, CRITICAL };
|
||||
enum type { WEBSOCKET, NETWORK, API };
|
||||
class Log {
|
||||
public:
|
||||
[[maybe_unused]] static void create([[maybe_unused]] const level& lvl, [[maybe_unused]] const type& t, [[maybe_unused]] const std::string& message) {
|
||||
__maybe_unused
|
||||
static void create(__maybe_unused const level& lvl, __maybe_unused const type& t, __maybe_unused const std::string& message) {
|
||||
#ifdef DEBUG
|
||||
std::string color;
|
||||
switch (lvl) {
|
||||
|
@ -1,3 +1,7 @@
|
||||
#ifndef UTILS_TYPES_HPP_
|
||||
#define UTILS_TYPES_HPP_
|
||||
#define __maybe_unused [[maybe_unused]]
|
||||
#define __deprecated_t(text) [[deprecated(text)]]
|
||||
#define __deprecated [[deprecated]]
|
||||
#define __nodiscard [[nodiscard]]
|
||||
#endif
|
@ -1,4 +1,3 @@
|
||||
#include <utils/cxxopts.hpp>
|
||||
#include <api.hpp>
|
||||
#include <includes.hpp>
|
||||
int main(int argc, char* argv[]) {
|
||||
@ -14,18 +13,18 @@ int main(int argc, char* argv[]) {
|
||||
std::cout << "sparkle version 0.3" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
if (result.count("help")) {
|
||||
std::cout << options.help() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
if (!result.contains("token")) {
|
||||
std::cout << "\033[31mError: no token provided.\033[0m" << std::endl;
|
||||
std::cout << options.help() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
class WebSocket* bot = &WebSocket::getInstance(result["token"].as<std::string>(), GatewayIntents::AllIntents);
|
||||
|
||||
bot->on(GatewayEvents::READY, [](const Discord<User>& ctx) {
|
||||
@ -33,15 +32,15 @@ int main(int argc, char* argv[]) {
|
||||
std::cout << std::boolalpha << user->isBot << std::endl;
|
||||
std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << std::endl;
|
||||
});
|
||||
|
||||
bot->on(GatewayEvents::MESSAGE_CREATE, [bot](const Discord<Message, User, Author>& msg) {
|
||||
auto& [message, user, author] = msg.ctx();
|
||||
if (!author->isBot) {
|
||||
message->send("939957962972229634", message->pack("content", std::to_string(user->isBot)));
|
||||
bot->getEvents();
|
||||
bot->sendPresenceUpdate(StatusType::DND, "meow", ActivityType::Streaming);
|
||||
//bot->sendPresenceUpdate(StatusType::DND, "meow", ActivityType::Streaming);
|
||||
}
|
||||
});
|
||||
|
||||
bot->start();
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user