diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index ed66f0a..711b383 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -11,11 +11,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: add curl - run: sudo apt-get install libcurl4-openssl-dev - - name: configure + - name: Install Dependencies + run: sudo apt-get update && sudo apt-get install libcurl4-openssl-dev -y + - name: Apply Configure run: ./configure - - name: cmake with fetching + - name: Cmake With Fetching External Dependencies run: cmake -DUSE_EXTERNAL_DPP=OFF -DUSE_EXTERNAL_JSON=OFF - - name: make + - name: Make run: make diff --git a/include/coingecko.h b/include/coingecko.h index d99b02a..e753575 100644 --- a/include/coingecko.h +++ b/include/coingecko.h @@ -1,13 +1,14 @@ -#include - #include +#include #include +#include #include "nlohmann/json.hpp" -#include - namespace gecko { -void fetch(dpp::slashcommand_t event); -} + +void fetch_tokens(dpp::slashcommand_t event); +void fetch_price(dpp::slashcommand_t event); + +} // namespace gecko diff --git a/src/coingecko.cpp b/src/coingecko.cpp index cf4a711..7bc7331 100644 --- a/src/coingecko.cpp +++ b/src/coingecko.cpp @@ -9,7 +9,7 @@ size_t write_callback(char* ptr, size_t size, size_t nmemb, std::string* data) { return size * nmemb; } -void gecko::fetch(dpp::slashcommand_t event) { +void gecko::fetch_price(dpp::slashcommand_t event) { // Get the coingecko id symbol from the command arguments. std::string coingecko_id = std::get(event.get_parameter("coingecko_id")); @@ -63,3 +63,49 @@ void gecko::fetch(dpp::slashcommand_t event) { curl_easy_cleanup(curl); } + +void gecko::fetch_tokens(dpp::slashcommand_t event) { + CURL* curl = curl_easy_init(); + if (!curl) { + std::cerr << "Error: could not initialize libcurl" << std::endl; + return; + } + + std::string url = "https://api.coingecko.com/api/v3/coins/"; + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + + std::string response_data; + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data); + + CURLcode res = curl_easy_perform(curl); + if (res != CURLE_OK) { + std::cerr << "Error: curl_easy_perform() failed: " + << curl_easy_strerror(res) << std::endl; + curl_easy_cleanup(curl); + return; + } + + try { + json response_json = json::parse(response_data); + std::cout << "=============================" << std::endl; + std::cout << ">> coingecko response: " << response_json << std::endl; + std::string tokens; + for (auto& token : response_json) { + tokens.append("- "); + tokens.append(token["id"]); + tokens.append("\n"); + } + + event.reply(tokens); + + } catch (const std::exception& e) { + std::cerr << "Error: failed to parse JSON response: " << e.what() + << std::endl; + curl_easy_cleanup(curl); + + event.reply(":exclamation: coins: error failed to call API data."); + } + + curl_easy_cleanup(curl); +} diff --git a/src/main.cpp b/src/main.cpp index 53cd9bd..5c9c851 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,27 +5,36 @@ int main() { dpp::cluster bot(std::getenv("DISCORD_TOKEN")); bot.on_slashcommand([](auto event) { - if (event.command.get_command_name() == "ping") { + auto input = event.command.get_command_name(); + if (input == "ping") { event.reply("Pong!"); } - - if (event.command.get_command_name() == "price") { - gecko::fetch(event); + if (input == "price") { + gecko::fetch_price(event); + } + if (input == "coins") { + gecko::fetch_tokens(event); } - return 0; }); bot.on_ready([&bot](auto event) { if (dpp::run_once()) { + dpp::slashcommand command_coins; + command_coins.set_name("coins") + .set_description("List of available tokens from Coingecko.") + .set_application_id(bot.me.id); + bot.global_command_create(command_coins); + dpp::slashcommand command_price; command_price.set_name("price") - .set_description("Get the price of a crypto token") + .set_description( + "Get the price of a crypto token. (Please use /coins to get all " + "listed tokens.)") .set_application_id(bot.me.id) .add_option( dpp::command_option(dpp::co_string, "coingecko_id", "(Coingecko) ID of the token: ", true)); - bot.global_command_create(command_price); } });