From 902f495986f77769a5364a58442b5385a702d077 Mon Sep 17 00:00:00 2001 From: mbn <66748817+mbn-code@users.noreply.github.com> Date: Sun, 12 Jan 2025 14:40:21 +0100 Subject: [PATCH 1/4] hint button implementation added --- .gitignore | 3 +- src/commands/project_cmd.cpp | 51 +++++++++++++++++++++---- src/res/hints.txt | 74 ++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 src/res/hints.txt diff --git a/.gitignore b/.gitignore index 7503717..0cc6ae0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ build # token config.json -/src/config.json \ No newline at end of file +/src/config.json +src/config.json \ No newline at end of file diff --git a/src/commands/project_cmd.cpp b/src/commands/project_cmd.cpp index 8a08412..9d6e2fe 100644 --- a/src/commands/project_cmd.cpp +++ b/src/commands/project_cmd.cpp @@ -1,15 +1,52 @@ #include "commands.h" #include "../globals/globals.h" +static bool buttonClicked = false; + void cmd::projectCommand(dpp::cluster& bot, const dpp::slashcommand_t& event) { - static int index; - const std::string project = cmd::utils::readFileLine("res/project.txt", index); + static int index; + const std::string project = cmd::utils::readFileLine("res/project.txt", index); + + dpp::embed embed = dpp::embed() + .set_color(globals::color::defaultColor) + .add_field("Project Idea", project); + + dpp::message message(event.command.channel_id, embed); + + // Add hint button with index as custom_id + message.add_component( + dpp::component().add_component( + dpp::component() + .set_label("Hint") + .set_type(dpp::cot_button) + .set_style(dpp::cos_primary) + .set_id("hint_button_" + std::to_string(index)) + ) + ); + + event.reply(message); + + // Reset the button click state when the /project command is run + buttonClicked = false; + + bot.on_button_click([&bot](const dpp::button_click_t& event) + { + if (event.custom_id.rfind("hint_button_", 0) == 0 && !buttonClicked) + { + // Extract index from custom_id + int hintIndex = std::stoi(event.custom_id.substr(12)) - 1; + const std::string hint = cmd::utils::readFileLine("res/hints.txt", hintIndex); + + dpp::embed hintEmbed = dpp::embed() + .set_color(globals::color::defaultColor) + .add_field("Hint", hint); - dpp::embed embed = dpp::embed() - .set_color(globals::color::defaultColor) - .add_field(project, ""); + dpp::message hintMessage(event.command.channel_id, hintEmbed); + event.reply(hintMessage); - dpp::message message(event.command.channel_id, embed); - event.reply(message); + buttonClicked = true; + } + } + ); } diff --git a/src/res/hints.txt b/src/res/hints.txt new file mode 100644 index 0000000..0ab1b21 --- /dev/null +++ b/src/res/hints.txt @@ -0,0 +1,74 @@ +Use the `std::cout` stream to print text to the console. +Use a `for` loop to iterate from 1 to 10 and print each number. +Use nested `for` loops to print a square pattern. +Use a `for` loop to iterate from 1 to 10 and accumulate the sum. +Consider using a GUI library like Qt or GTK. +Use a `for` loop to print your name multiple times. +Use a `for` loop with a step of 2 to print even numbers. +Use a `for` loop to iterate from 10 to 1 and print each number. +Use a `for` loop to iterate through ASCII values of letters. +Use nested `for` loops to print a triangle pattern. +Use a `for` loop to multiply numbers from 1 to the given number. +Use the modulus operator `%` to check for even or odd. +Use nested `for` loops to print the multiplication table. +Use the Euclidean algorithm to find the GCD. +Convert the number to a string and check if it reads the same backward. +Use a loop or recursion to generate Fibonacci numbers. +Use the formula `F = C * 9/5 + 32` for conversion. +Use a loop to iterate through the string and count vowels. +Use the `std::reverse` function or a loop to reverse the string. +Use a loop to iterate through the array and find the maximum value. +Use nested loops to iterate through matrices and add corresponding elements. +Implement the bubble sort algorithm to sort the array. +Use a loop to check if the number has any divisors other than 1 and itself. +Use `switch` statements to perform different arithmetic operations. +Use the `rand()` function and `srand()` to generate random numbers. +Sort both words and compare if they are equal. +Use the `rand()` function to generate random numbers between 1 and 6. +Use a loop and a helper function to check for prime numbers. +Use nested `for` loops to print a rectangle pattern. +Use a loop to extract and sum each digit of the number. +Use the formula `area = π * r^2` for calculation. +Combine parts of the first and last name to create a username. +Use a loop to prompt the user to guess a number until they get it right. +Use nested loops to iterate through and print each element of the array. +Use a loop and `sleep` function to create a timer. +Use a loop to iterate through the string and count occurrences. +Use nested loops and the binomial coefficient formula to print Pascal’s triangle. +Use a loop or the `pow` function to calculate the power. +Use the `rand()` function to generate random numbers for heads or tails. +Implement the insertion sort algorithm to sort the array. +Use a loop to convert binary digits to a decimal number. +Use a list or array to store and manage to-do items. +Use the leap year rules to check if a year is a leap year. +Use a loop to iterate through the series and calculate the sum. +Use a loop to prompt for username and password and check against stored values. +Use a loop to convert a decimal number to binary digits. +Use nested loops to print a diamond pattern. +Use a loop to find the largest and second largest numbers. +Compare the string with its reverse to check for palindrome. +Use a loop to shift each character by a fixed number of positions. +Use a loop to iterate through the array and calculate the sum. +Use `rand()` to generate computer choices and compare with user input. +Use the formula `A = P(1 + r/n)^(nt)` for calculation. +Use a list or array to store and manage inventory items. +Use nested loops to print the border of a square. +Use the `time` library to get and display the current date and time. +Use nested loops and careful indexing to print a spiral pattern. +Use a loop to sum the numbers and divide by the count. +Use a loop to find the maximum and minimum values. +Use a loop to check if the sum of the cubes of digits equals the number. +Use a 2D array to represent the board and implement game logic. +Use a loop to iterate through the sentence and count words. +Use a loop and `sleep` function to create a stopwatch. +Use a loop to iterate through the string and print ASCII values. +Use a loop to find and print all divisors of the number. +Use a loop to convert hexadecimal digits to a decimal number. +Use nested loops to print multiplication tables from 1 to 10. +Use the formula `LCM(a, b) = abs(a*b) / GCD(a, b)` for calculation. +Use a loop to check if each character is a digit. +Use a loop to prompt the user to guess a number until they get it right. +Use a loop to prompt the user to guess letters and reveal the word. +Use a map or dictionary to count and store word occurrences. +Use recursion or the `std::next_permutation` function to generate permutations. +Use a loop and `switch` statements to create a menu system. From 84d01f19cae56072780076b48d09f8c7ca932379 Mon Sep 17 00:00:00 2001 From: mbn <66748817+mbn-code@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:46:46 +0100 Subject: [PATCH 2/4] hint button update. hint button remade to support json file with project idea and hint. --- src/commands/project_cmd.cpp | 133 ++++++++++++++++++---------- src/res/project.json | 166 +++++++++++++++++++++++++++++++++++ src/res/project.txt | 74 ---------------- 3 files changed, 255 insertions(+), 118 deletions(-) create mode 100644 src/res/project.json delete mode 100644 src/res/project.txt diff --git a/src/commands/project_cmd.cpp b/src/commands/project_cmd.cpp index 9d6e2fe..00483f5 100644 --- a/src/commands/project_cmd.cpp +++ b/src/commands/project_cmd.cpp @@ -1,52 +1,97 @@ #include "commands.h" #include "../globals/globals.h" +#include "nlohmann/json.hpp" + +using json = nlohmann::json; + +static bool buttonClicked = false; static bool buttonClicked = false; void cmd::projectCommand(dpp::cluster& bot, const dpp::slashcommand_t& event) { - static int index; - const std::string project = cmd::utils::readFileLine("res/project.txt", index); - - dpp::embed embed = dpp::embed() - .set_color(globals::color::defaultColor) - .add_field("Project Idea", project); - - dpp::message message(event.command.channel_id, embed); - - // Add hint button with index as custom_id - message.add_component( - dpp::component().add_component( - dpp::component() - .set_label("Hint") - .set_type(dpp::cot_button) - .set_style(dpp::cos_primary) - .set_id("hint_button_" + std::to_string(index)) - ) - ); - - event.reply(message); - - // Reset the button click state when the /project command is run - buttonClicked = false; - - bot.on_button_click([&bot](const dpp::button_click_t& event) - { - if (event.custom_id.rfind("hint_button_", 0) == 0 && !buttonClicked) - { - // Extract index from custom_id - int hintIndex = std::stoi(event.custom_id.substr(12)) - 1; - const std::string hint = cmd::utils::readFileLine("res/hints.txt", hintIndex); - - dpp::embed hintEmbed = dpp::embed() - .set_color(globals::color::defaultColor) - .add_field("Hint", hint); - - dpp::message hintMessage(event.command.channel_id, hintEmbed); - event.reply(hintMessage); - - buttonClicked = true; - } - } - ); + static int index = -1; + index++; + + std::ifstream projectFile("res/project.json"); + if (!projectFile.is_open()) { + event.reply("Failed to open project file."); + return; + } + + json data; + try { + projectFile >> data; + } catch (const json::parse_error& e) { + event.reply("Failed to parse project file."); + return; + } + + if (!data.contains("projects") || !data["projects"].is_array() || index >= data["projects"].size()) { + event.reply("Invalid project data or index out of bounds."); + return; + } + + const auto& project = data["projects"][index]; + if (!project.contains("title") || !project.contains("description")) { + event.reply("Project data is missing required fields."); + return; + } + + const std::string projectTitle = project["title"]; + const std::string projectDescription = project["description"]; + const std::string projectHint = project.contains("hint") ? project["hint"] : "No hint available."; + + + dpp::embed embed = dpp::embed() + .set_color(globals::color::defaultColor) + .add_field("Project Idea", projectTitle) + .add_field("Description", projectDescription); + + dpp::message message(event.command.channel_id, embed); + + // Add hint button with index as custom_id + message.add_component( + dpp::component().add_component( + dpp::component() + .set_label("Hint") + .set_type(dpp::cot_button) + .set_style(dpp::cos_primary) + .set_id("hint_button_" + std::to_string(index)) + ) + ); + + event.reply(message); + + // Reset the button click state when the /project command is run + buttonClicked = false; + + bot.on_button_click([&bot, &data](const dpp::button_click_t& event) + { + json data; + try { + std::ifstream projectFile("res/project.json"); + projectFile >> data; + } + catch (const json::parse_error& e) { + event.reply("Failed to parse project file."); + return; + } + + const auto& project = data["projects"][index]; + const std::string hint = project.contains("hint") ? project["hint"] : "No hint available."; + if (event.custom_id.rfind("hint_button_", 0) == 0 && !buttonClicked) + { + + dpp::embed hintEmbed = dpp::embed() + .set_color(globals::color::defaultColor) + .add_field("Hint", hint); + + dpp::message hintMessage(event.command.channel_id, hintEmbed); + event.reply(hintMessage); + + buttonClicked = true; + } + } + ); } diff --git a/src/res/project.json b/src/res/project.json new file mode 100644 index 0000000..98b33ef --- /dev/null +++ b/src/res/project.json @@ -0,0 +1,166 @@ +{ + "projects": [ + { + "title": "Hello World! Program", + "description": "Create a program that prints `Hello, World!` to the screen.", + "hint": "Use the [std::cout](https://en.cppreference.com/w/cpp/io/cout) stream to print text to the console.", + "level": "Beginner" + }, + { + "title": "Numbers 1 to 10", + "description": "Display the numbers from 1 to 10 on the screen.", + "hint": "Use a `for` loop to iterate from 1 to 10 and print each number. [Learn more about for loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Square Pattern of Stars", + "description": "Print a square made of `*` symbols in a grid format.", + "hint": "Use nested `for` loops to print a square pattern. [Learn more about nested loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Sum of First 10 Numbers", + "description": "Calculate and display the sum of numbers from 1 to 10.", + "hint": "Use a `for` loop to iterate from 1 to 10 and accumulate the sum. [Learn about for loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Simple GUI Application", + "description": "Create a basic graphical user interface with text and buttons.", + "hint": "Consider using a GUI library like Qt or GTK. [Learn more about Qt](https://doc.qt.io/qt-5.15/index.html), [Learn about GTK](https://www.gtk.org/docs)", + "level": "Intermediate" + }, + { + "title": "Print Your Name Multiple Times", + "description": "Print your name 5 times on the screen.", + "hint": "Use a `for` loop to print your name multiple times. [Learn more about loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Even Numbers from 2 to 20", + "description": "Display even numbers starting from 2 up to 20.", + "hint": "Use a `for` loop with a step of 2 to print even numbers. [Learn about steps in loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Countdown from 10 to 1", + "description": "Create a countdown starting from 10 and ending at 1.", + "hint": "Use a `for` loop to iterate from 10 to 1 and print each number. [Learn more about loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Alphabet A to Z", + "description": "Print the alphabet from A to Z.", + "hint": "Use a `for` loop to iterate through ASCII values of letters. [Learn more about ASCII](https://www.asciitable.com/)", + "level": "Beginner" + }, + { + "title": "Triangle of Stars", + "description": "Print a triangle shape using `*` symbols.", + "hint": "Use nested `for` loops to print a triangle pattern. [Learn about nested loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Factorial Calculator", + "description": "Calculate the factorial of a given number.", + "hint": "Use a `for` loop to multiply numbers from 1 to the given number. [Learn about factorials](https://en.wikipedia.org/wiki/Factorial)", + "level": "Beginner" + }, + { + "title": "Even or Odd Number Checker", + "description": "Check if a number is even or odd.", + "hint": "Use the modulus operator `%` to check for even or odd. [Learn about modulus operator](https://www.geeksforgeeks.org/modulo-operator-in-c-cpp-with-examples/)", + "level": "Beginner" + }, + { + "title": "Multiplication Table", + "description": "Print the multiplication table for a given number.", + "hint": "Use nested `for` loops to print the multiplication table. [Learn about nested loops](https://en.cppreference.com/w/cpp/language/for)", + "level": "Beginner" + }, + { + "title": "Greatest Common Divisor (GCD)", + "description": "Find the greatest common divisor of two numbers.", + "hint": "Use the Euclidean algorithm to find the GCD. [Learn more about GCD](https://en.wikipedia.org/wiki/Greatest_common_divisor)", + "level": "Intermediate" + }, + { + "title": "Palindrome Checker", + "description": "Check if a number is a palindrome.", + "hint": "Convert the number to a string and check if it reads the same backward. [Learn about palindromes](https://en.wikipedia.org/wiki/Palindrome)", + "level": "Intermediate" + }, + { + "title": "Fibonacci Sequence", + "description": "Generate the nth Fibonacci number.", + "hint": "Use a loop or recursion to generate Fibonacci numbers. [Learn about Fibonacci](https://en.wikipedia.org/wiki/Fibonacci_sequence)", + "level": "Intermediate" + }, + { + "title": "Celsius to Fahrenheit Converter", + "description": "Convert a temperature from Celsius to Fahrenheit.", + "hint": "Use the formula `F = C * 9/5 + 32` for conversion. [Learn more about temperature conversion](https://www.rapidtables.com/convert/temperature/celsius-to-fahrenheit.html)", + "level": "Beginner" + }, + { + "title": "Vowel Counter", + "description": "Count the number of vowels in a string.", + "hint": "Use a loop to iterate through the string and count vowels. [Learn more about string manipulation](https://en.cppreference.com/w/cpp/string/basic_string)", + "level": "Beginner" + }, + { + "title": "String Reversal", + "description": "Reverse a given string.", + "hint": "Use the [std::reverse](https://en.cppreference.com/w/cpp/algorithm/reverse) function or a loop to reverse the string.", + "level": "Beginner" + }, + { + "title": "Largest Number in an Array", + "description": "Find the largest number in an array.", + "hint": "Use a loop to iterate through the array and find the maximum value. [Learn about arrays](https://en.cppreference.com/w/cpp/container/array)", + "level": "Intermediate" + }, + { + "title": "Matrix Addition", + "description": "Add two matrices together.", + "hint": "Use nested loops to iterate through matrices and add corresponding elements. [Learn more about matrices](https://www.wikiwand.com/en/articles/Matrix)", + "level": "Intermediate" + }, + { + "title": "Bubble Sort", + "description": "Sort an array using the bubble sort algorithm.", + "hint": "Implement the bubble sort algorithm to sort the array. [Learn more about bubble sort](https://en.wikipedia.org/wiki/Bubble_sort)", + "level": "Intermediate" + }, + { + "title": "Prime Number Checker", + "description": "Check if a given number is prime.", + "hint": "Use a loop to check if the number has any divisors other than 1 and itself. [Learn about prime numbers](https://en.wikipedia.org/wiki/Prime_number)", + "level": "Intermediate" + }, + { + "title": "Basic Calculator", + "description": "Create a basic calculator that can add, subtract, multiply, and divide.", + "hint": "Use `switch` statements to perform different arithmetic operations. [Learn about switch statements](https://en.cppreference.com/w/cpp/language/switch)", + "level": "Intermediate" + }, + { + "title": "Random Number Generator", + "description": "Generate a random number between 1 and 100.", + "hint": "Use the `rand()` function and `srand()` to generate random numbers. [Learn more about random numbers](https://en.cppreference.com/w/cpp/numeric/random/rand)", + "level": "Beginner" + }, + { + "title": "Anagram Checker", + "description": "Check if a word is an anagram of another word.", + "hint": "Sort both words and compare if they are equal. [Learn more about anagrams](https://en.wikipedia.org/wiki/Anagram)", + "level": "Intermediate" + }, + { + "title": "Dice Rolling Simulator", + "description": "Simulate rolling a pair of dice.", + "hint": "Use the `rand()` function to generate random numbers between 1 and 6. [Learn about random numbers](https://en.cppreference.com/w/cpp/numeric/random/rand)", + "level": "Beginner" + } + ] +} diff --git a/src/res/project.txt b/src/res/project.txt deleted file mode 100644 index b9d82d0..0000000 --- a/src/res/project.txt +++ /dev/null @@ -1,74 +0,0 @@ -Print "Hello, World!" to the screen. -Display numbers 1 to 10. -Print a square of * symbols. -Calculate the sum of numbers 1 to 10. -Create a simple GUI (e.g. with text and buttons). -Print your name 5 times. -Print even numbers from 2 to 20. -Create a simple countdown from 10 to 1. -Print the alphabet (A to Z). -Print a triangle of * symbols. -Calculate the factorial of a number. -Check if a number is even or odd. -Display the multiplication table for a number. -Find the greatest common divisor (GCD) of two numbers. -Check if a number is a palindrome. -Calculate the nth Fibonacci number. -Convert Celsius to Fahrenheit. -Count the number of vowels in a string. -Reverse a string. -Find the largest number in an array. -Create a program that adds two matrices. -Sort an array using bubble sort. -Check if a number is a prime number. -Create a basic calculator (add, subtract, multiply, divide). -Generate a random number between 1 and 100. -Check if a word is an anagram of another word. -Simulate rolling a pair of dice. -Print the first n prime numbers. -Draw a rectangle of * symbols. -Find the sum of digits in a number. -Calculate the area of a circle given its radius. -Create a program that generates a username from a name. -Implement a basic guessing game. -Print the elements of a 2D array. -Create a stopwatch timer. -Count the occurrences of a character in a string. -Print Pascal’s triangle. -Calculate the power of a number (a^b). -Create a program to simulate a coin toss. -Sort an array using insertion sort. -Convert a binary number to decimal. -Implement a simple to-do list. -Check if a year is a leap year. -Print the sum of a series, e.g., 1 + 1/2 + 1/3 + ... + 1/n. -Implement a simple login system. -Convert a decimal number to binary. -Display a pattern like a diamond using *. -Find the second largest number in an array. -Check if a string is a palindrome. -Implement a Caesar cipher for text encryption. -Find the sum of an array. -Create a program to simulate a simple game of Rock, Paper, Scissors. -Calculate compound interest. -Create a simple inventory management system. -Print the border of a square with *. -Display the current system date and time. -Print a spiral pattern of numbers. -Calculate the average of numbers in an array. -Find the maximum and minimum of numbers in an array. -Implement a program to check for armstrong numbers. -Create a Tic-Tac-Toe game for two players. -Count the words in a sentence. -Implement a simple stopwatch. -Print the ASCII values of characters in a string. -Create a program to find all divisors of a number. -Convert a number from hexadecimal to decimal. -Generate the multiplication table for 1 to 10. -Find the LCM of two numbers. -Check if a string contains only digits. -Implement a number guessing game. -Create a hangman game with a predefined word. -Count the occurrences of each word in a sentence. -Generate all permutations of a string. -Implement a basic text-based menu system. From 2025e27b7a997e13ed8d2b068665d56c797b9f35 Mon Sep 17 00:00:00 2001 From: mbn <66748817+mbn-code@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:47:24 +0100 Subject: [PATCH 3/4] Delete src/res/hints.txt --- src/res/hints.txt | 74 ----------------------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 src/res/hints.txt diff --git a/src/res/hints.txt b/src/res/hints.txt deleted file mode 100644 index 0ab1b21..0000000 --- a/src/res/hints.txt +++ /dev/null @@ -1,74 +0,0 @@ -Use the `std::cout` stream to print text to the console. -Use a `for` loop to iterate from 1 to 10 and print each number. -Use nested `for` loops to print a square pattern. -Use a `for` loop to iterate from 1 to 10 and accumulate the sum. -Consider using a GUI library like Qt or GTK. -Use a `for` loop to print your name multiple times. -Use a `for` loop with a step of 2 to print even numbers. -Use a `for` loop to iterate from 10 to 1 and print each number. -Use a `for` loop to iterate through ASCII values of letters. -Use nested `for` loops to print a triangle pattern. -Use a `for` loop to multiply numbers from 1 to the given number. -Use the modulus operator `%` to check for even or odd. -Use nested `for` loops to print the multiplication table. -Use the Euclidean algorithm to find the GCD. -Convert the number to a string and check if it reads the same backward. -Use a loop or recursion to generate Fibonacci numbers. -Use the formula `F = C * 9/5 + 32` for conversion. -Use a loop to iterate through the string and count vowels. -Use the `std::reverse` function or a loop to reverse the string. -Use a loop to iterate through the array and find the maximum value. -Use nested loops to iterate through matrices and add corresponding elements. -Implement the bubble sort algorithm to sort the array. -Use a loop to check if the number has any divisors other than 1 and itself. -Use `switch` statements to perform different arithmetic operations. -Use the `rand()` function and `srand()` to generate random numbers. -Sort both words and compare if they are equal. -Use the `rand()` function to generate random numbers between 1 and 6. -Use a loop and a helper function to check for prime numbers. -Use nested `for` loops to print a rectangle pattern. -Use a loop to extract and sum each digit of the number. -Use the formula `area = π * r^2` for calculation. -Combine parts of the first and last name to create a username. -Use a loop to prompt the user to guess a number until they get it right. -Use nested loops to iterate through and print each element of the array. -Use a loop and `sleep` function to create a timer. -Use a loop to iterate through the string and count occurrences. -Use nested loops and the binomial coefficient formula to print Pascal’s triangle. -Use a loop or the `pow` function to calculate the power. -Use the `rand()` function to generate random numbers for heads or tails. -Implement the insertion sort algorithm to sort the array. -Use a loop to convert binary digits to a decimal number. -Use a list or array to store and manage to-do items. -Use the leap year rules to check if a year is a leap year. -Use a loop to iterate through the series and calculate the sum. -Use a loop to prompt for username and password and check against stored values. -Use a loop to convert a decimal number to binary digits. -Use nested loops to print a diamond pattern. -Use a loop to find the largest and second largest numbers. -Compare the string with its reverse to check for palindrome. -Use a loop to shift each character by a fixed number of positions. -Use a loop to iterate through the array and calculate the sum. -Use `rand()` to generate computer choices and compare with user input. -Use the formula `A = P(1 + r/n)^(nt)` for calculation. -Use a list or array to store and manage inventory items. -Use nested loops to print the border of a square. -Use the `time` library to get and display the current date and time. -Use nested loops and careful indexing to print a spiral pattern. -Use a loop to sum the numbers and divide by the count. -Use a loop to find the maximum and minimum values. -Use a loop to check if the sum of the cubes of digits equals the number. -Use a 2D array to represent the board and implement game logic. -Use a loop to iterate through the sentence and count words. -Use a loop and `sleep` function to create a stopwatch. -Use a loop to iterate through the string and print ASCII values. -Use a loop to find and print all divisors of the number. -Use a loop to convert hexadecimal digits to a decimal number. -Use nested loops to print multiplication tables from 1 to 10. -Use the formula `LCM(a, b) = abs(a*b) / GCD(a, b)` for calculation. -Use a loop to check if each character is a digit. -Use a loop to prompt the user to guess a number until they get it right. -Use a loop to prompt the user to guess letters and reveal the word. -Use a map or dictionary to count and store word occurrences. -Use recursion or the `std::next_permutation` function to generate permutations. -Use a loop and `switch` statements to create a menu system. From 395ec69351c6d0926ea1718fc25fe984d396db2f Mon Sep 17 00:00:00 2001 From: fellalli Date: Sun, 19 Jan 2025 16:55:51 +0100 Subject: [PATCH 4/4] Fixes and improvements --- src/commands/project_cmd.cpp | 87 +++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/commands/project_cmd.cpp b/src/commands/project_cmd.cpp index 00483f5..bfe080a 100644 --- a/src/commands/project_cmd.cpp +++ b/src/commands/project_cmd.cpp @@ -1,39 +1,40 @@ #include "commands.h" #include "../globals/globals.h" -#include "nlohmann/json.hpp" using json = nlohmann::json; -static bool buttonClicked = false; - -static bool buttonClicked = false; - void cmd::projectCommand(dpp::cluster& bot, const dpp::slashcommand_t& event) { static int index = -1; index++; std::ifstream projectFile("res/project.json"); - if (!projectFile.is_open()) { + if (!projectFile.is_open()) + { event.reply("Failed to open project file."); return; } json data; - try { + try + { projectFile >> data; - } catch (const json::parse_error& e) { + } + catch (const json::parse_error& e) + { event.reply("Failed to parse project file."); return; } - if (!data.contains("projects") || !data["projects"].is_array() || index >= data["projects"].size()) { + if (!data.contains("projects") || !data["projects"].is_array() || index >= data["projects"].size()) + { event.reply("Invalid project data or index out of bounds."); return; } const auto& project = data["projects"][index]; - if (!project.contains("title") || !project.contains("description")) { + if (!project.contains("title") || !project.contains("description")) + { event.reply("Project data is missing required fields."); return; } @@ -54,44 +55,46 @@ void cmd::projectCommand(dpp::cluster& bot, const dpp::slashcommand_t& event) message.add_component( dpp::component().add_component( dpp::component() - .set_label("Hint") - .set_type(dpp::cot_button) - .set_style(dpp::cos_primary) - .set_id("hint_button_" + std::to_string(index)) + .set_label("Hint") + .set_type(dpp::cot_button) + .set_style(dpp::cos_primary) + .set_id("hint_button_" + std::to_string(index)) ) ); event.reply(message); - // Reset the button click state when the /project command is run - buttonClicked = false; + bot.on_button_click([&bot, &data](const dpp::button_click_t& event) { + // Ignore if button id does not start with hint_button_ + if (event.custom_id.rfind("hint_button_", 0) != 0) + return; - bot.on_button_click([&bot, &data](const dpp::button_click_t& event) + // Remove button + dpp::message updatedMsg = event.command.get_context_message(); + updatedMsg.components.clear(); + bot.message_edit(updatedMsg); + + json data; + try { - json data; - try { - std::ifstream projectFile("res/project.json"); - projectFile >> data; - } - catch (const json::parse_error& e) { - event.reply("Failed to parse project file."); - return; - } - - const auto& project = data["projects"][index]; - const std::string hint = project.contains("hint") ? project["hint"] : "No hint available."; - if (event.custom_id.rfind("hint_button_", 0) == 0 && !buttonClicked) - { - - dpp::embed hintEmbed = dpp::embed() - .set_color(globals::color::defaultColor) - .add_field("Hint", hint); - - dpp::message hintMessage(event.command.channel_id, hintEmbed); - event.reply(hintMessage); - - buttonClicked = true; - } + std::ifstream projectFile("res/project.json"); + projectFile >> data; } - ); + catch (const json::parse_error& e) + { + event.reply("Failed to parse project file."); + return; + } + + const int hintButtonIndex = std::stoi(event.custom_id.substr(event.custom_id.rfind('_') + 1)); + const auto& project = data["projects"][hintButtonIndex]; + const std::string hint = project.contains("hint") ? project["hint"] : "No hint available."; + + dpp::embed hintEmbed = dpp::embed() + .set_color(globals::color::defaultColor) + .add_field("Hint", hint); + + dpp::message hintMessage(event.command.channel_id, hintEmbed); + event.reply(hintMessage); + }); }