From 99d1a1563676c2bb95574fb079a9367fbd1acf34 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Mon, 9 Dec 2024 19:49:21 +0500 Subject: feat: random markov responses! --- bot/src/handlers.cpp | 41 ++++++++++++++++++++++++++++++++--------- bot/src/schemas/channel.hpp | 2 +- 2 files changed, 33 insertions(+), 10 deletions(-) (limited to 'bot/src') diff --git a/bot/src/handlers.cpp b/bot/src/handlers.cpp index 38ff28b..c57edad 100644 --- a/bot/src/handlers.cpp +++ b/bot/src/handlers.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -98,25 +99,47 @@ namespace bot::handlers { const irc::Message &message, const schemas::Channel &channel, const schemas::ChannelPreferences &preference) { - bool is_markov_responses_enabled = + bool are_markov_responses_enabled = std::any_of(preference.get_features().begin(), preference.get_features().end(), [](const int &x) { return (schemas::ChannelFeature)x == schemas::ChannelFeature::MARKOV_RESPONSES; }); - if (!is_markov_responses_enabled) return; + if (!are_markov_responses_enabled) return; + + bool are_random_markov_responses_enabled = + std::any_of(preference.get_features().begin(), + preference.get_features().end(), [](const int &x) { + return (schemas::ChannelFeature)x == + schemas::ChannelFeature::RANDOM_MARKOV_RESPONSES; + }); std::string prefix = "@" + bundle.irc_client.get_bot_username() + ","; + bool intended_call = message.message.substr(0, prefix.length()) == prefix; + + int random = -1; + std::string question; + + if (are_random_markov_responses_enabled && !intended_call) { + std::random_device dev; + std::mt19937 rng(dev()); + + std::uniform_int_distribution dist(0, 10); + random = dist(rng); + + if (random != 0) return; + question = message.message; + } else { + question = + message.message.substr(prefix.length(), message.message.length()); + } - if (message.message.substr(0, prefix.length()) != prefix) return; + if (random == -1 && !intended_call) return; - cpr::Response response = cpr::Post( - cpr::Url{"https://markov.ilotterytea.kz/api/v1/generate"}, - cpr::Multipart{ - {"question", - message.message.substr(prefix.length(), message.message.length())}, - {"max_length", 200}}); + cpr::Response response = + cpr::Post(cpr::Url{"https://markov.ilotterytea.kz/api/v1/generate"}, + cpr::Multipart{{"question", question}, {"max_length", 200}}); if (response.status_code != 200) return; diff --git a/bot/src/schemas/channel.hpp b/bot/src/schemas/channel.hpp index e346b9f..ace4c4d 100644 --- a/bot/src/schemas/channel.hpp +++ b/bot/src/schemas/channel.hpp @@ -47,7 +47,7 @@ namespace bot::schemas { std::optional opted_out_at; }; - enum ChannelFeature { MARKOV_RESPONSES }; + enum ChannelFeature { MARKOV_RESPONSES, RANDOM_MARKOV_RESPONSES }; class ChannelPreferences { public: -- cgit v1.2.3