Add /gemini command
This commit is contained in:
@@ -12,11 +12,14 @@ If DPP is installed in a different location you can specify the root directory t
|
|||||||
cmake .. -DDPP_ROOT_DIR=<your-path>
|
cmake .. -DDPP_ROOT_DIR=<your-path>
|
||||||
```
|
```
|
||||||
## Running the bot
|
## 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
|
```json
|
||||||
{ "token": "your bot token here" }
|
{
|
||||||
|
"token": "your bot token here",
|
||||||
|
"gemini": "your gemini api key here"
|
||||||
|
}
|
||||||
```
|
```
|
||||||
Start the bot:
|
Start the bot :
|
||||||
```bash
|
```bash
|
||||||
cd build
|
cd build
|
||||||
./airi
|
./airi
|
||||||
|
|||||||
50
src/main.cpp
50
src/main.cpp
@@ -8,13 +8,58 @@ int main(int argc, char const *argv[]) {
|
|||||||
configfile >> configdocument;
|
configfile >> configdocument;
|
||||||
|
|
||||||
const std::string BOT_TOKEN = configdocument["token"];
|
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);
|
dpp::cluster bot(BOT_TOKEN, dpp::i_default_intents | dpp::i_guild_members);
|
||||||
|
|
||||||
bot.on_log(dpp::utility::cout_logger());
|
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") {
|
if (event.command.get_command_name() == "ping") {
|
||||||
event.reply("Pong!");
|
event.reply("Pong!");
|
||||||
|
return;
|
||||||
|
} else if (event.command.get_command_name() == "gemini") {
|
||||||
|
std::string prompt = std::get<std::string>(
|
||||||
|
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) {
|
bot.on_ready([&bot](const dpp::ready_t& event) {
|
||||||
if (dpp::run_once<struct register_bot_commands>()) {
|
if (dpp::run_once<struct register_bot_commands>()) {
|
||||||
bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));
|
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)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user