D++ (DPP)
C++ Discord API Bot Library
Attaching a file to a message

Attached files must be locally stored.

To attach a file to a message, you can upload a local image.

D++ has this helper function to read a file: dpp::utility::read_file.

An example program:

#include <dpp/dpp.h>
int main() {
dpp::cluster bot("token");
/* Message handler to look for a command called !file */
bot.on_message_create([&bot](const dpp::message_create_t &event) {
if (event.msg.content == "!file") {
// create a message
dpp::message msg(event.msg.channel_id, "Hey there, i've got a new file!");
// attach the file
msg.set_file_content(dpp::utility::read_file("path_to_your_file.txt"));
/*
* alternatively, you can put any other name in here.
* This name doesn't have to be the same as the uploaded filename.
* But it should have the same file extension.
*/
msg.set_filename("file.txt");
// send the message
bot.message_create(msg);
}
});
bot.start(false);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:384
Create message.
Definition: dispatcher.h:1156
message msg
message that was created (sent).
Definition: dispatcher.h:1165
std::string content
Definition: message.h:1020

Attachments via an url aren't possible. But there's a workaround for. You can download the file and then attach it to the message.

To make requests, D++ also has a helper function: dpp::cluster::request.

The following example program shows how to request a file and attach it to a message.

#include <dpp/dpp.h>
int main() {
dpp::cluster bot("token");
/* Message handler to look for a command called !file */
bot.on_message_create([&bot](const dpp::message_create_t &event) {
if (event.msg.content == "!file") {
// request an image
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot, channel_id = event.msg.channel_id](const dpp::http_request_completion_t & httpRequestCompletion) {
// create a message
dpp::message msg(channel_id, "This is my new attachment:");
// attach the image on success
if (httpRequestCompletion.status == 200) {
msg.set_file_content(httpRequestCompletion.body);
msg.set_filename("example-image.png"); // give the file a name
}
// send the message
bot.message_create(msg);
});
}
});
bot.start(false);
return 0;
}

Here's another example of how to add a local image to an embed.

Upload the image in the same message as the embed and then reference it in the embed.

#include <dpp/dpp.h>
int main() {
dpp::cluster bot("token");
/* Message handler to look for a command called !file */
bot.on_message_create([&bot](const dpp::message_create_t &event) {
if (event.msg.content == "!file") {
// create a message
dpp::message msg(event.msg.channel_id, "");
// attach the file to the message
msg.set_file_content(dpp::utility::read_file("path_to_your_image.jpg"));
msg.set_filename("image.jpg");
dpp::embed embed;
embed.set_image("attachment://image.jpg"); // reference to the attached file
msg.add_embed(embed);
// send the message
bot.message_create(msg);
}
});
bot.start(false);
return 0;
}
D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0