This commit is contained in:
fluttershy 2025-02-10 16:17:48 +05:00
parent eee362d419
commit 227c77604f
6 changed files with 9 additions and 15 deletions

View File

@ -1,8 +1,6 @@
# Sparkle # Sparkle
Library for creating Discord bots. Library for creating Discord bots.
## How to Build ## How to Build
### Required Libraries ### Required Libraries
- `curl` - `curl`
- `ixwebsocket` - `ixwebsocket`
@ -25,4 +23,4 @@ build/sparkle -t <BOT_TOKEN>
- [ ] Interactions - [ ] Interactions
- [ ] Tests - [ ] Tests
# #
![](fluttershy.gif) ![](res/fluttershy.gif)

View File

@ -27,11 +27,11 @@ void EventEmitter::once(const std::string& event, const eventHandlers& handler)
handlers[event].emplace_back(std::move(wrappedHandler)); handlers[event].emplace_back(std::move(wrappedHandler));
} }
void EventEmitter::off(const std::string& event, eventHandlers handler) { void EventEmitter::off(const std::string& event, eventHandlers handler) {
if (auto it = handlers.find(event); it != handlers.end()) { if (auto it = handlers.find(event); it != handlers.cend()) {
auto& vec = it->second; auto& vec = it->second;
vec.erase(std::ranges::remove_if(vec, [&handler](const eventHandlers& h) { vec.erase(std::ranges::remove_if(vec, [&handler](const eventHandlers& h) {
return h.target<eventHandlers>() == handler.target<eventHandlers>(); return h.target<eventHandlers>() == handler.target<eventHandlers>();
}).begin(), vec.end()); }).begin(), vec.cend());
} }
} }
WebSocket::WebSocket(const std::string& token, int intents, bool isBot) : token(token), intents(intents), isBot(isBot) { WebSocket::WebSocket(const std::string& token, int intents, bool isBot) : token(token), intents(intents), isBot(isBot) {
@ -71,7 +71,7 @@ WebSocket::WebSocket(const std::string& token, int intents, bool isBot) : token(
switch (res["op"].get<GatewayOpcodes>()) { switch (res["op"].get<GatewayOpcodes>()) {
case GatewayOpcodes::Hello: case GatewayOpcodes::Hello:
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>(); heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
!connected ? connected = true, webSocket.send(id.dump()) : false; if (connected) connected = true; else webSocket.send(id.dump());
std::thread([this, &heartbeat_interval, &connected]() { std::thread([this, &heartbeat_interval, &connected]() {
while (connected && heartbeat_interval != -1) { while (connected && heartbeat_interval != -1) {
Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval)); Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval));

View File

@ -26,8 +26,8 @@ private:
const nlohmann::json payload = { {"op", GatewayOpcodes::Heartbeat}, {"d", nullptr} }; const nlohmann::json payload = { {"op", GatewayOpcodes::Heartbeat}, {"d", nullptr} };
WebSocket(const std::string& token, int intents, bool isBot); WebSocket(const std::string& token, int intents, bool isBot);
public: public:
auto operator=(const WebSocket&)->WebSocket & = delete; auto operator=(const WebSocket&)->WebSocket& = delete;
auto operator=(WebSocket&&)->WebSocket & = delete; auto operator=(WebSocket&&)->WebSocket& = delete;
WebSocket(WebSocket&&) = delete; WebSocket(WebSocket&&) = delete;
WebSocket(const WebSocket&) = delete; WebSocket(const WebSocket&) = delete;
_maybe_unused void sendPresenceUpdate(const char* statusType, const std::string& activityName, const int& activityType); _maybe_unused void sendPresenceUpdate(const char* statusType, const std::string& activityName, const int& activityType);

View File

Before

Width:  |  Height:  |  Size: 189 KiB

After

Width:  |  Height:  |  Size: 189 KiB

View File

@ -28,18 +28,16 @@ int main(int argc, char* argv[]) {
class WebSocket* bot = &WebSocket::getInstance(result["token"].as<std::string>(), GatewayIntents::AllIntents); class WebSocket* bot = &WebSocket::getInstance(result["token"].as<std::string>(), GatewayIntents::AllIntents);
bot->on(GatewayEvents::READY, [](const Discord<User>& ctx) { bot->once(GatewayEvents::READY, [](const Discord<User>& ctx) {
auto& [user] = ctx.ctx(); auto& [user] = ctx.ctx();
std::cout << std::boolalpha << user->isBot << std::endl; std::cout << std::boolalpha << user->isBot << std::endl;
std::cout << nlohmann::json::parse(user->me())["username"].get<std::string>() << 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) { bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author>& msg) {
auto& [message, user, author] = msg.ctx(); auto& [message, user, author] = msg.ctx();
if (!author->isBot) { if (!author->isBot) {
message->send("1334963948767940648", message->pack("content", std::to_string(user->isBot))); message->send("1334963948767940648", message->pack("content", std::to_string(user->isBot)));
bot->getEvents();
bot->sendPresenceUpdate(StatusType::DND, "meow", ActivityType::Listening);
} }
}); });

View File

@ -1,9 +1,7 @@
/*
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <includes.hpp> #include <includes.hpp>
#include <network.hpp> #include <network.hpp>
TEST(NetworkManagerTest, RequestReturnsExpectedValue) { TEST(NetworkManagerTest, RequestReturnsExpectedValue) {
NetworkManager& networkManager = NetworkManager::getInstance(); NetworkManager& networkManager = NetworkManager::getInstance();
EXPECT_FALSE(networkManager.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/939957962972229634/messages", { "content", "test" }).empty()); EXPECT_FALSE(networkManager.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/939957962972229634/messages", { "content", "test" }).empty());
} }
*/