D++ (DPP)
C++ Discord API Bot Library
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");
/* 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;
}

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");
/* 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;
}

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");
/* 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;
}

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

dpp::slashcommand_t
User has issued a slash command.
Definition: dispatcher.h:715
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:101
dpp::interaction_create_t::command
interaction command
command interaction
Definition: dispatcher.h:698
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:1361
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.
Definition: dispatcher.h:228
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:99
dpp::ready_t
Session ready.
Definition: dispatcher.h:981
D++ 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