Compare commits
6 Commits
ba2ad34d6e
...
main
Author | SHA1 | Date | |
---|---|---|---|
14d98194b3 | |||
6ef969edb6 | |||
8dda053dcf | |||
3c4aa768ec | |||
28253e423c | |||
09abd6c116 |
@@ -4,6 +4,7 @@ cmake_minimum_required(VERSION 3.22)
|
|||||||
project(TheBartender VERSION 0.3)
|
project(TheBartender VERSION 0.3)
|
||||||
|
|
||||||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
||||||
|
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
|
||||||
|
|
||||||
# Create an executable
|
# Create an executable
|
||||||
add_executable(${PROJECT_NAME} src/Base/Entry.cpp src/Commands.cpp
|
add_executable(${PROJECT_NAME} src/Base/Entry.cpp src/Commands.cpp
|
||||||
@@ -14,7 +15,8 @@ find_package(DPP REQUIRED)
|
|||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(SQLITE3 REQUIRED sqlite3)
|
pkg_check_modules(SQLITE3 REQUIRED sqlite3)
|
||||||
pkg_check_modules(CAIRO REQUIRED cairo)
|
pkg_check_modules(CAIRO REQUIRED cairo)
|
||||||
pkg_check_modules(WEBP REQUIRED libwebpdecoder)
|
pkg_check_modules(STB REQUIRED stb)
|
||||||
|
pkg_check_modules(WEBP REQUIRED libwebp)
|
||||||
|
|
||||||
# Link the pre-installed DPP package.
|
# Link the pre-installed DPP package.
|
||||||
target_link_libraries(${PROJECT_NAME} ${DPP_LIBRARIES} ${SQLITE3_LIBRARIES}
|
target_link_libraries(${PROJECT_NAME} ${DPP_LIBRARIES} ${SQLITE3_LIBRARIES}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
// Bot info
|
// Bot info
|
||||||
#define BOT_NAME "The Bartender Bot"
|
#define BOT_NAME "The Bartender Bot"
|
||||||
#define BOT_VERSION "v0.4"
|
#define BOT_VERSION "v0.4-1"
|
||||||
#define CURRENCY_NAME "Night Coin"
|
#define CURRENCY_NAME "Night Coin"
|
||||||
|
|
||||||
// Buttons
|
// Buttons
|
||||||
@@ -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,14 @@
|
|||||||
"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_FAIL_INSUFFICIENT_BALANCE(amount) \
|
||||||
|
"You do not have " + amount + " " CURRENCY_NAME "(s) to afford this."
|
||||||
#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). Are 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 \
|
||||||
@@ -61,7 +64,9 @@
|
|||||||
"## " BOT_NAME " " BOT_VERSION \
|
"## " BOT_NAME " " BOT_VERSION \
|
||||||
"\n-> Written by <@!607952795794145281>.\n-> Source code " \
|
"\n-> Written by <@!607952795794145281>.\n-> Source code " \
|
||||||
"https://git.thenight.club/cat/BartenderBot.\n-> Made with " \
|
"https://git.thenight.club/cat/BartenderBot.\n-> Made with " \
|
||||||
"[D++](<https://dpp.dev/>) and tears."
|
"[D++](<https://dpp.dev/>), [cairo](<https://www.cairographics.org/>), " \
|
||||||
|
"[stb](<https://github.com/nothings/stb>), " \
|
||||||
|
"[libwebp](<https://chromium.googlesource.com/webm/libwebp>), and tears."
|
||||||
|
|
||||||
// Command balance/bal
|
// Command balance/bal
|
||||||
#define COMMAND_BALANCE_DESCRIPTION "See someone's balance of " CURRENCY_NAME
|
#define COMMAND_BALANCE_DESCRIPTION "See someone's balance of " CURRENCY_NAME
|
||||||
|
@@ -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); }
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#include "../Common.hpp"
|
#include "../Common.hpp"
|
||||||
#include "../Utility/CairoTools.hpp"
|
#include "../Utility/CairoTools.hpp"
|
||||||
|
#include "TransactionMethods.hpp"
|
||||||
|
|
||||||
#include <dpp/dispatcher.h>
|
#include <dpp/dispatcher.h>
|
||||||
#include <dpp/dpp.h>
|
#include <dpp/dpp.h>
|
||||||
@@ -11,6 +12,16 @@
|
|||||||
void commandGenerateReport(const dpp::slashcommand_t &event,
|
void commandGenerateReport(const dpp::slashcommand_t &event,
|
||||||
dpp::cluster &bot) {
|
dpp::cluster &bot) {
|
||||||
event.thinking();
|
event.thinking();
|
||||||
|
auto issuer = event.command.get_issuing_user().id;
|
||||||
|
// Insufficient balance
|
||||||
|
if (!checkFromUsersBalance(issuer, COMMAND_GENERATE_REPORT_COST)) {
|
||||||
|
event.edit_response(COMMAND_GENERATE_REPORT_FAIL_INSUFFICIENT_BALANCE(
|
||||||
|
std::to_string(COMMAND_GENERATE_REPORT_COST)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
deductFromUsersBalance(issuer, COMMAND_GENERATE_REPORT_COST);
|
||||||
|
|
||||||
|
// Start processing
|
||||||
dpp::snowflake fileId =
|
dpp::snowflake fileId =
|
||||||
std::get<dpp::snowflake>(event.get_parameter("image"));
|
std::get<dpp::snowflake>(event.get_parameter("image"));
|
||||||
dpp::attachment file = event.command.get_resolved_attachment(fileId);
|
dpp::attachment file = event.command.get_resolved_attachment(fileId);
|
||||||
@@ -27,7 +38,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()
|
||||||
|
@@ -34,12 +34,13 @@ void commandPay(const dpp::slashcommand_t &event, dpp::cluster &bot) {
|
|||||||
std::get<dpp::snowflake>(event.get_parameter("recipient")).str();
|
std::get<dpp::snowflake>(event.get_parameter("recipient")).str();
|
||||||
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
||||||
|
|
||||||
// See if we can deduct the payment first
|
// Insufficient balance
|
||||||
if (!deductFromUsersBalance(event.command.get_issuing_user().id, amount)) {
|
if (!checkFromUsersBalance(ADMIN_ID, amount)) {
|
||||||
event.reply(COMMAND_PAY_FAIL_INSUFFICIENT_AMOUNT(recipient,
|
event.reply(COMMAND_PAY_FAIL_INSUFFICIENT_AMOUNT(recipient,
|
||||||
std::to_string(amount)));
|
std::to_string(amount)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
deductFromUsersBalance(ADMIN_ID, amount);
|
||||||
|
|
||||||
// Lets pay them
|
// Lets pay them
|
||||||
increaseFromUsersBalance(recipient, amount);
|
increaseFromUsersBalance(recipient, amount);
|
||||||
@@ -68,12 +69,13 @@ void commandBurnMoney(const dpp::slashcommand_t &event, dpp::cluster &bot) {
|
|||||||
}
|
}
|
||||||
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
||||||
|
|
||||||
// Insufficient in balance
|
// Insufficient balance
|
||||||
if (!deductFromUsersBalance(ADMIN_ID, amount)) {
|
if (!checkFromUsersBalance(ADMIN_ID, amount)) {
|
||||||
event.reply(COMMAND_BURN_FAIL_INSUFFICIENT_AMOUNT(std::to_string(amount)));
|
event.reply(COMMAND_BURN_FAIL_INSUFFICIENT_AMOUNT(std::to_string(amount)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deductFromUsersBalance(ADMIN_ID, amount);
|
||||||
event.reply(
|
event.reply(
|
||||||
COMMAND_BURN_SUCCESS(std::to_string(ADMIN_ID), std::to_string(amount)));
|
COMMAND_BURN_SUCCESS(std::to_string(ADMIN_ID), std::to_string(amount)));
|
||||||
}
|
}
|
||||||
@@ -122,16 +124,15 @@ void increaseFromUsersBalance(const dpp::snowflake userid,
|
|||||||
" WHERE UID=" + userid.str());
|
" WHERE UID=" + userid.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount) {
|
void deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount) {
|
||||||
std::uint64_t balance = std::stoll(getUserBalance(userid));
|
std::uint64_t balance = std::stoll(getUserBalance(userid));
|
||||||
if (balance < amount) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
execSQL("UPDATE MONEY SET CASH=" + std::to_string(balance - amount) +
|
execSQL("UPDATE MONEY SET CASH=" + std::to_string(balance - amount) +
|
||||||
" WHERE UID=" + userid.str());
|
" WHERE UID=" + userid.str());
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
bool checkFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount) {
|
||||||
|
std::uint64_t balance = std::stoll(getUserBalance(userid));
|
||||||
|
return balance >= amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getUserBalance(const dpp::snowflake userid) {
|
std::string getUserBalance(const dpp::snowflake userid) {
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
// Helper methods
|
// Helper methods
|
||||||
void increaseFromUsersBalance(const dpp::snowflake userid,
|
void increaseFromUsersBalance(const dpp::snowflake userid,
|
||||||
std::uint64_t amount);
|
std::uint64_t amount);
|
||||||
bool deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount);
|
void deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount);
|
||||||
|
bool checkFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount);
|
||||||
std::string getUserBalance(const dpp::snowflake userid);
|
std::string getUserBalance(const dpp::snowflake userid);
|
||||||
|
|
||||||
void addUserToDatabase(const dpp::snowflake userid);
|
void addUserToDatabase(const dpp::snowflake userid);
|
||||||
|
Reference in New Issue
Block a user