From 1a614ea8e8e0253b1fec949d6991c2a39d5496f5 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 4 Nov 2023 12:23:07 +0100 Subject: [PATCH] Created the add.cpp command --- src/commands/add.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/commands/add.cpp diff --git a/src/commands/add.cpp b/src/commands/add.cpp new file mode 100644 index 0000000..2a77ebb --- /dev/null +++ b/src/commands/add.cpp @@ -0,0 +1,32 @@ +#include "add.h" +#include "api/util.h" +#include + +using namespace sql; +using json = nlohmann::json; + +void add::execute(sql::Connection &con, dpp::cluster &bot, const dpp::slashcommand_t &event) { + ResultSet *res = util::getResultSet(con, "SELECT * FROM channels WHERE guildId = ? AND channelId = ?", + {event.command.guild_id.str(), event.command.channel_id.str()}); + if (res->next()) { + util::sendError(event, "This channel is already registered."); + return; + } + + dpp::webhook newWebhook = dpp::webhook(); + newWebhook.name = "Sheepstar"; + newWebhook.channel_id = event.command.channel_id; + + bot.create_webhook(newWebhook, [&con, event](const dpp::confirmation_callback_t &res) { + if (res.is_error()) { + util::sendError(event, "An error occurred while creating the webhook."); + return; + } + + std::string url = json::parse(res.http_info.body)["url"].get(); + util::executeQuery(con, "INSERT INTO channels (guildId, channelId, webhookToken, cachedName) VALUES (?, ?, ?, ?)", + {event.command.guild_id.str(), event.command.channel_id.str(), url, event.command.channel.name}); + + util::sendSuccess(event, "This channel has been registered."); + }); +}