arg handling and stuff
This commit is contained in:
parent
2df93a60cc
commit
cc42d1d9b8
1
.gitignore
vendored
1
.gitignore
vendored
@ -36,3 +36,4 @@
|
||||
|
||||
balances.txt
|
||||
gbp-bot
|
||||
temp-GBP
|
||||
|
@ -1121,6 +1121,8 @@ gbp.cpp
|
||||
/home/seth/documents/programming/discord-bots/gbp/src/gbp.cpp
|
||||
string
|
||||
-
|
||||
vector
|
||||
-
|
||||
|
||||
/home/seth/documents/programming/discord-bots/gbp/src/gbp.cpp
|
||||
string
|
||||
|
Binary file not shown.
@ -1,4 +1,4 @@
|
||||
{
|
||||
"token": "OTQ5MTIxNDU2MTY2NTM5Mjk2.YiFwPA.WEl7l-Q0aH3aQNE7mZc2jN8Z0TU",
|
||||
"token": "token here",
|
||||
"bot_command": "!gbp"
|
||||
}
|
||||
|
@ -1,10 +1,25 @@
|
||||
#include "gbp.cpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define FILE_WARNING "/FILE/"
|
||||
|
||||
std::string printFullGBPList(bool = false);
|
||||
std::string genericResponse();
|
||||
|
||||
std::string commandParse(std::vector<std::string> args)
|
||||
{
|
||||
if (args[0] == "gbplist")
|
||||
return printFullGBPList();
|
||||
else if (args[0] == "hi")
|
||||
return genericResponse();
|
||||
else
|
||||
return "Invalid command!";
|
||||
}
|
||||
|
||||
|
||||
#define FILE_NAME "temp-GBP"
|
||||
std::string printFullGBPList(bool update = false)
|
||||
std::string printFullGBPList(bool update)
|
||||
{
|
||||
std::map<int, std::pair<int, std::string>> gbp;
|
||||
if (update)
|
||||
@ -23,10 +38,12 @@ std::string printFullGBPList(bool update = false)
|
||||
}
|
||||
file.close();
|
||||
|
||||
std::string out;
|
||||
out.append(FILE_WARNING);
|
||||
out.append(" ");
|
||||
out.append(FILE_NAME);
|
||||
std::string out = std::string(FILE_WARNING) + " " + std::string(FILE_NAME);
|
||||
return out;
|
||||
}
|
||||
#undef FILE_NAME
|
||||
|
||||
std::string genericResponse()
|
||||
{
|
||||
return "Fuck you";
|
||||
}
|
||||
|
41
src/main.cpp
41
src/main.cpp
@ -13,6 +13,21 @@
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
std::vector<std::string> separate_args(std::string args)
|
||||
{
|
||||
std::vector<std::string> out;
|
||||
while (1) {
|
||||
if (args.find(" ") == -1) {
|
||||
out.push_back(args);
|
||||
break;
|
||||
} else {
|
||||
out.push_back(args.substr(0, args.find(" ")));
|
||||
args = args.substr(args.find(" ") + 1, args.length() - (args.find(" ") + 1));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* ##hasCommand
|
||||
*
|
||||
@ -29,6 +44,21 @@ bool hasCommand(dpp::message msg)
|
||||
return msg.content.substr(0, (msg.content.find(" "))) == config["bot_command"];
|
||||
}
|
||||
|
||||
dpp::message setMessage(unsigned int channel_id, std::string content)
|
||||
{
|
||||
std::vector<std::string> messageArgs = separate_args(content);
|
||||
if (messageArgs[0] == std::string(FILE_WARNING)) {
|
||||
dpp::message msg = dpp::message(channel_id, "");
|
||||
msg.add_file("gbp-list.txt", dpp::utility::read_file(content.substr(content.find(" ") + 1, content.length() - (content.find(" ") + 1))));
|
||||
return msg;
|
||||
} else {
|
||||
dpp::message msg = dpp::message(channel_id, content);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ##onMessage
|
||||
*
|
||||
@ -42,18 +72,15 @@ void onMessage(dpp::cluster &bot, dpp::message msg)
|
||||
{
|
||||
if (!hasCommand(msg))
|
||||
return;
|
||||
std::cout << "Command received!\n";
|
||||
|
||||
|
||||
int argIdx = msg.content.find(" ") + 1;
|
||||
std::string argument = msg.content.substr(argIdx, msg.content.length() - argIdx);
|
||||
|
||||
std::string msgContent = "";
|
||||
std::vector<std::string> args = separate_args(argument);
|
||||
std::string msgContent = commandParse(args);
|
||||
std::cout << msgContent << std::endl;
|
||||
|
||||
|
||||
|
||||
|
||||
dpp::message toSend = dpp::message(msg.channel_id, msgContent);
|
||||
dpp::message toSend = setMessage(msg.channel_id, msgContent);
|
||||
|
||||
bot.message_create(toSend);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user