428 lines
22 KiB
C++
428 lines
22 KiB
C++
// Discord bot library - provides all Discord API functionality
|
|
#include "../DPP/include/dpp/dpp.h"
|
|
|
|
// Standard input/output - for console logging (std::cout, std::cerr)
|
|
#include <iostream>
|
|
|
|
// String handling - for std::string class and std::to_string() function
|
|
#include <string>
|
|
|
|
// Terminal I/O control - for hiding password input in getHiddenInput()
|
|
#include <termios.h>
|
|
|
|
// Unix standard definitions - provides STDIN_FILENO constant
|
|
#include <unistd.h>
|
|
|
|
// Time functions - for std::time() to generate timestamps
|
|
#include <ctime>
|
|
|
|
// Dynamic arrays - for std::vector to hold image byte data
|
|
#include <vector>
|
|
|
|
// Threading support - for std::thread to send images asynchronously
|
|
#include <thread>
|
|
|
|
// Time utilities - for std::chrono::seconds() delays
|
|
#include <chrono>
|
|
|
|
// Algorithm functions - for std::transform (string case conversion)
|
|
#include <algorithm>
|
|
|
|
// STB Image library - single header for loading images (JPEG, PNG, etc.)
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "stb_image.h"
|
|
|
|
// STB Image Write library - single header for saving images
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
#include "stb_image_write.h"
|
|
|
|
// Embedded image data - contains the JPEG as a byte array (our generated header)
|
|
#include "embedded_image.h"
|
|
|
|
/**
|
|
* Function to securely input text without displaying it on screen (like password input)
|
|
* This is used for Discord bot tokens to keep them private in the terminal
|
|
*
|
|
* @param prompt - The text to display before asking for input
|
|
* @return std::string - The hidden text that was typed
|
|
*/
|
|
std::string getHiddenInput(const std::string& prompt) {
|
|
// Display the prompt to the user
|
|
std::cout << prompt;
|
|
std::cout.flush(); // Force immediate output (don't wait for newline)
|
|
|
|
// Save current terminal settings so we can restore them later
|
|
termios oldt;
|
|
tcgetattr(STDIN_FILENO, &oldt); // Get current terminal attributes
|
|
|
|
// Create new terminal settings that disable echo (hide typed characters)
|
|
termios newt = oldt; // Copy current settings
|
|
newt.c_lflag &= ~ECHO; // Remove the ECHO flag (disables character display)
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // Apply new settings immediately
|
|
|
|
// Read the hidden input
|
|
std::string input;
|
|
std::getline(std::cin, input); // Read entire line (including spaces)
|
|
|
|
// Restore original terminal settings (re-enable echo)
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
|
std::cout << std::endl; // Add newline since user's Enter wasn't displayed
|
|
|
|
return input;
|
|
}
|
|
|
|
/**
|
|
* Function to vertically flip JPEG image data using STB libraries (upside-down)
|
|
*
|
|
* @param image_data - The JPEG data as bytes
|
|
* @return std::vector<uint8_t> - The vertically flipped JPEG image data
|
|
*/
|
|
std::vector<uint8_t> flipImageVertically(const std::vector<uint8_t>& image_data) {
|
|
std::cout << "📸 Flipping image (" << image_data.size() << " bytes)..." << std::endl;
|
|
|
|
// Step 1: Decode JPEG to raw pixel data
|
|
int width, height, channels;
|
|
unsigned char* decoded_data = stbi_load_from_memory(
|
|
image_data.data(), // Input JPEG data
|
|
image_data.size(), // Size of input data
|
|
&width, // Output: image width
|
|
&height, // Output: image height
|
|
&channels, // Output: number of color channels
|
|
0 // Desired channels (0 = keep original)
|
|
);
|
|
|
|
if (!decoded_data) {
|
|
std::cout << "❌ Failed to decode JPEG image: " << stbi_failure_reason() << std::endl;
|
|
return image_data; // Return original if decode fails
|
|
}
|
|
|
|
std::cout << "✅ Decoded image: " << width << "x" << height << " pixels, " << channels << " channels" << std::endl;
|
|
|
|
// Step 2: Create buffer for flipped image data
|
|
int bytes_per_pixel = channels;
|
|
int row_size = width * bytes_per_pixel;
|
|
std::vector<unsigned char> flipped_pixels(width * height * bytes_per_pixel);
|
|
|
|
// Step 3: Flip vertically (reverse row order to make image upside-down)
|
|
for (int y = 0; y < height; y++) {
|
|
for (int x = 0; x < width; x++) {
|
|
// Calculate source and destination pixel positions
|
|
int src_index = (y * width + x) * bytes_per_pixel; // Original position
|
|
int dst_index = ((height - 1 - y) * width + x) * bytes_per_pixel; // Flipped position (vertically)
|
|
|
|
// Copy all color channels for this pixel
|
|
for (int c = 0; c < bytes_per_pixel; c++) {
|
|
flipped_pixels[dst_index + c] = decoded_data[src_index + c];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Step 4: Encode flipped pixels back to JPEG
|
|
std::vector<uint8_t> output_jpeg;
|
|
|
|
// Helper function to capture JPEG data written by stb_image_write
|
|
// This is a "callback" - STB calls this function whenever it has encoded data to write
|
|
auto write_func = [](void* context, void* data, int size) {
|
|
// Cast void* back to our vector (context is the &output_jpeg from below)
|
|
std::vector<uint8_t>* output = static_cast<std::vector<uint8_t>*>(context);
|
|
uint8_t* bytes = static_cast<uint8_t*>(data);
|
|
// Append the new JPEG bytes to our output vector
|
|
output->insert(output->end(), bytes, bytes + size);
|
|
};
|
|
|
|
// Write flipped pixels to JPEG format (quality: 90/100)
|
|
int success = stbi_write_jpg_to_func(
|
|
write_func, // Callback function to capture data
|
|
&output_jpeg, // Context passed to callback
|
|
width, // Image width
|
|
height, // Image height
|
|
channels, // Number of channels
|
|
flipped_pixels.data(), // Pixel data
|
|
90 // JPEG quality (0-100)
|
|
);
|
|
|
|
// Clean up decoded image memory
|
|
stbi_image_free(decoded_data);
|
|
|
|
if (!success) {
|
|
std::cout << "❌ Failed to encode flipped image to JPEG" << std::endl;
|
|
return image_data; // Return original if encode fails
|
|
}
|
|
return output_jpeg;
|
|
}
|
|
|
|
/**
|
|
* MAIN FUNCTION - Entry point of the Discord bot program
|
|
* Sets up the bot, registers commands, and starts the event loop
|
|
*/
|
|
int main() {
|
|
// Step 1: Get Discord bot token securely from user
|
|
std::cout << "=== Discord Bot Startup ===" << std::endl;
|
|
std::string BOT_TOKEN = getHiddenInput("Enter Discord Bot Token: ");
|
|
|
|
// Validate that a token was provided
|
|
if (BOT_TOKEN.empty()) {
|
|
std::cerr << "Error: Bot token cannot be empty!" << std::endl;
|
|
return 1; // Exit with error code
|
|
}
|
|
|
|
// Step 2: Create Discord bot instance
|
|
// dpp::cluster is the main bot class that handles all Discord connections
|
|
// i_default_intents gives basic permissions, i_message_content allows reading message text
|
|
dpp::cluster bot(BOT_TOKEN, dpp::i_default_intents | dpp::i_message_content);
|
|
|
|
// Step 3: Enable logging to see what the bot is doing
|
|
bot.on_log(dpp::utility::cout_logger());
|
|
|
|
// Step 4: Set up event handler for file uploads
|
|
// Lambda function that gets called whenever a message with attachments is posted
|
|
bot.on_message_create([&bot](const dpp::message_create_t& event) {
|
|
// Ignore messages sent by bots (including our own) to prevent loops
|
|
if (event.msg.author.is_bot()) {
|
|
return; // Early exit - don't process bot messages
|
|
}
|
|
|
|
// Check if this message has any file attachments
|
|
if (!event.msg.attachments.empty()) {
|
|
// Debug output to console (only you see this, not Discord users)
|
|
std::cout << "\n=== FILE UPLOAD DETECTED ===" << std::endl;
|
|
std::cout << "User: " << event.msg.author.username << "#" << event.msg.author.discriminator << std::endl;
|
|
std::cout << "Channel ID: " << event.msg.channel_id << std::endl;
|
|
std::cout << "Message ID: " << event.msg.id << std::endl;
|
|
std::cout << "Attachments: " << event.msg.attachments.size() << std::endl;
|
|
|
|
// Flag to track if we found any JPEG files
|
|
bool found_jpeg = false;
|
|
std::vector<dpp::attachment> jpeg_attachments; // Store JPEG attachments for processing
|
|
|
|
// Loop through each attached file
|
|
for (const auto& attachment : event.msg.attachments) {
|
|
// Print file details to console
|
|
std::cout << " - File: " << attachment.filename << std::endl;
|
|
std::cout << " Size: " << attachment.size << " bytes" << std::endl;
|
|
std::cout << " URL: " << attachment.url << std::endl;
|
|
|
|
// Check if filename ends with ".jpg" or ".jpeg" (case-insensitive)
|
|
std::string filename_lower = attachment.filename;
|
|
// Convert entire filename to lowercase for easier comparison
|
|
std::transform(filename_lower.begin(), filename_lower.end(), filename_lower.begin(), ::tolower);
|
|
|
|
// Check if file ends with .jpg (4 chars) or .jpeg (5 chars)
|
|
// substr(length - N) gets the last N characters of the string
|
|
if ((filename_lower.length() >= 4 && filename_lower.substr(filename_lower.length() - 4) == ".jpg") ||
|
|
(filename_lower.length() >= 5 && filename_lower.substr(filename_lower.length() - 5) == ".jpeg")) {
|
|
std::cout << " ✅ JPEG FILE DETECTED!" << std::endl;
|
|
found_jpeg = true;
|
|
jpeg_attachments.push_back(attachment); // Store for processing
|
|
}
|
|
}
|
|
|
|
// If we found JPEG files, process them
|
|
if (found_jpeg) {
|
|
std::cout << "\n🎯 ACTION: Processing JPEG file upload" << std::endl;
|
|
|
|
// Add emoji reaction to the message (like clicking a reaction in Discord)
|
|
bot.message_add_reaction(event.msg.id, event.msg.channel_id, "🔄");
|
|
|
|
// Send a text response in the same channel
|
|
bot.message_create(dpp::message(event.msg.channel_id, "📸 JPEG detected! Flipping image upside-down..."));
|
|
|
|
// Process each JPEG in a separate thread (after 5 second delay)
|
|
// We use threads so the bot doesn't freeze while processing images
|
|
for (const auto& jpeg_attachment : jpeg_attachments) {
|
|
std::thread([&bot, // Capture bot by reference (shared)
|
|
channel_id = event.msg.channel_id, // Copy these values into the thread
|
|
user_id = event.msg.author.id, // so they don't get lost when event ends
|
|
attachment_url = jpeg_attachment.url,
|
|
original_filename = jpeg_attachment.filename
|
|
]() {
|
|
// Wait 5 seconds before processing
|
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
|
|
|
std::cout << "\n--- PROCESSING JPEG IMAGE ---" << std::endl;
|
|
std::cout << "Downloading: " << original_filename << std::endl;
|
|
std::cout << "From URL: " << attachment_url << std::endl;
|
|
|
|
// Download the image using DPP's HTTP client
|
|
std::cout << "🌐 Starting image download..." << std::endl;
|
|
bot.request(attachment_url, dpp::m_get, [&bot, channel_id, user_id, original_filename](const dpp::http_request_completion_t& response) {
|
|
if (response.status == 200) {
|
|
std::cout << "✅ Downloaded image (" << response.body.size() << " bytes)" << std::endl;
|
|
|
|
// Convert response body to vector for processing
|
|
std::vector<uint8_t> original_data(response.body.begin(), response.body.end());
|
|
|
|
// Flip the image vertically (upside-down)
|
|
std::vector<uint8_t> flipped_data = flipImageVertically(original_data);
|
|
|
|
// Create new filename for flipped image
|
|
std::string flipped_filename = "upside_down_" + original_filename;
|
|
|
|
// Create message with flipped image
|
|
std::string message_text = "🔄 Here's your upside-down image! <@" + std::to_string(user_id) + ">";
|
|
dpp::message msg(channel_id, message_text);
|
|
msg.add_file(flipped_filename, std::string(flipped_data.begin(), flipped_data.end()));
|
|
|
|
// Send the flipped image
|
|
bot.message_create(msg, [original_filename](const dpp::confirmation_callback_t& callback) {
|
|
if (callback.is_error()) {
|
|
std::cout << "❌ Failed to send flipped image: " << callback.get_error().message << std::endl;
|
|
} else {
|
|
std::cout << "✅ Successfully sent flipped " << original_filename << "!" << std::endl;
|
|
}
|
|
});
|
|
|
|
} else {
|
|
std::cout << "❌ Failed to download image. Status: " << response.status << std::endl;
|
|
bot.message_create(dpp::message(channel_id, "❌ Failed to download image for processing."));
|
|
}
|
|
});
|
|
|
|
}).detach(); // detach() = thread runs independently, we don't wait for it to finish
|
|
}
|
|
}
|
|
|
|
std::cout << "============================" << std::endl;
|
|
}
|
|
});
|
|
|
|
// Step 5: Set up slash command handler (commands that start with /)
|
|
// [&bot] captures the bot variable by reference so we can use it inside the lambda
|
|
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
|
|
// Generate unique ID for this command execution (for debugging)
|
|
static int command_counter = 0; // static = keeps value between function calls (like a global variable)
|
|
int command_id = ++command_counter; // Pre-increment: add 1 then use value (so first command = 1)
|
|
|
|
// Print detailed debug information about the command
|
|
std::cout << "\n=== COMMAND RECEIVED #" << command_id << " ===" << std::endl;
|
|
std::cout << "Command: " << event.command.get_command_name() << std::endl;
|
|
std::cout << "User: " << event.command.get_issuing_user().username << "#" << event.command.get_issuing_user().discriminator << std::endl;
|
|
std::cout << "User ID: " << event.command.get_issuing_user().id << std::endl;
|
|
std::cout << "Guild ID: " << event.command.guild_id << std::endl;
|
|
std::cout << "Channel ID: " << event.command.channel_id << std::endl;
|
|
std::cout << "Timestamp: " << std::time(nullptr) << std::endl; // Unix timestamp
|
|
std::cout << "Interaction ID: " << event.command.id << std::endl;
|
|
|
|
// Check if this is the command we care about
|
|
if (event.command.get_command_name() == "oomer") {
|
|
std::cout << "Status: EXPECTED COMMAND (Execution #" << command_id << ")" << std::endl;
|
|
std::cout << "========================" << std::endl;
|
|
|
|
// CRITICAL: Must acknowledge Discord interaction within 3 seconds
|
|
// or Discord shows "The application did not respond" error
|
|
std::cout << "Sending interaction acknowledgment for command #" << command_id << "..." << std::endl;
|
|
event.reply("🚀 Render started... (Command #" + std::to_string(command_id) + ")");
|
|
|
|
// Generate unique thread ID for debugging async operations
|
|
static int thread_counter = 0; // Separate counter for threads
|
|
int current_thread_id = ++thread_counter;
|
|
|
|
// Start background thread to send image after delay
|
|
// This prevents blocking the main Discord event loop
|
|
std::thread([&bot, // Capture bot by reference
|
|
channel_id = event.command.channel_id, // Copy channel ID
|
|
user_id = event.command.get_issuing_user().id, // Copy user ID
|
|
thread_id = current_thread_id, // Copy thread ID
|
|
cmd_id = command_id // Copy command ID
|
|
]() {
|
|
// Wait 5 seconds before sending image (simulates processing time)
|
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
|
|
|
std::cout << "\n--- SENDING EMBEDDED IMAGE ---" << std::endl;
|
|
std::cout << "Using embedded JPEG data (" << oomerbot_jpeg_size << " bytes)..." << std::endl;
|
|
|
|
// Create vector from embedded image data (no file I/O needed!)
|
|
// This copies data from the compiled-in byte array to a vector
|
|
std::vector<uint8_t> image_data(oomerbot_jpeg_data, oomerbot_jpeg_data + oomerbot_jpeg_size);
|
|
|
|
// Sanity check - make sure we have image data
|
|
if (!image_data.empty()) {
|
|
// Create message text with user mention (@username becomes clickable)
|
|
std::string message_text = "🎨 Render complete! <@" + std::to_string(user_id) + ">";
|
|
|
|
// Create Discord message object
|
|
dpp::message msg(channel_id, message_text);
|
|
|
|
// Attach the image file to the message
|
|
// First parameter: filename that appears in Discord
|
|
// Second parameter: file data as string
|
|
msg.add_file("oomerbot.jpeg", std::string(image_data.begin(), image_data.end()));
|
|
|
|
// Send the message with image attachment
|
|
// Lambda function handles the response (success or error)
|
|
bot.message_create(msg, [thread_id, cmd_id](const dpp::confirmation_callback_t& callback) {
|
|
if (callback.is_error()) {
|
|
// Something went wrong - print error details
|
|
std::cout << "❌ Thread #" << thread_id << " (command #" << cmd_id << ") failed to send image: " << callback.get_error().message << std::endl;
|
|
std::cout << "Error code: " << callback.get_error().code << std::endl;
|
|
} else {
|
|
// Success!
|
|
std::cout << "✅ Thread #" << thread_id << " (command #" << cmd_id << ") successfully sent embedded oomerbot.jpeg!" << std::endl;
|
|
}
|
|
});
|
|
} else {
|
|
// This should never happen since image is embedded at compile time
|
|
std::cout << "❌ Embedded image data is empty" << std::endl;
|
|
}
|
|
}).detach(); // detach() = thread runs independently, don't wait for it
|
|
|
|
} else {
|
|
// Handle unexpected commands (probably old registrations)
|
|
std::cout << "Status: UNEXPECTED COMMAND (probably old registration) - Command #" << command_id << std::endl;
|
|
std::cout << "========================" << std::endl;
|
|
event.reply("⚠️ This command is no longer supported. Please use `/oomer` instead.");
|
|
}
|
|
});
|
|
|
|
// Step 6: Set up bot ready event (called when bot successfully connects to Discord)
|
|
bot.on_ready([&bot](const dpp::ready_t& event) {
|
|
// run_once ensures this only happens on first connection, not reconnections
|
|
if (dpp::run_once<struct register_bot_commands>()) {
|
|
std::cout << "Bot is ready! Starting command registration..." << std::endl;
|
|
std::cout << "Bot user: " << bot.me.username << " (ID: " << bot.me.id << ")" << std::endl;
|
|
|
|
// Clean up any old slash commands before registering new ones
|
|
std::cout << "Clearing old global commands..." << std::endl;
|
|
bot.global_commands_get([&bot](const dpp::confirmation_callback_t& callback) {
|
|
if (!callback.is_error()) {
|
|
// Get list of existing commands
|
|
auto commands = std::get<dpp::slashcommand_map>(callback.value);
|
|
std::cout << "Found " << commands.size() << " existing commands" << std::endl;
|
|
|
|
// Delete each old command
|
|
for (auto& command : commands) {
|
|
std::cout << "Deleting old command: " << command.second.name << std::endl;
|
|
bot.global_command_delete(command.first, [](const dpp::confirmation_callback_t& del_callback) {
|
|
if (del_callback.is_error()) {
|
|
std::cout << "❌ Failed to delete command: " << del_callback.get_error().message << std::endl;
|
|
} else {
|
|
std::cout << "✅ Command deleted successfully" << std::endl;
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
std::cout << "❌ Failed to get existing commands: " << callback.get_error().message << std::endl;
|
|
}
|
|
|
|
// Register our new slash command
|
|
std::cout << "Registering oomer command..." << std::endl;
|
|
// Parameters: command_name, description, bot_id
|
|
bot.global_command_create(dpp::slashcommand("oomer", "render oj", bot.me.id), [](const dpp::confirmation_callback_t& reg_callback) {
|
|
if (reg_callback.is_error()) {
|
|
std::cout << "❌ Failed to register command: " << reg_callback.get_error().message << std::endl;
|
|
} else {
|
|
std::cout << "✅ Command registered successfully!" << std::endl;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
// Step 7: Start the bot and run forever
|
|
// st_wait means the program will not exit, it will keep running until interrupted
|
|
std::cout << "Starting bot event loop..." << std::endl;
|
|
bot.start(dpp::st_wait);
|
|
|
|
// This line should never be reached unless the bot shuts down
|
|
return 0;
|
|
} |