Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/commands/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
74 changes: 74 additions & 0 deletions src/commands/help_cmd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "../globals/globals.h"
#include "commands.h"

#include <algorithm>
#include <cctype>
#include <string>
#include <vector>

struct HelpEntry {
std::string name;
std::string description;
std::string usage;
};

const std::vector<HelpEntry> 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<char>(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<std::string>(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 <command>` 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));
}
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ std::vector<cmdStruct> 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()
{
Expand Down