Skip to content

Commit

Permalink
refactor: ♻️ edit shard log function; update README; update simple ex…
Browse files Browse the repository at this point in the history
…ample
  • Loading branch information
Xminent committed Oct 31, 2023
1 parent d74bd42 commit 6056141
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 46 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,54 @@ A WIP C++ library for Discord applications.

## Usage/Examples

```cpp
#include <dotenv/dotenv.h>
#include <ekizu/shard.hpp>
#include <nlohmann/json.hpp>

using namespace ekizu;

void handle_event(Event e);

int main()
{
dotenv::init();

const auto intents = Intents::AllIntents;
auto shard =
Shard{ ShardId::ONE, dotenv::getenv("DISCORD_TOKEN"), intents };

if (const auto res = shard.run(handle_event); !res) {
fmt::print("Failed to run shard: {}\n", res.error().message());
return 1;
}
}

template <typename... Func> struct overload : Func... {
using Func::operator()...;
};

template <typename... Func> overload(Func...) -> overload<Func...>;

void handle_event(Event ev)
{
std::visit(
overload{ [](const Ready &r) {
fmt::print("{} is ready!", r.user.username);
},
[](MessageCreate msg) {
fmt::print("Message: {}\n",
msg.message.content);
},
[](Resumed) { fmt::print("Resumed Event Called\n"); },
[](const auto &e) {
fmt::print("Unhandled event: {}\n",
typeid(e).name());
} },
ev);
}
```
See [examples](https://github.com/Xminent/ekizu/tree/dev/examples).
## Getting Started
Expand Down
56 changes: 17 additions & 39 deletions examples/simple.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
#include <dotenv/dotenv.h>
#include <ekizu/lru_cache.hpp>
#include <ekizu/shard.hpp>
#include <fmt/core.h>
#include <nlohmann/json.hpp>

using namespace ekizu;

void handle_event(Event e, SnowflakeLruCache<Message> &message_cache);
void handle_event(Event e);

int main()
{
dotenv::init();

const auto token = dotenv::getenv("DISCORD_TOKEN");

if (token.empty()) {
fmt::print("No token :(\n");
return 1;
}

const auto intents = Intents::AllIntents;
auto shard = Shard{ ShardId::ONE, token, intents };
auto message_cache = SnowflakeLruCache<Message>{ 500 };
auto shard =
Shard{ ShardId::ONE, dotenv::getenv("DISCORD_TOKEN"), intents };

if (const auto res = shard.run([&message_cache](Event e) {
handle_event(std::move(e), message_cache);
});
!res) {
if (const auto res = shard.run(handle_event); !res) {
fmt::print("Failed to run shard: {}\n", res.error().message());
return 1;
}
Expand All @@ -38,30 +26,20 @@ template <typename... Func> struct overload : Func... {

template <typename... Func> overload(Func...) -> overload<Func...>;

void handle_event(Event ev, SnowflakeLruCache<Message> &message_cache)
void handle_event(Event ev)
{
std::visit(
overload{
[](const Ready &r) {
fmt::print("{} is ready!", r.user.username);
},
[&message_cache](MessageCreate msg) {
fmt::print("{}'s message history:\n",
msg.message.author.username);

// cache the message.
message_cache.emplace(msg.message.id,
std::move(msg.message));

// Print the author's message history.
for (const auto &[id, m] : message_cache) {
fmt::print("{}: {}\n", id, m.content);
}
},
[](Resumed) { std::cout << "Resumed Event Called\n"; },
[](const auto &e) {
fmt::print("Unhandled event: {}\n",
typeid(e).name());
} },
overload{ [](const Ready &r) {
fmt::print("{} is ready!", r.user.username);
},
[](MessageCreate msg) {
fmt::print("Message: {}\n",
msg.message.content);
},
[](Resumed) { fmt::print("Resumed Event Called\n"); },
[](const auto &e) {
fmt::print("Unhandled event: {}\n",
typeid(e).name());
} },
ev);
}
27 changes: 20 additions & 7 deletions src/shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,8 @@ void Shard::handle_event(tcb::span<const std::byte> data)

void Shard::handle_dispatch(const nlohmann::json &data)
{
// std::string type = data["t"];
// const auto msg =
// fmt::format("received dispatch | event_type={}, sequence={}",
// type, m_sequence->load());

// log(msg);
log(fmt::format("received dispatch | event_type={}, sequence={}",
data["t"].get<std::string>(), m_sequence->load()));

if (!m_on_event) {
return;
Expand Down Expand Up @@ -198,9 +194,26 @@ void Shard::handle_heartbeat_ack()

void Shard::log(std::string_view msg, LogLevel level)
{
handle_dispatch(nlohmann::json{
if (!m_on_event) {
return;
}

auto lk = m_on_event->lock();
auto &cb = static_cast<std::function<void(Event)> &>(lk);

if (!cb) {
return;
}

const auto debug_event = event_from_str(nlohmann::json{
{ "t", "LOG" },
{ "d", { { "message", msg }, { "level", level } } } });

if (!debug_event) {
return;
}

cb(*debug_event);
}

Result<void> Shard::set_auto_reconnect(bool auto_reconnect)
Expand Down

0 comments on commit 6056141

Please sign in to comment.