Skip to content

Commit

Permalink
Feature : Add Coins Command (#6)
Browse files Browse the repository at this point in the history
* feat: add coins command (not tested yet)

* fix: make returned tokens/coins as list from bot

* chore: fix failing build by updating the index

* chore: fix missing sudo
  • Loading branch information
igprad authored Oct 13, 2023
1 parent 950be4b commit f6b85c9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 19 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions include/coingecko.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include <iostream>

#include <curl/curl.h>
#include <dpp/dpp.h>

#include <cstdlib>
#include <iostream>

#include "nlohmann/json.hpp"

#include <dpp/dpp.h>

namespace gecko {
void fetch(dpp::slashcommand_t event);
}

void fetch_tokens(dpp::slashcommand_t event);
void fetch_price(dpp::slashcommand_t event);

} // namespace gecko
48 changes: 47 additions & 1 deletion src/coingecko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(event.get_parameter("coingecko_id"));
Expand Down Expand Up @@ -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);
}
23 changes: 16 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<struct register_bot_commands>()) {
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);
}
});
Expand Down

0 comments on commit f6b85c9

Please sign in to comment.