Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : Add Coins Command #6

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading