D++ (DPP)
C++ Discord API Bot Library
Caching Messages

By default D++ does not cache messages. The example program below demonstrates how to instantiate a custom cache using dpp::cache which will allow you to cache messages and query the cache for messages by ID.

This can be adjusted to cache any type derived from dpp::managed including types you define yourself.

Note
This example will cache and hold onto messages forever! In a real world situation this would be bad. If you do use this, you should use the dpp::cache::remove() method periodically to remove stale items. This is left out of this example as a learning exercise to the reader. For further reading please see the documentation of dpp::cache
#include <dpp/dpp.h>
#include <sstream>
int main() {
/* Create bot */
/* Create a cache to contain types of dpp::message */
dpp::cache<dpp::message> message_cache;
/* Message handler */
bot.on_message_create([&](const dpp::message_create_t &event) {
/* Make a permanent pointer using new, for each message to be cached */
/* Store the message into the pointer by copying it */
*m = event.msg;
/* Store the new pointer to the cache using the store() method */
message_cache.store(m);
/* Simple ghetto command handler. In the real world, use slashcommand or commandhandler here. */
std::stringstream ss(event.msg.content);
std::string cmd;
ss >> cmd;
/* Look for our command */
if (cmd == "!get") {
ss >> msg_id;
/* Search our cache for a cached message */
dpp::message* find_msg = message_cache.find(msg_id);
if (find_msg != nullptr) {
/* Found a cached message, echo it out */
bot.message_create(dpp::message(event.msg.channel_id, "This message had the following content: " + find_msg->content));
} else {
/* Nothing like that here. Have you checked under the carpet? */
bot.message_create(dpp::message(event.msg.channel_id, "There is no message cached with this ID"));
}
}
});
/* Start bot */
bot.start(dpp::st_wait);
return 0;
}
A cache object maintains a cache of dpp::managed objects.
Definition: cache.h:49
void store(T *object)
Store an object in the cache. Passing a nullptr will have no effect.
Definition: cache.h:100
T * find(snowflake id)
Find an object in the cache by id.
Definition: cache.h:153
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:271
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition: snowflake.h:48
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:64
@ i_default_intents
Default D++ intents (all non-privileged intents)
Definition: intents.h:72
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:254
Create message.
Definition: dispatcher.h:1354
message msg
message that was created (sent).
Definition: dispatcher.h:1363
Represents messages sent and received on Discord.
Definition: message.h:1105
std::string content
Definition: message.h:1115
snowflake channel_id
Definition: message.h:1107
D++ 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