general code improvements

This commit is contained in:
SuperNovaa41 2022-03-05 09:47:51 -05:00
parent cb8675459b
commit 1e4d772244
3 changed files with 22 additions and 16 deletions

View File

@ -5,7 +5,6 @@
#include <algorithm> #include <algorithm>
#define FILE_WARNING "/FILE/" #define FILE_WARNING "/FILE/"
#define EMBED_WARNING "/EMBED/"
typedef std::string (*vFunc)(void); typedef std::string (*vFunc)(void);
typedef std::string (*vecFunc)(std::vector<std::string>); typedef std::string (*vecFunc)(std::vector<std::string>);
@ -32,7 +31,20 @@ std::string commandParse(std::vector<std::string> args)
return "Invalid command!"; return "Invalid command!";
} }
#define FILE_NAME "temp-GBP" std::string sendFile(std::string filePath, std::string fileName = "")
{
if (fileName == "")
fileName = filePath;
/*
* Files are sent with:
* FILE_WARNING $PATH $FILENAME
*
* $FILENAME is the name of the file that will be sent to discord, if you don't want the file sent
* to have the save name as the one on disk, or if you have a custom path for the file, set $FILENAME
*/
return std::string(FILE_WARNING) + " " + filePath + " " + fileName;
}
/** /**
* printFullGBPList * printFullGBPList
* *
@ -53,10 +65,8 @@ std::string printFullGBPList()
} }
file.close(); file.close();
std::string out = std::string(FILE_WARNING) + " " + std::string(FILE_NAME); return sendFile("temp-GBP", "gbp-list.txt");
return out;
} }
#undef FILE_NAME
/** /**
* ##fetchGBP * ##fetchGBP

View File

@ -13,6 +13,8 @@
using json = nlohmann::json; using json = nlohmann::json;
#define BOT_COMMAND "!gbp"
/** /**
* ##separateArgs * ##separateArgs
* *
@ -29,10 +31,9 @@ std::vector<std::string> separateArgs(std::string args)
if (args.find(" ") == -1) { if (args.find(" ") == -1) {
out.push_back(args); out.push_back(args);
break; break;
} else {
out.push_back(args.substr(0, args.find(" ")));
args = args.substr(args.find(" ") + 1, args.length() - (args.find(" ") + 1));
} }
out.push_back(args.substr(0, args.find(" ")));
args = args.substr(args.find(" ") + 1, args.length() - (args.find(" ") + 1));
} }
return out; return out;
} }
@ -47,10 +48,7 @@ std::vector<std::string> separateArgs(std::string args)
*/ */
bool hasCommand(dpp::message msg) bool hasCommand(dpp::message msg)
{ {
json config; return msg.content.substr(0, (msg.content.find(" "))) == std::string(BOT_COMMAND);
std::ifstream configFile("../config.json");
configFile >> config;
return msg.content.substr(0, (msg.content.find(" "))) == config["bot_command"];
} }
/** /**
@ -79,10 +77,9 @@ void onMessage(dpp::cluster &bot, dpp::message msg)
dpp::message toSend; dpp::message toSend;
if (messageArgs[0] == std::string(FILE_WARNING)) { if (messageArgs[0] == std::string(FILE_WARNING)) {
toSend = dpp::message(msg.channel_id, ""); toSend = dpp::message(msg.channel_id, "");
toSend.add_file("gbp-list.txt", dpp::utility::read_file(messageArgs[1])); toSend.add_file(messageArgs[2], 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 */ /* Send the message */
bot.message_create(toSend); bot.message_create(toSend);
@ -95,7 +92,6 @@ int main()
json config; json config;
std::ifstream configFile("../config.json"); std::ifstream configFile("../config.json");
configFile >> config; configFile >> config;
std::string token = config["token"]; std::string token = config["token"];
dpp::cluster bot(token, dpp::i_default_intents | dpp::i_message_content); dpp::cluster bot(token, dpp::i_default_intents | dpp::i_message_content);