This repository has been archived on 2025-03-15. You can view files and clone it, but cannot push or open issues or pull requests.
sparkle/libs/api/Message.hpp

41 lines
1.7 KiB
C++
Raw Permalink Normal View History

2024-12-31 02:20:56 +05:00
#ifndef API_MESSAGE_HPP_
#define API_MESSAGE_HPP_
2025-01-25 13:22:59 +05:00
#include <includes.hpp>
#include <net.hpp>
2024-12-31 02:20:56 +05:00
class Message {
private:
2025-01-25 11:24:56 +05:00
nlohmann::json data;
2024-12-31 02:20:56 +05:00
WebSocket& web;
NetworkManager& req;
public:
2025-01-25 11:24:56 +05:00
Message(const nlohmann::json& data = "") : data(data), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {};
std::string send(const std::string& id, const nlohmann::json& msg) {
2025-01-19 04:35:20 +05:00
return req.request(HttpMethods::POST, DiscordEndpoints::details::latest + "/channels/" + id + "/messages", msg.dump());
2024-12-31 02:20:56 +05:00
}
2025-01-25 11:24:56 +05:00
std::string getMessages(const std::string& id, const std::string& count, const std::string& before = "") {
2025-01-13 14:40:21 +05:00
if (before.empty()) {
2025-01-19 04:35:20 +05:00
return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/channels/" + id + "/messages?limit=" + count);
2025-01-25 13:22:59 +05:00
} else {
2025-01-19 04:35:20 +05:00
return req.request(HttpMethods::GET, DiscordEndpoints::details::latest + "/channels/" + id + "/messages?before=" + before + "&limit=" + count);
2025-01-13 14:40:21 +05:00
}
2025-01-19 04:35:20 +05:00
return "";
2025-01-13 14:40:21 +05:00
}
2025-01-25 11:24:56 +05:00
std::string deleteMessage(const std::string& channel_id, const std::string& message_id) {
2025-01-19 04:35:20 +05:00
return req.request(HttpMethods::DELETE, DiscordEndpoints::details::latest + "/channels/" + channel_id + "/messages/" + message_id);
2025-01-13 14:40:21 +05:00
}
2025-01-25 11:24:56 +05:00
nlohmann::json pack(const nlohmann::json& key, const std::string& value) const {
2025-01-25 13:22:59 +05:00
return { { key, value } };
2025-01-25 11:24:56 +05:00
}
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);
}
2024-12-31 02:20:56 +05:00
};
#endif