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

Search
Loading...
Searching...
No Matches
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 */
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); /* Because we're handling messages, we need to use the "i_message_content" intent! */
/* Create a cache to contain types of dpp::message */
dpp::cache<dpp::message> message_cache;
bot.on_log(dpp::utility::cout_logger());
/* Message handler */
bot.on_message_create([&message_cache](const dpp::message_create_t &event) {
/* Make a permanent pointer using new, for each message to be cached */
dpp::message* m = new dpp::message();
/* 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);
});
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot, &message_cache](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "get") {
dpp::message* find_msg = message_cache.find(std::get<std::string>(event.get_parameter("message_id")));
/* If find_msg is null, tell the user and return. */
if (!find_msg) {
event.reply("There is no message cached with this ID");
return;
}
event.reply("This message had the following content: " + find_msg->content);
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create a new command. */
dpp::slashcommand newcommand("get", "Get the contents of a message that was cached via an id", bot.me.id);
/* Add a parameter option. */
newcommand.add_option(dpp::command_option(dpp::co_string, "message_id", "The ID of the message you want to find", true));
/* Register the command */
bot.global_command_create(newcommand);
}
});
/* Start bot */
bot.start(dpp::st_wait);
return 0;
}
dpp::cache
A cache object maintains a cache of dpp::managed objects.
Definition cache.h:50
dpp::cache::store
void store(T *object)
Store an object in the cache. Passing a nullptr will have no effect.
Definition cache.h:101
dpp::cache::find
T * find(snowflake id)
Find an object in the cache by id.
Definition cache.h:154
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::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:1436
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::i_message_content
@ i_message_content
Intent for receipt of message content.
Definition intents.h:112
dpp::i_default_intents
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition intents.h:142
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::co_string
@ co_string
A string value.
Definition appcommand.h:69
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::command_option
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition appcommand.h:208
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::interaction_create_t::get_parameter
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
dpp::message_create_t
Create message.
Definition dispatcher.h:1746
dpp::message
Represents messages sent and received on Discord.
Definition message.h:2350
dpp::message::content
std::string content
Contents of the message.
Definition message.h:2395
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806
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