cleans up the code, and makes everything consistent

This commit is contained in:
SuperNovaa41 2022-03-04 11:56:37 -05:00
parent 7a9fd8d4a4
commit 8e29c3dfa6
3 changed files with 59 additions and 13 deletions

View File

@ -7,21 +7,36 @@
std::string printFullGBPList(bool = false); std::string printFullGBPList(bool = false);
std::string genericResponse(); std::string genericResponse();
/**
* ##commandParse
*
* Uses the given arguments to choose a command to use
*
* Arguments:
* * std::vector<std::string> args - Given arguments, separated into a vector
*/
std::string commandParse(std::vector<std::string> args) std::string commandParse(std::vector<std::string> args)
{ {
if (args[0] == "gbplist") if (args[0] == "gbplist")
return printFullGBPList(); return printFullGBPList(args[1] == "update" ? true : false);
else if (args[0] == "hi") else if (args[0] == "hi")
return genericResponse(); return genericResponse();
else else
return "Invalid command!"; return "Invalid command!";
} }
#define FILE_NAME "temp-GBP" #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::string printFullGBPList(bool update)
{ {
std::map<int, std::pair<int, std::string>> gbp; std::map<unsigned short int, std::pair<int, std::string>> gbp;
if (update) if (update)
gbp = fetchAndReadGBP(); gbp = fetchAndReadGBP();
else else
@ -29,9 +44,8 @@ std::string printFullGBPList(bool update)
std::ofstream file; std::ofstream file;
file.open("temp-GBP"); file.open("temp-GBP");
std::string line; std::string line;
for (std::map<int, std::pair<int, std::string>>::iterator it = gbp.begin(); it != gbp.end(); it++) { for (std::map<unsigned short int, std::pair<int, std::string>>::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"; line = std::to_string(it->first) + ": " + it->second.second + " (" + std::to_string(it->second.first) + " GBP)\n";
file << line; file << line;
@ -43,6 +57,11 @@ std::string printFullGBPList(bool update)
} }
#undef FILE_NAME #undef FILE_NAME
/**
* ##genericResponse
*
* Gives a generic response.
*/
std::string genericResponse() std::string genericResponse()
{ {
return "Fuck you"; return "Fuck you";

View File

@ -3,6 +3,11 @@
#include <bits/stdc++.h> #include <bits/stdc++.h>
#include <Python.h> #include <Python.h>
/**
* ##fetchLatestGBP
*
* Uses the python script located in /src/ to fetch the latest GBP and write it to a file
*/
void fetchLatestGBP() void fetchLatestGBP()
{ {
FILE* fp; FILE* fp;
@ -13,9 +18,15 @@ void fetchLatestGBP()
Py_Finalize(); Py_Finalize();
} }
std::map<int, std::pair<int, std::string>>readGBPIntoList() /**
* ##readGBPIntoList
*
* Returns a map of containing the contents of the GBP file.
* Formatted as <position (unsigned short int), <gbp amount (int), username (std::string)>>
*/
std::map<unsigned short int, std::pair<int, std::string>>readGBPIntoList()
{ {
std::map<int, std::pair<int, std::string>> GBP; std::map<unsigned short int, std::pair<int, std::string>> GBP;
std::ifstream file; std::ifstream file;
file.open("../src/balances.txt"); file.open("../src/balances.txt");
std::string line; std::string line;
@ -27,11 +38,15 @@ std::map<int, std::pair<int, std::string>>readGBPIntoList()
GBP.insert({i, {tGBP, username}}); GBP.insert({i, {tGBP, username}});
i++; i++;
} }
file.close();
return GBP; return GBP;
} }
std::map<int, std::pair<int, std::string>>fetchAndReadGBP() /**
* fetches latest GBP, and then reads it into a map
*/
std::map<unsigned short int, std::pair<int, std::string>>fetchAndReadGBP()
{ {
fetchLatestGBP(); fetchLatestGBP();
return readGBPIntoList(); return readGBPIntoList();

View File

@ -13,7 +13,16 @@
using json = nlohmann::json; using json = nlohmann::json;
std::vector<std::string> 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<std::string> separateArgs(std::string args)
{ {
std::vector<std::string> out; std::vector<std::string> out;
while (1) { while (1) {
@ -61,19 +70,22 @@ void onMessage(dpp::cluster &bot, dpp::message msg)
int argIdx = msg.content.find(" ") + 1; int argIdx = msg.content.find(" ") + 1;
std::string argument = msg.content.substr(argIdx, msg.content.length() - argIdx); std::string argument = msg.content.substr(argIdx, msg.content.length() - argIdx);
std::vector<std::string> args = separate_args(argument); /* Parse the command */
std::vector<std::string> args = separateArgs(argument);
std::string msgContent = commandParse(args); std::string msgContent = commandParse(args);
std::vector<std::string> messageArgs = separate_args(msgContent); /* Here we check if we should embed a file, or just send a message */
std::vector<std::string> messageArgs = separateArgs(msgContent);
dpp::message toSend; dpp::message toSend;
if (messageArgs[0] == std::string(FILE_WARNING)) { if (messageArgs[0] == std::string(FILE_WARNING)) {
std::cout << "Handling file!\n"; std::cout << "Handling file!\n";
toSend = dpp::message(msg.channel_id, ""); 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 { } else {
toSend = dpp::message(msg.channel_id, msgContent); toSend = dpp::message(msg.channel_id, msgContent);
} }
/* Send the message */
bot.message_create(toSend); bot.message_create(toSend);
} }