Skip to content

Commit

Permalink
Improve CMake, update examples and add non-blocking option
Browse files Browse the repository at this point in the history
  • Loading branch information
sanz committed Mar 27, 2023
1 parent 856682e commit b7cfd4a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target_include_directories(topgg PUBLIC include)

target_link_libraries(topgg PUBLIC
${CURL_LIBRARIES}
nlohmann_json::nlohmann_json
)

if (MSVC)
Expand All @@ -43,9 +44,10 @@ target_link_libraries(leaderboard PRIVATE
)

find_package(CURL REQUIRED)
find_package(nlohmann_json 3.2.0 REQUIRED)

add_subdirectory(third_party/Catch2)

# Uncomment if you want to run unit tests
# enable_testing()
# add_subdirectory(test)
# add_subdirectory(test)
27 changes: 15 additions & 12 deletions examples/votebot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@

#include "topgg/topgg.hpp"

// Put your bot token here
const std::string BOT_TOKEN = "YOUR_BOT_TOKEN";
// Put your Top.gg bot token here
const std::string TOPGG_TOKEN = "YOUR_TOPGG_TOKEN";

// Put your Top.gg bot webhook server port here
const uint16_t PORT = 5000;

int main()
{
topgg::Api api(BOT_TOKEN);
topgg::Api api(TOPGG_TOKEN);
topgg::WebhookServer server(api, PORT);

server.onVote([&api](const topgg::Vote& vote) {
// Increment the vote count for this user or do something else with the vote data
std::cout << "User " << vote.getUserID() << " voted for the bot!\n";

// Check if there was an error sending a thank you message to the user
const auto [httpCode, responseBody] = api.post("/bots/stats/votes", {{"bot", api.getBotID()}, {"user", vote.getUserID()}});
if (httpCode != 200) {
std::cerr << "Failed to thank the user for voting: " << responseBody << "\n";
}
// Launch in a new thread for non-blocking behavior
std::thread([vote, &api] {
// Increment the vote count for this user or do something else with the vote data
std::cout << "User " << vote.getUserID() << " voted for the bot!\n";

// Check if there was an error sending a thank you message to the user
const auto [httpCode, responseBody] = api.post("/bots/stats/votes", {{"bot", api.getBotID()}, {"user", vote.getUserID()}});
if (httpCode != 200) {
std::cerr << "Failed to thank the user for voting: " << responseBody << "\n";
}
}).detach();
});

server.start();

return 0;
}
}

0 comments on commit b7cfd4a

Please sign in to comment.