diff --git a/src/commands.cpp b/src/commands.cpp index a423558..4e03ea9 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -7,21 +7,36 @@ std::string printFullGBPList(bool = false); std::string genericResponse(); +/** + * ##commandParse + * + * Uses the given arguments to choose a command to use + * + * Arguments: + * * std::vector args - Given arguments, separated into a vector + */ std::string commandParse(std::vector args) { if (args[0] == "gbplist") - return printFullGBPList(); + return printFullGBPList(args[1] == "update" ? true : false); else if (args[0] == "hi") return genericResponse(); else return "Invalid command!"; } - #define FILE_NAME "temp-GBP" +/** + * printFullGBPList + * + * Returns a path to a file that contains a formatted list of gbp values + * + * Arguments: + * * bool update - Should we fetch the latest GBP? + */ std::string printFullGBPList(bool update) { - std::map> gbp; + std::map> gbp; if (update) gbp = fetchAndReadGBP(); else @@ -29,9 +44,8 @@ std::string printFullGBPList(bool update) std::ofstream file; file.open("temp-GBP"); - std::string line; - for (std::map>::iterator it = gbp.begin(); it != gbp.end(); it++) { + for (std::map>::iterator it = gbp.begin(); it != gbp.end(); it++) { line = std::to_string(it->first) + ": " + it->second.second + " (" + std::to_string(it->second.first) + " GBP)\n"; file << line; @@ -43,6 +57,11 @@ std::string printFullGBPList(bool update) } #undef FILE_NAME +/** + * ##genericResponse + * + * Gives a generic response. + */ std::string genericResponse() { return "Fuck you"; diff --git a/src/gbp.cpp b/src/gbp.cpp index 9495b2d..140b907 100644 --- a/src/gbp.cpp +++ b/src/gbp.cpp @@ -3,6 +3,11 @@ #include #include +/** + * ##fetchLatestGBP + * + * Uses the python script located in /src/ to fetch the latest GBP and write it to a file + */ void fetchLatestGBP() { FILE* fp; @@ -13,9 +18,15 @@ void fetchLatestGBP() Py_Finalize(); } -std::map>readGBPIntoList() +/** + * ##readGBPIntoList + * + * Returns a map of containing the contents of the GBP file. + * Formatted as > + */ +std::map>readGBPIntoList() { - std::map> GBP; + std::map> GBP; std::ifstream file; file.open("../src/balances.txt"); std::string line; @@ -27,11 +38,15 @@ std::map>readGBPIntoList() GBP.insert({i, {tGBP, username}}); i++; } + file.close(); return GBP; } -std::map>fetchAndReadGBP() +/** + * fetches latest GBP, and then reads it into a map + */ +std::map>fetchAndReadGBP() { fetchLatestGBP(); return readGBPIntoList(); diff --git a/src/main.cpp b/src/main.cpp index 03aceed..41a0340 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,7 +13,16 @@ using json = nlohmann::json; -std::vector separate_args(std::string args) +/** + * ##separateArgs + * + * Returns a vector of each word in the given input, split by " " + * + * Arguments: + * * std::string args - The string to split up + */ + +std::vector separateArgs(std::string args) { std::vector out; while (1) { @@ -61,19 +70,22 @@ void onMessage(dpp::cluster &bot, dpp::message msg) int argIdx = msg.content.find(" ") + 1; std::string argument = msg.content.substr(argIdx, msg.content.length() - argIdx); - std::vector args = separate_args(argument); + /* Parse the command */ + std::vector args = separateArgs(argument); std::string msgContent = commandParse(args); - std::vector messageArgs = separate_args(msgContent); + /* Here we check if we should embed a file, or just send a message */ + std::vector messageArgs = separateArgs(msgContent); dpp::message toSend; if (messageArgs[0] == std::string(FILE_WARNING)) { std::cout << "Handling file!\n"; toSend = dpp::message(msg.channel_id, ""); - toSend.add_file("gbp-list.txt", dpp::utility::read_file(msgContent.substr(msgContent.find(" ") + 1, msgContent.length() - (msgContent.find(" ") + 1)))); + toSend.add_file("gbp-list.txt", dpp::utility::read_file(messageArgs[1])); } else { toSend = dpp::message(msg.channel_id, msgContent); } - + + /* Send the message */ bot.message_create(toSend); }