1
0
forked from rcxpony/sparkle
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

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

@ -27,11 +27,11 @@ void EventEmitter::once(const std::string& event, const eventHandlers& handler)
handlers[event].emplace_back(std::move(wrappedHandler));
}
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;
vec.erase(std::ranges::remove_if(vec, [&handler](const eventHandlers& h) {
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) {
@ -71,7 +71,7 @@ WebSocket::WebSocket(const std::string& token, int intents, bool isBot) : token(
switch (res["op"].get<GatewayOpcodes>()) {
case GatewayOpcodes::Hello:
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]() {
while (connected && heartbeat_interval != -1) {
Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval));

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

Before

(image error) Size: 189 KiB

After

(image error) Size: 189 KiB

@ -28,18 +28,16 @@ int main(int argc, char* argv[]) {
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();
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) {
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Discord<Message, User, Author>& msg) {
auto& [message, user, author] = msg.ctx();
if (!author->isBot) {
message->send("1334963948767940648", message->pack("content", std::to_string(user->isBot)));
bot->getEvents();
bot->sendPresenceUpdate(StatusType::DND, "meow", ActivityType::Listening);
}
});

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