Skip to content

Commit

Permalink
chore: 📝 update README; remove comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Xminent committed Dec 26, 2023
1 parent c6fa336 commit 229d936
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 74 deletions.
122 changes: 54 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ A WIP C++ library for Discord applications.

## Features

- Fully asynchronous, perfect for single-threaded applications
- Low memory footprint
- Discord API v10 support
- Gateway support, with auto-reconnect
Expand All @@ -12,79 +13,64 @@ A WIP C++ library for Discord applications.
## Usage/Examples

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

using namespace ekizu;

std::function<void(Event)> handle_event(HttpClient &http);

int main()
{
dotenv::init();
const auto token = dotenv::getenv("DISCORD_TOKEN");

auto http = HttpClient{ token };
const auto intents = Intents::AllIntents;
auto shard = Shard{ ShardId::ONE, token, intents };

if (const auto res = shard.run(handle_event(http)); !res) {
fmt::println("Failed to run shard: {}", res.error().message());
return 1;
}
Result<> handle_event(const Event &ev, const HttpClient &http,
const asio::yield_context &yield);

async_main(const asio::yield_context &yield) {
const std::string token{std::getenv("DISCORD_TOKEN")};
HttpClient http{token};
Shard shard{ShardId::ONE, token, Intents::AllIntents};

while (true) {
auto res = shard.next_event(yield);

if (!res) {
if (res.error().failed()) {
fmt::println(
"Failed to get next event: {}", res.error().message());
return res.error();
}
// Could be handling a non-dispatch event.
continue;
}

asio::spawn(
yield,
[e = std::move(res.value()), &http](auto y) {
(void)handle_event(e, http, y);
},
asio::detached);
}

return outcome::success();
}

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

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

std::function<void(Event)> handle_event(HttpClient &http)
{
return [&http](Event ev) {
std::visit(
overload{
[](const Ready &r) {
fmt::println("{} is ready!",
r.user.username);
},
[&http](const MessageCreate &payload) {
const auto &[msg] = payload;

const auto res =
http.create_message(
msg.channel_id)
.content(fmt::format(
"{} said: {}\nAvatar: {}",
msg.author
.username,
msg.content,
msg.author.avatar ?
*msg.author
.avatar :
"null"))
.send();

if (!res) {
fmt::println(
"Failed to send message: {}",
res.error().message());
}
},
[](Resumed) {
fmt::println("Resumed Event Called");
},
[](const auto &e) {
fmt::println(
"Uncaught {} event: {}",
typeid(e).name(),
nlohmann::json{ e }.dump());
} },
ev);
};
Result<> handle_event(const Event &ev, const HttpClient &http,
const asio::yield_context &yield) {
std::visit(
[&](auto &&event) {
using T = std::decay_t<decltype(event)>;

if constexpr (std::is_same_v<T, MessageCreate>) {
const auto &[msg_a] = event;
const Message &msg = msg_a;

if (msg.content == "ping") {
(void)http.create_message(msg.channel_id)
.content("pong")
.send(yield);
}
}
},
ev);

return outcome::success();
}
```
Expand Down Expand Up @@ -127,10 +113,10 @@ Take full advantage of the [.clang-format](.clang-format) file located in the ro
### Third party Dependencies

- [fmt](https://github.com/fmtlib/fmt) (comes bundled with project)
- [net](https://github.com/xminent/net) (comes bundled with project)
- [Boost (ASIO, Beast, Outcome, URL)](https://github.com/boostorg/boost) (comes bundled with project, unless you have it installed)
- [nlohmann_json](https://github.com/nlohmann/json) (comes bundled with project)
- [zlib](https://github.com/madler/zlib) (comes bundled with project, unless you have it installed)

## License

[MIT](https://choosealicense.com/licenses/mit/)
[MIT](https://choosealicense.com/licenses/mit/)
6 changes: 0 additions & 6 deletions src/shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ Result<> Shard::close(CloseFrame reason,
{static_cast<net::ws::close_code>(reason.code), reason.reason}, yield);
}

// Result<> Shard::close(CloseFrame reason,
// const boost::asio::yield_context &yield) {
// if (!m_ws) { return boost::system::errc::not_connected; }

// }

Result<Event> Shard::next_event(const boost::asio::yield_context &yield) {
BOOST_OUTCOME_TRY(auto msg, next_message(yield));

Expand Down

0 comments on commit 229d936

Please sign in to comment.