Skip to content

Commit

Permalink
[feat,doc]: make autoposter a part of the client object, and add doxy…
Browse files Browse the repository at this point in the history
…gen documentation
  • Loading branch information
null8626 committed Jul 11, 2024
1 parent c036233 commit a3916ec
Show file tree
Hide file tree
Showing 20 changed files with 3,634 additions and 505 deletions.
14 changes: 13 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
* eol=lf
.clang-format eol=lf
.gitignore eol=lf
.gitmodules eol=lf
.gitattributes eol=lf
README.md eol=lf
CMakeLists.txt eol=lf
docs/Doxyfile eol=lf
docs/*.png binary
docs/*.css eol=lf linguist-vendored=true
docs/*.html eol=lf linguist-vendored=true
**/*.cmake eol=lf
**/*.cpp eol=lf
**/*.h eol=lf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
**/*.rpm
.idea
cmake-build-debug/

include/dpp/
build/
deps/
docs/html/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "docs/doxygen-awesome-css"]
path = docs/doxygen-awesome-css
url = https://github.com/jothepro/doxygen-awesome-css.git
18 changes: 9 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ file(GLOB TOPGG_SOURCE_FILES src/*.cpp)

add_library(topgg SHARED ${TOPGG_SOURCE_FILES})

if(ENABLE_CORO)
set(TOPGG_CXX_STANDARD 20)
target_compile_definitions(topgg PUBLIC DPP_CORO=ON)
else()
set(TOPGG_CXX_STANDARD 17)
endif()

set_target_properties(topgg PROPERTIES
OUTPUT_NAME topgg
CXX_STANDARD 17
CXX_STANDARD ${TOPGG_CXX_STANDARD}
CXX_STANDARD_REQUIRED ON
)

if (ENABLE_CORO)
target_compile_definitions(topgg PUBLIC DPP_CORO=ON)
set_target_properties(topgg PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
)
endif()

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
Expand All @@ -42,6 +41,7 @@ endif()
if(WIN32)
target_compile_definitions(topgg PRIVATE TOPGG_SHARED)
target_compile_definitions(topgg PRIVATE __TOPGG_BUILDING__)
target_sources(topgg PRIVATE "${CMAKE_SOURCE_DIR}/topgg.rc")
endif()

target_include_directories(topgg PUBLIC
Expand Down
176 changes: 84 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,96 +53,106 @@ cmake --build build --config Release
### Fetching a bot from its Discord ID

```cpp
#include <topgg/topgg.h>
#include <dpp/dpp.h>
#include <iostream>

int main() {
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{&bot, "your top.gg token"};

topgg_client.get_bot(264811613708746752, [](const auto& result) {
try {
const auto topgg_bot = result.get();

std::cout << topgg_bot.username << std::endl;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
});
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{bot, "your top.gg token"};

return 0;
// using C++17 callbacks
topgg_client.get_bot(264811613708746752, [](const auto& result) {
try {
const auto topgg_bot = result.get();

std::cout << topgg_bot.username << std::endl;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
});

// using C++20 coroutines
try {
const auto result = co_await topgg_client.co_get_bot(264811613708746752);
const auto topgg_bot = result.get();

std::cout << topgg_bot.username << std::endl;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
```

### Fetching a user from its Discord ID

```cpp
#include <topgg/topgg.h>
#include <dpp/dpp.h>
#include <iostream>

int main() {
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{&bot, "your top.gg token"};

topgg_client.get_user(661200758510977084, [](const auto& result) {
try {
const auto user = result.get();

std::cout << user.username << std::endl;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
});
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{bot, "your top.gg token"};

return 0;
// using C++17 callbacks
topgg_client.get_user(264811613708746752, [](const auto& result) {
try {
const auto user = result.get();

std::cout << user.username << std::endl;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
});

// using C++20 coroutines
try {
const auto result = co_await topgg_client.co_get_user(661200758510977084);
const auto user = result.get();

std::cout << user.username << std::endl;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
```

### Posting your bot's statistics

```cpp
#include <topgg/topgg.h>
#include <dpp/dpp.h>
#include <iostream>

int main() {
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{&bot, "your top.gg token"};

const size_t server_count = 12345;
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{bot, "your top.gg token"};

topgg_client.post_stats(topgg::stats{server_count}, []() {
// using C++17 callbacks
topgg_client.post_stats([](const auto success) {
if (success) {
std::cout << "stats posted!" << std::endl;
});
}
});

return 0;
// using C++20 coroutines
const auto success = co_await topgg_client.post_stats();

if (success) {
std::cout << "stats posted!" << std::endl;
}
```

### Checking if a user has voted your bot

```cpp
#include <topgg/topgg.h>
#include <dpp/dpp.h>
#include <iostream>

int main() {
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{&bot, "your top.gg token"};

topgg_client.has_voted(661200758510977084, [](const auto& result) {
try {
if (result.get()) {
std::cout << "checks out" << std::endl;
}
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{bot, "your top.gg token"};

// using C++17 callbacks
topgg_client.has_voted(661200758510977084, [](const auto& result) {
try {
if (result.get()) {
std::cout << "checks out" << std::endl;
}
});

return 0;
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
});

// using C++20 coroutines
try {
const auto result = co_await topgg_client.has_voted(661200758510977084);

if (result.get()) {
std::cout << "checks out" << std::endl;
}
} catch (const std::exception& ext) {
std::cout << "error: " << ext.what() << std::endl;
}
```

Expand All @@ -151,39 +161,21 @@ int main() {
If you want for the SDK to automatically retrieve the server count itself by listening to Discord's gateway events, use `topgg::autoposter::cached`!

```cpp
#include <topgg/topgg.h>
#include <dpp/dpp.h>
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{bot, "your top.gg token"};

int main() {
std::shared_ptr<dpp::cluster> bot{new dpp::cluster{"your bot token"}};

topgg::autoposter::cached autoposter{bot, "your top.gg token", std::chrono::minutes(15)};

// your bot's code...

return 0;
}
topgg_client.start_autoposter();
```
### Stats-fed autoposting
If you want to use your own function (e.g: retrieve the server count on your own by making an SQL query), then you can use `topgg::autoposter::custom`!
```cpp
#include <topgg/topgg.h>
#include <dpp/dpp.h>

static topgg::stats fetch_stats(dpp::cluster* bot) {
// fetch server count here...
}

int main() {
std::shared_ptr<dpp::cluster> bot{new dpp::cluster{"your bot token"}};

topgg::autoposter::custom autoposter{bot, "your top.gg token", std::chrono::minutes(15), fetch_stats};
dpp::cluster bot{"your bot token"};
topgg::client topgg_client{bot, "your top.gg token"};
// your bot's code...

return 0;
}
topgg_client.start_autoposter([](dpp::cluster& bot_inner) {
return topgg::stats{...};
});
```
Loading

0 comments on commit a3916ec

Please sign in to comment.