Added component response for generate_report
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
"Upload a background image to use (Only supports PNG, JPEG, and WEBP files)"
|
"Upload a background image to use (Only supports PNG, JPEG, and WEBP files)"
|
||||||
#define COMMAND_GENERATE_REPORT_ARGS_HEADLINE_DESCRIPTION \
|
#define COMMAND_GENERATE_REPORT_ARGS_HEADLINE_DESCRIPTION \
|
||||||
"What is the headline?"
|
"What is the headline?"
|
||||||
|
|
||||||
#define COMMAND_GENERATE_REPORT_FAIL_NOT_SUPPORTED(filetype) \
|
#define COMMAND_GENERATE_REPORT_FAIL_NOT_SUPPORTED(filetype) \
|
||||||
"File type: " + filetype + \
|
"File type: " + filetype + \
|
||||||
" is not supported. Only PNG, JPEG, and WEBP are allowed!"
|
" is not supported. Only PNG, JPEG, and WEBP are allowed!"
|
||||||
@@ -31,12 +32,13 @@
|
|||||||
"Failed to download the background image! Aborting." RESPONSE_NO_CHARGE
|
"Failed to download the background image! Aborting." RESPONSE_NO_CHARGE
|
||||||
#define COMMAND_GENERATE_REPORT_FAIL_IMAGE_LOAD \
|
#define COMMAND_GENERATE_REPORT_FAIL_IMAGE_LOAD \
|
||||||
"Failed to load the background image! Aborting." RESPONSE_NO_CHARGE
|
"Failed to load the background image! Aborting." RESPONSE_NO_CHARGE
|
||||||
|
|
||||||
#define COMMAND_GENERATE_REPORT_CONFIRMATION_QUESTION(price, imageURL, \
|
#define COMMAND_GENERATE_REPORT_CONFIRMATION_QUESTION(price, imageURL, \
|
||||||
headline) \
|
imageMIME, headline) \
|
||||||
"This image generation will cost you " + price + \
|
"This image generation will cost you " + price + \
|
||||||
" " CURRENCY_NAME \
|
" " CURRENCY_NAME \
|
||||||
"(s).\nAre you sure you want to continue?\nHeadline: " + \
|
"(s).\nAre you sure you want to continue?\nHeadline: " + \
|
||||||
headline + " [image](" + imageURL + ")"
|
headline + "\n[" + imageMIME + "](" + imageURL + ")"
|
||||||
|
|
||||||
// Command get_pfp
|
// Command get_pfp
|
||||||
#define COMMAND_GET_PFP_DESCRIPTION \
|
#define COMMAND_GET_PFP_DESCRIPTION \
|
||||||
|
@@ -20,4 +20,5 @@ std::unordered_map<std::string, std::function<void(COMMAND_ARGS)>> Commands{
|
|||||||
{COMMAND_GENERATE_REPORT, commandGenerateReport}};
|
{COMMAND_GENERATE_REPORT, commandGenerateReport}};
|
||||||
|
|
||||||
std::unordered_map<std::string, std::function<void(COMPONENT_ARGS)>> Components{
|
std::unordered_map<std::string, std::function<void(COMPONENT_ARGS)>> Components{
|
||||||
{COMPONENT_COMMAND_CANCEL, componentCancel}};
|
{COMPONENT_COMMAND_CANCEL, componentCancel},
|
||||||
|
{COMPONENT_GENERATE_REPORT_CONFIRM, componentGenerateReport}};
|
||||||
|
@@ -1,6 +1,53 @@
|
|||||||
#include "../../Common.hpp"
|
#include "../../Common.hpp"
|
||||||
#include "../../Utility/CairoTools.hpp"
|
#include "../../Utility/CairoTools.hpp"
|
||||||
|
|
||||||
#include <dpp/cluster.h>
|
#include <dpp/cluster.h>
|
||||||
#include <dpp/dispatcher.h>
|
#include <dpp/dispatcher.h>
|
||||||
|
#include <dpp/misc-enum.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void componentGenerateReport(COMPONENT_ARGS) {
|
||||||
|
std::string content = event.command.get_context_message().content;
|
||||||
|
|
||||||
|
// Find headline
|
||||||
|
size_t start = content.find(": ") + 2;
|
||||||
|
std::string headline =
|
||||||
|
content.substr(start, content.find("\n[", start) - start);
|
||||||
|
|
||||||
|
// Find image MIME
|
||||||
|
start = content.find("[") + 1;
|
||||||
|
std::string imageType =
|
||||||
|
content.substr(start, content.find("]", start) - start);
|
||||||
|
|
||||||
|
// Find image link
|
||||||
|
start = content.find("](") + 2;
|
||||||
|
std::string url = content.substr(start, content.find(")", start) - start);
|
||||||
|
|
||||||
|
// Handler
|
||||||
|
auto fileType = supportedImageFileTypes.find(imageType);
|
||||||
|
bot.request(url, dpp::http_method::m_get,
|
||||||
|
[event, &bot, headline,
|
||||||
|
fileType](const dpp::http_request_completion_t &result) {
|
||||||
|
// We might not be able to download it
|
||||||
|
if (result.status != 200) {
|
||||||
|
event.edit_response(
|
||||||
|
COMMAND_GENERATE_REPORT_FAIL_IMAGE_DOWNLOAD);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Its possible that file is corrupted
|
||||||
|
auto imageAsSurface = fileType->second(result.body);
|
||||||
|
if (imageAsSurface == nullptr) {
|
||||||
|
event.edit_response(COMMAND_GENERATE_REPORT_FAIL_IMAGE_LOAD);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string responseData =
|
||||||
|
GenerateReportImage(imageAsSurface, headline);
|
||||||
|
dpp::message response(event.command.channel_id, "");
|
||||||
|
response.add_file("report.png", responseData);
|
||||||
|
event.edit_response(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void componentCancel(COMPONENT_ARGS) { event.edit_response(REQUEST_CANCELLED); }
|
void componentCancel(COMPONENT_ARGS) { event.edit_response(REQUEST_CANCELLED); }
|
||||||
|
@@ -27,7 +27,8 @@ void commandGenerateReport(const dpp::slashcommand_t &event,
|
|||||||
dpp::message confirmRequest(
|
dpp::message confirmRequest(
|
||||||
event.command.channel_id,
|
event.command.channel_id,
|
||||||
COMMAND_GENERATE_REPORT_CONFIRMATION_QUESTION(
|
COMMAND_GENERATE_REPORT_CONFIRMATION_QUESTION(
|
||||||
std::to_string(COMMAND_GENERATE_REPORT_COST), file.url, headline));
|
std::to_string(COMMAND_GENERATE_REPORT_COST), file.url,
|
||||||
|
fileType->first, headline));
|
||||||
|
|
||||||
confirmRequest.add_component(
|
confirmRequest.add_component(
|
||||||
dpp::component()
|
dpp::component()
|
||||||
|
Reference in New Issue
Block a user