From 8e663889bad344a368182717b3cd3938141f90c1 Mon Sep 17 00:00:00 2001 From: Xavi99 Date: Tue, 27 Oct 2026 10:56:28 +0100 Subject: [PATCH] Help Command --- CMakeLists.txt | 1 + src/commands/commands.h | 7 ++++ src/commands/help_cmd.cpp | 74 +++++++++++++++++++++++++++++++++++++++ src/main.cpp | 4 +-- 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/commands/help_cmd.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 35dbed9..447404e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ add_executable(bot src/commands/project_cmd.cpp src/commands/rule_cmd.cpp src/commands/beginner.cpp + src/commands/help_cmd.cpp # utils src/utils/suggestion/suggestion.cpp diff --git a/src/commands/commands.h b/src/commands/commands.h index edfc965..7ce3885 100644 --- a/src/commands/commands.h +++ b/src/commands/commands.h @@ -71,6 +71,13 @@ namespace cmd */ void beginnerCommand(dpp::cluster& bot, const dpp::slashcommand_t& event); + /** + * @brief Replies with a help guide + * @param bot cluster + * @param event slash command event + */ + void helpCommand(dpp::cluster& bot, const dpp::slashcommand_t& event); + namespace utils { /** diff --git a/src/commands/help_cmd.cpp b/src/commands/help_cmd.cpp new file mode 100644 index 0000000..5208ffd --- /dev/null +++ b/src/commands/help_cmd.cpp @@ -0,0 +1,74 @@ +#include "../globals/globals.h" +#include "commands.h" + +#include +#include +#include +#include + +struct HelpEntry { + std::string name; + std::string description; + std::string usage; +}; + +const std::vector helpEntries = { + {"topic", "Get a topic question to spark conversation.", "/topic"}, + {"beginner", "Get a beginner's guide to C++ with resources.", "/beginner"}, + {"coding", "Get a random C++ coding question.", "/coding"}, + {"close", "Close a ticket or forum post.", "/close"}, + {"ticket", "Open a private support ticket.", "/ticket [participant]"}, + {"code", "Show how to format code on Discord.", "/code"}, + {"project", "Get a random C++ project idea with a hint button.", "/project"}, + {"rule", "Show a server rule by its number.", "/rule [number]"}, + {"help", "List all commands or show help for one command.", "/help [command]"}}; + +std::string to_lower(std::string value) { + std::transform(value.begin(), value.end(), value.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + return value; +} + +void cmd::helpCommand(dpp::cluster &bot, const dpp::slashcommand_t &event) { + const dpp::command_interaction cmdData = event.command.get_command_interaction(); + + if (!cmdData.options.empty()) { + const auto &option = cmdData.options[0]; + if (option.type == dpp::co_string) { + const std::string query = to_lower(std::get(option.value)); + + const auto it = std::find_if(helpEntries.begin(), helpEntries.end(), + [&query](const HelpEntry &entry) { return entry.name == query; }); + + if (it == helpEntries.end()) { + const dpp::embed embed = dpp::embed() + .set_color(globals::color::defaultColor) + .set_title("Unknown command") + .add_field("`/" + query + "` does not exist.", + "Use `/help` to list all available commands."); + + event.reply(dpp::message(event.command.channel_id, embed).set_flags(dpp::m_ephemeral)); + return; + } + + const dpp::embed embed = dpp::embed() + .set_color(globals::color::defaultColor) + .set_title("Help: /" + it->name) + .add_field("Description", it->description) + .add_field("Usage", "`" + it->usage + "`"); + + event.reply(dpp::message(event.command.channel_id, embed)); + return; + } + } + + dpp::embed embed = dpp::embed() + .set_color(globals::color::defaultColor) + .set_title("Bot Commands") + .set_description("Use `/help ` for details about a specific command."); + + for (const auto &entry : helpEntries) + embed.add_field("/" + entry.name, entry.description); + + event.reply(dpp::message(event.command.channel_id, embed)); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c1bacad..fa1d60d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,8 +18,8 @@ std::vector cmdList = { { "ticket", "Open a ticket", cmd::ticketCommand, { dpp::command_option(dpp::command_option_type::co_user, "participant", "Add participant", false) }}, { "code", "Formatting code on Discord", cmd::codeCommand }, { "project", "Get a project idea", cmd::projectCommand }, - { "rule", "Get the server rules", cmd::ruleCommand, { dpp::command_option(dpp::command_option_type::co_integer, "number", "Rule to mention", false) }} -}; + { "rule", "Get the server rules", cmd::ruleCommand, { dpp::command_option(dpp::command_option_type::co_integer, "number", "Rule to mention", false) }}, + { "help", "List all commands or show help for one command", cmd::helpCommand, {dpp::command_option(dpp::command_option_type::co_string, "command", "Command to show help for", false)}}}; int main() {