When you create or get an object from Discord, you send the request to its API and in return you get either an error or the object you requested/created. You can pass a function to API calls as the callback function. This means that when the request completes, and you get a response from the API, your callback function executes. You must be careful with lambda captures! Good practice would be not capturing variables by reference unless you have to, since when the request completes and the function executes, the variables can already be destructed. Advanced reference can be found here. Now, let's see callback functions in action:
#include <dpp/dpp.h>
int main() {
int64_t limit = std::get<int64_t>(event.get_parameter("quantity"));
bot.messages_get(event.command.channel_id, 0, 0, 0, limit, [event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
std::cout << callback.get_error().message << std::endl;
return;
}
auto messages = callback.get<dpp::message_map>();
std::string contents;
for (const auto& x : messages) {
contents += x.second.content + '\n';
}
event.reply(contents);
});
bot.log(dpp::loglevel::ll_error, callback.get_error().message);
return;
}
});
event.reply(callback.get_error().message);
return;
}
});
}
});
if (dpp::run_once <struct register_global_commands>()) {
constexpr int64_t min_val{1};
constexpr int64_t max_val{100};
msgs_get.add_option(
.set_min_value(min_val)
.set_max_value(max_val)
);
dpp::slashcommand msg_error(
"msg-error",
"Get an error instead of message :)", bot.me.id);
bot.global_bulk_command_create({ msgs_get, channel_create, msg_error });
}
});
return 0;
}
This is the result: