1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include "../include/handle_actions.hpp"
#include "../include/http_webhook_server.hpp"
#include "../include/utils.hpp"
#include <thread>
#include <unordered_map>
dpp::activity_type activity_type_from_string(const std::string &type) {
static const std::pair<std::string, dpp::activity_type> type_map[] = {
{"playing", dpp::activity_type::at_game},
{"streaming", dpp::activity_type::at_streaming},
{"listening", dpp::activity_type::at_listening},
{"watching", dpp::activity_type::at_watching},
{"custom", dpp::activity_type::at_custom},
{"competing", dpp::activity_type::at_competing}
};
for (const auto& kv : type_map) {
if (kv.first == type) return kv.second;
}
return dpp::activity_type::at_game; // default
}
int main(int argc, char *argv[]) {
int intents = dpp::i_default_intents;
std::unique_ptr<nlohmann::json> json_data =
std::make_unique<nlohmann::json>();
if (argc > 2) {
setenv("BOT_TOKEN", argv[1], 1);
setenv("PORT", argv[2], 1);
if (argc > 3) {
json_data =
std::make_unique<nlohmann::json>(nlohmann::json::parse(argv[3]));
}
if (argc > 4) {
intents = std::stoi(argv[4]);
}
}
const std::string BOT_TOKEN = getenv("BOT_TOKEN");
const std::string PORT = getenv("PORT");
dpp::cluster bot(BOT_TOKEN, intents);
bot.on_log(dpp::utility::cout_logger());
bot.on_slashcommand([&json_data, &bot](
const dpp::slashcommand_t &event) -> dpp::task<void> {
std::unordered_map<std::string, std::string> key_values =
app::generate_key_values(event);
std::string command_name = event.command.get_command_name();
std::string response = "Interaction found, but no response found.";
if (!command_name.empty() && json_data->contains(command_name)) {
auto &command_data = (*json_data)[command_name];
if (command_data.contains("actions")) {
auto &action = command_data["actions"];
// Actions are a list of Objects
if (action.is_array()) {
std::cout << "Executing → Actions: " << action.dump() << std::endl;
std::unordered_map<std::string, std::string>
already_returned_message =
co_await handle_actions(event, action, key_values);
if (already_returned_message.contains("error")) {
std::cout << "Error: " << already_returned_message["error"]
<< std::endl;
} else {
std::cout << "Actions executed successfully." << std::endl;
// let's push the values to the key_values
for (const auto &[key, value] : already_returned_message) {
if (key != "success") {
key_values[key] = value;
}
}
}
}
// let's retrieve final response.
response = command_data["response"];
auto update_response = app::update_string(response, key_values);
std::cout << "Executing → Response: " << update_response << std::endl;
co_return event.edit_response(update_response);
}
if (command_data.contains("response")) {
response = command_data["response"];
}
//
}
auto update_response = app::update_string(response, key_values);
std::cout << "Executing → Response: " << update_response << std::endl;
co_return event.reply(update_response);
});
bot.on_ready([&bot, &json_data, &PORT](const dpp::ready_t &event) {
if (dpp::run_once<struct register_bot_commands>()) {
std::thread http_thread([&json_data, &PORT, &bot]() {
try {
HttpWebhookServer server(std::stoi(PORT), [&json_data, &bot](
const HttpWebhookServer::
HttpRequest &req) {
HttpWebhookServer::HttpResponse res;
if (req.method == "POST") {
res.status_code = 200;
res.headers["Content-Type"] = "application/json";
try {
nlohmann::json body_json = app::json_from_string(req.body);
res.body = R"({"received": "POST request received"})";
if (body_json.contains("command")) {
if (body_json["command"] == "update") {
json_data =
std::make_unique<nlohmann::json>(body_json["data"]);
} else if (body_json["command"] == "update_status") {
std::string status = body_json.contains("status")
? body_json["status"]
: "online";
std::string activity = body_json.contains("activity")
? body_json["activity"]
: "";
std::string activity_status =
body_json.contains("activity_status")
? body_json["activity_status"]
: "";
std::string activity_url =
body_json.contains("activity_url")
? body_json["activity_url"]
: "";
std::string activity_type =
body_json.contains("activity_type")
? body_json["activity_type"]
: "playing";
dpp::presence p;
if (status == "online") {
p = dpp::presence(
dpp::presence_status::ps_online,
dpp::activity(
activity_type_from_string(activity_type),
activity, activity_status, activity_url));
} else if (status == "offline") {
p = dpp::presence(
dpp::presence_status::ps_offline,
dpp::activity(
activity_type_from_string(activity_type),
activity, activity_status, activity_url));
} else if (status == "dnd") {
p = dpp::presence(
dpp::presence_status::ps_dnd,
dpp::activity(
activity_type_from_string(activity_type),
activity, activity_status, activity_url));
} else if (status == "idle") {
p = dpp::presence(
dpp::presence_status::ps_idle,
dpp::activity(
activity_type_from_string(activity_type),
activity, activity_status, activity_url));
} else if (status == "invisible") {
p = dpp::presence(
dpp::presence_status::ps_invisible,
dpp::activity(
activity_type_from_string(activity_type),
activity, activity_status, activity_url));
}
bot.set_presence(p);
}
res.body =
R"({"status": "success", "message": "Command executed successfully"})";
}
} catch (const std::exception &e) {
res.status_code = 400;
res.body = std::string("{\"error\": \"") + e.what() + "\"}";
}
} else {
res.status_code = 400;
res.headers["Content-Type"] = "text/plain";
res.body = "Invalid request method.";
}
return res;
});
std::cout << "[BOT] Webhook server running on port " << PORT << "..."
<< std::endl;
server.start();
} catch (const std::exception &e) {
std::cerr << "[BOT] Server error: " << e.what() << std::endl;
}
});
http_thread.detach();
}
});
bot.start(dpp::st_wait);
}
|