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/interface/Message.hpp

38 lines
1.7 KiB
C++
Raw Normal View History

2025-01-29 16:06:47 +05:00
#ifndef INTERFACE_MESSAGE_HPP_
#define INTERFACE_MESSAGE_HPP_
2025-01-25 13:22:59 +05:00
#include <includes.hpp>
2025-01-29 16:06:47 +05:00
#include <network.hpp>
2024-12-31 02:20:56 +05:00
class Message {
private:
2025-01-27 17:24:33 +05:00
const nlohmann::json& data;
2024-12-31 02:20:56 +05:00
WebSocket& web;
NetworkManager& req;
public:
2025-01-30 23:05:59 +05:00
Message(const nlohmann::json& json) : data(json), web(WebSocket::getInstance()), req(NetworkManager::getInstance()) {};
auto send(const std::string& id, const nlohmann::json& msg) -> std::string {
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-30 23:05:59 +05:00
auto getMessages(const std::string& id, const std::string& count, const std::string& before = "") -> std::string {
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-30 23:05:59 +05:00
auto deleteMessage(const std::string& channel_id, const std::string& message_id) -> std::string {
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-30 23:05:59 +05:00
_nodiscard auto pack(const nlohmann::json& key, const std::string& value) const -> nlohmann::json {
2025-01-25 13:22:59 +05:00
return { { key, value } };
2025-01-25 11:24:56 +05:00
}
struct Api {
2025-01-27 17:24:33 +05:00
const nlohmann::json& data;
2025-01-25 11:24:56 +05:00
NetworkManager& req;
2025-01-27 17:24:33 +05:00
Api(const nlohmann::json& json) : data(json), req(NetworkManager::getInstance()) {};
2025-01-30 17:21:08 +05:00
unsigned long latency() const {
2025-01-25 11:24:56 +05:00
return req.getLatency();
}
};
2024-12-31 02:20:56 +05:00
};
#endif