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

Search
Loading...
Searching...
No Matches
Creating and Interacting with Threads

A new feature added to Discord recently is Threads, these allow you to break off a message into a different "channel", without creating a whole new channel. There are also other types of "thread channels", one example being a forums channel. This type of channel only contains threads, meaning you can't send messages in it so if you want to make one of them, be careful about trying to send a message in it!

In this tutorial, we'll be going through:

- How to create a thread.
- How to loop through all the active threads in a server (and sending a message in one).
- How to lock a thread (editing threads).

First, let's go through creating a thread.

#include <dpp/dpp.h>
int main() {
/* Create the bot */
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when the bot detects a message in any server and any channel it has access to. */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "create-thread") {
/* Here we create a thread in the current channel. It will expire after 60 minutes of inactivity. We'll also allow other mods to join, and we won't add a slowdown timer. */
bot.thread_create("Cool thread!", event.command.channel_id, 60, dpp::channel_type::CHANNEL_PUBLIC_THREAD, true, 0, [event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("Failed to create a thread!");
return;
}
event.reply("Created a thread for you!");
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register the command */
bot.global_command_create(dpp::slashcommand("create-thread", "Create a thread!", 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::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::CHANNEL_PUBLIC_THREAD
@ CHANNEL_PUBLIC_THREAD
A temporary sub-channel within a GUILD_TEXT or GUILD_FORUM channel.
Definition channel.h:85
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::confirmation_callback_t
The results of a REST call wrapped in a convenient struct.
Definition restresults.h:262
dpp::interaction_create_t::reply
void reply(command_completion_event_t callback=utility::log_error()) const
Acknowledge interaction without displaying a message to the user, for use with button and select menu...
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806

If all went well, you'll see that the bot has successfully created a thread!

Now, let's cover looping through all the threads in a server. For this demonstration, we'll be picking the first thread we find in the list and sending a message in it.

#include <dpp/dpp.h>
int main() {
/* Create the bot */
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when the bot detects a message in any server and any channel it has access to. */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "message-thread") {
/* Get all active threads in a guild. */
bot.threads_get_active(event.command.guild_id, [&bot, event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("Failed to get threads!");
return;
}
/* Get the list of active threads in the guild. */
auto threads = callback.get<dpp::active_threads>();
dpp::snowflake thread_id;
/* Loop through the threads, getting each value in the map. Then we get the first value and then break off.
* The reason we're getting only the first value is because, for this example, we'll just assume you've only got a single active thread (the one created by the bot)
*/
for (const auto& _thread : threads) {
thread_id = _thread.first;
break;
}
/* Send a message in the first thread we find. */
bot.message_create(dpp::message(thread_id, "Hey, I'm first to message in a cool thread!"), [event](const dpp::confirmation_callback_t& callback2) {
if (callback2.is_error()) {
event.reply("Failed to send a message in a thread.");
return;
}
event.reply("I've sent a message in the specified thread.");
});
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register the command */
bot.global_command_create(dpp::slashcommand("message-thread", "Message a thread!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::interaction::guild_id
snowflake guild_id
Optional: the guild it was sent from.
Definition appcommand.h:1051
dpp::snowflake
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition snowflake.h:54
dpp::active_threads
std::map< snowflake, active_thread_info > active_threads
A map of threads alongside optionally the thread_member tied to the bot if it is in the thread....
Definition thread.h:234
dpp::confirmation_callback_t::is_error
bool is_error() const
Returns true if the call resulted in an error rather than a legitimate value in the confirmation_call...
dpp::confirmation_callback_t::get
T get() const
Get the stored value via std::get.
Definition restresults.h:330
dpp::message
Represents messages sent and received on Discord.
Definition message.h:2363

After that, you'll be able to see your bot send a message in your thread!

Those of you who are familar with sending messages in regular channels may have also noticed that sending messages to threads is the same as sending a general message. This is because threads are basically channels with a couple more features!

Now, we're going to cover how to lock a thread! With this, you'll also learn how to edit threads in general, meaning you can go forward and learn how to change even more stuff about threads, as much as your heart desires!

#include <dpp/dpp.h>
int main() {
/* Create the bot */
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when the bot detects a message in any server and any channel it has access to. */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "lock-thread") {
/* Get this channel as a thread. */
bot.thread_get(event.command.channel_id, [&bot, event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("I failed to get the thread!");
return;
}
/* Get the thread from the callback. */
auto thread = callback.get<dpp::thread>();
/* Set the thread to locked. */
thread.metadata.locked = true;
/* Now we tell discord about our updates, meaning the thread will lock! */
bot.thread_edit(thread, [event](const dpp::confirmation_callback_t& callback2) {
if (callback2.is_error()) {
event.reply("I failed to lock the thread!");
return;
}
event.reply("I have locked the thread!");
});
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register the command */
bot.global_command_create(dpp::slashcommand("lock-thread", "Lock the thread that you run this command in!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::thread
A definition of a discord thread. A thread is a superset of a channel. Not to be confused with std::t...
Definition thread.h:107

Once you've ran that, you'll see that you were successfully able to lock a thread!

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