Sending

HTTP

Discord++ bots make HTTP REST calls by way of the Call series of objects, which abstract away the intricacies of the intricacies of the underlying libraries from the bot's developer. If you're using Native The base Call object takes 6 different arguments, enumerated here:

Call

ArgumentTypeRequiredDescription
methodstd::stringThe kind of HTTP operation that the call will be: One of either "GET" (for getting something), "POST" (for creating something), "PATCH" (for modifying something), or "DELETE". (for deleting something)
targetstd::stringThe API URL that the call will use. "https://discord.com/api/v#/" is appended for you, you just need the direct API path such as "/users/@me" .
typestd::stringThe MIME type of the body, very likely either "application/json" for JSON payloads or "multipart/form-data" for file calls. multipart/form-data calls also needs a boundary used to separate the parts of the body. (FileCall below handles this automatically.)
bodystd::stringThe data payload you're sending to Discord, formatted as a string.
onWritevoid (const bool)The function called after Discord++ sends the Call to Discord, primarily for use in ratelimiting- you likely won't need this. If there was an error the const bool argument will be true.
onReadvoid (const bool, const json)The function called with the response Discord has for the Call, formatted as an NLohmann JSON object. If there was an error the const bool argument will be true.

In addition, the base library has two Call abstractions: JsonCall and FileCall. JsonCall is set up to easily take and NLohmann JSON object as a payload and FileCall allows you to send both a file and an NLohmann JSON object.

JsonCall

ArgumentTypeRequiredDescription
methodstd::stringAs in Call
targetstd::stringAs in Call
typestd::string🗑️Always "application/json"
bodystd::string🗑️Rendered based on payload
payloadnlohmann::jsonThe JSON object to send to Discord
onWritevoid (const bool)As in Call
onReadvoid (const bool, const json)As in Call

FileCall

ArgumentTypeRequiredDescription
methodstd::stringAs in Call
targetstd::stringAs in Call
typestd::string🗑️Always "multipart/formdata; boundary={generated boundary}"
bodystd::string🗑️Rendered based on filename, filetype, file, and payload
filenamestd::stringThe name of the file, e.g. "image.jpg".
filetypestd::stringThe MIME type of the file, e.g. "image/jpg". There's a list of common MIME types in Mozilla's MDN Web Docs
filestd::stringThe file encoded as a string, see below for an example of how to create this. There may be a utility function in the future to automate this process. Note: The file string could be quite large, take care to not make copies of it if possible.
payloadnlohmann::jsonThe JSON object to send to Discord
onWritevoid (const bool)As in Call
onReadvoid (const bool, const json)As in Call

Note: this example uses a CreateMessageCall, a child class of FileCall in the Native plugin. It hides the nlohmann::json payload and renders it using some other fields- here it uses std::string content

cpp
# Open the file
std::ifstream ifs("image.jpg", std::ios::binary);
if (!ifs) {
std::cerr << "Couldn't load file 'image.jpg'!\n";
return;
}
# Seek to the end of the file and get the pointer position to determine the size of the file
ifs.seekg(0, std::ios::end);
std::ifstream::pos_type fileSize = ifs.tellg();
# Seek back to the beginning of the file
ifs.seekg(0, std::ios::beg);
# Create an appropriately-sized string
auto file = std::make_shared<std::string>(fileSize, '\0');
# Read the file into the string
ifs.read(file->data(), fileSize);
# Send the message
bot->createMessage()
->channel_id(*msg.channel_id)
->content("Look at this photograph")
->filename("image.jpg")
->filetype("image/jpg")
->file(file)
->run();

WebSocket

WebSocket commands are sent by way of Bot::send(const int opcode, std::shared_ptr<const json> payload, std::shared_ptr<const std::function<void()>> callback). If you don't want to deal in std::shared_ptrs the Overload plugin will create them for you. (It may be rolled into the main library in the future, it used to handle calls before the Call class and its children were created.) You can find a list of the gateway commands in the official Discord API docs. Here's an example Update Presence command from Echo:

cpp
bot->send(3, {
{"game", {
{"name", "with " + name},
{"type", 0}
}},
{"status", "online"},
{"afk", false},
{"since", "null"}
});

Webhook

Discord++ currently doesn't support webhooks, sorry. We're open to PRs, though!