| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
Attaching a File to a Message

To attach a local file to an message, you can use dpp::utility::read_file. It's a helper function from D++ that allows you to read the file's content and sent it to discord.

An example of this:

#include <dpp/dpp.h>
int main() {
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "file") {
dpp::message msg(event.command.channel_id, "Hey there, I've got a new file!");
/* attach the file to the message */
msg.add_file("foobar.txt", dpp::utility::read_file("path_to_your_file.txt"));
/* Reply to the user with the message, with our file attached. */
event.reply(msg);
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("file", "Send a message with a file attached!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::interaction::channel_id
snowflake channel_id
Optional: the channel it was sent from.
Definition appcommand.h:1056
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1449
dpp::utility::read_file
std::string DPP_EXPORT read_file(const std::string &filename)
Read a whole file into a std::string.
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::message
Represents messages sent and received on Discord.
Definition message.h:2363
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806

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

Amazingly, D++ also has a function for this! You can use dpp::cluster::request to make HTTP requests, allowing you to go ahead and pull the content from a URL.

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");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "file") {
/* Request the image from the URL specified and capture the event in a lambda. */
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [event](const dpp::http_request_completion_t& httpRequestCompletion) {
/* Create a message */
dpp::message msg(event.command.channel_id, "This is my new attachment:");
/* Attach the image to the message, only on success (Code 200). */
if (httpRequestCompletion.status == 200) {
msg.add_file("logo.png", httpRequestCompletion.body);
}
/* Send the message, with our attachment. */
event.reply(msg);
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("file", "Send a message with an image attached from the internet!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::m_get
@ m_get
GET.
Definition queues.h:190
dpp::http_request_completion_t
The result of any HTTP request. Contains the headers, vital rate limit figures, and returned request ...
Definition queues.h:112
dpp::http_request_completion_t::status
uint16_t status
HTTP status. e.g. 200 = OK, 404 = Not found, 429 = Rate limited, etc.
Definition queues.h:122
dpp::http_request_completion_t::body
std::string body
Reply body.
Definition queues.h:163

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");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "file") {
/* Create a message. */
dpp::message msg(event.command.channel_id, "");
/* Attach the image to the message we just created. */
msg.add_file("image.jpg", dpp::utility::read_file("path_to_your_image.jpg"));
/* Create an embed. */
dpp::embed embed;
embed.set_image("attachment://image.jpg"); /* Set the image of the embed to the attached image. */
/* Add the embed to the message. */
msg.add_embed(embed);
event.reply(msg);
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("file", "Send a local image along with an embed with the image!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::embed
A rich embed for display within a dpp::message.
Definition message.h:1103
dpp::embed::set_image
embed & set_image(std::string_view url)
Set embed image.
D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0