From 8851a396c7546761b4a9ebeaacfaacd05570b3d7 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 29 Jan 2026 00:52:59 +0100 Subject: [PATCH] Add /gemini command --- README.md | 9 ++++++--- src/main.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 47e47f5..ddf5f38 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,14 @@ If DPP is installed in a different location you can specify the root directory t cmake .. -DDPP_ROOT_DIR= ``` ## Running the bot -Create a config.json in the directory above the build directory: +Create a config.json in the directory above the `build` directory : ```json -{ "token": "your bot token here" } +{ + "token": "your bot token here", + "gemini": "your gemini api key here" +} ``` -Start the bot: +Start the bot : ```bash cd build ./airi diff --git a/src/main.cpp b/src/main.cpp index 7eec858..79098a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,13 +8,58 @@ int main(int argc, char const *argv[]) { configfile >> configdocument; const std::string BOT_TOKEN = configdocument["token"]; + const std::string GEMINI_KEY = configdocument["gemini"]; + dpp::cluster bot(BOT_TOKEN, dpp::i_default_intents | dpp::i_guild_members); bot.on_log(dpp::utility::cout_logger()); - bot.on_slashcommand([](const dpp::slashcommand_t& event) { + bot.on_slashcommand([&bot, GEMINI_KEY](const dpp::slashcommand_t& event) { if (event.command.get_command_name() == "ping") { event.reply("Pong!"); + return; + } else if (event.command.get_command_name() == "gemini") { + std::string prompt = std::get( + event.get_parameter("prompt") + ); + json payload = {{ + "contents", json::array({{ + { + "role", "user" + }, + { + "parts", json::array({ + { { "text", prompt } } + }) + } + } + }) + } + }; + std::string postdata = payload.dump(); + event.thinking(); + bot.request("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent", dpp::m_post, + [&bot, event, GEMINI_KEY](const dpp::http_request_completion_t& cc) { + if (cc.status != 200) { + event.edit_original_response( + dpp::message(std::to_string(cc.status)) + ); + return; + } + json response = json::parse(cc.body, nullptr, false); + std::string reply = response["candidates"][0]["content"]["parts"][0]["text"]; + if (reply.size() > 2000) { + reply = reply.substr(0, 2000); + } + event.edit_original_response( + dpp::message(reply) + ); + }, + postdata, + "application/json", { + { "x-goog-api-key", GEMINI_KEY } + } + ); } }); @@ -60,6 +105,9 @@ int main(int argc, char const *argv[]) { bot.on_ready([&bot](const dpp::ready_t& event) { if (dpp::run_once()) { bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id)); + + bot.global_command_create(dpp::slashcommand("gemini", "Ask gemini-2.5-flash.", bot.me.id) + .add_option(dpp::command_option(dpp::co_string, "prompt", "What you want to ask.", true))); } });