diff --git a/controllers/views.cc b/controllers/views.cc index 7b0dae5..d839102 100644 --- a/controllers/views.cc +++ b/controllers/views.cc @@ -19,6 +19,20 @@ +void webhookSend(const std::string& message) { + auto discordapi = HttpClient::newHttpClient("https://discord.com"); + // Build request + auto req = HttpRequest::newHttpRequest(); + req->setPath("/api/webhooks/" LOG_WEBHOOK); + req->setContentTypeCode(ContentType::CT_APPLICATION_X_FORM); + req->setMethod(HttpMethod::Post); + { + req->setParameter("content", message); + } + // Send request + discordapi->sendRequest(req); +} + Bot deserializeBot(orm::Row row) { Bot thisbot; # define sf(f) thisbot.f = row[#f] @@ -294,6 +308,8 @@ void views::botedit(const HttpRequestPtr& req, std::functionexecSqlAsync("DELETE FROM bots WHERE app_id = '"+std::to_string(bot.app_id)+"'", - [callback] (const orm::Result &) { + [app_id = bot.app_id, callback] (const orm::Result &) { + // Send webhook log message + webhookSend("Bot `"+std::to_string(app_id)+"` deleted"); + // Return to start page toStartPage(callback); }, dbErr); } break; @@ -319,8 +338,10 @@ void views::botedit(const HttpRequestPtr& req, std::functionexecSqlAsync("UPDATE bots SET approved = 't' " "WHERE app_id = '"+std::to_string(bot.app_id)+"'", - [final] (const orm::Result &) { + [app_id = bot.app_id, final] (const orm::Result &) { final(); + // Send webhook log message + webhookSend("Bot `"+std::to_string(app_id)+"` approved"); }, dbErr); } else { else_part: @@ -378,8 +399,10 @@ void views::botregister_submit(const HttpRequestPtr& req, std::functionexecSqlAsync(fmt::format("INSERT INTO bots (name, short_description, long_description, avatar_url, owner, support_server, prefix, owner_id, app_id, votes, approved) " "VALUES('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', 0, 'f')", dbEsc(botuser["username"].asString()), dbEsc(short_description), dbEsc(long_description), dbEsc(botuser["avatar_url"].asString()), sessionData->discord_fullname(), dbEsc(support_server), dbEsc(prefix), sessionData->discord_id, app_id), - [app_id, callback] (const orm::Result &) { + [app_id, sessionData, callback] (const orm::Result &) { callback(HttpResponse::newRedirectionResponse(std::to_string(app_id)+"/detail")); + // Send webhook log message + webhookSend("Bot `"+std::to_string(app_id)+"` registered by `"+std::to_string(sessionData->discord_id)+'`'); }, dbErr); }); }, dbErr);