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() {
if (callback.is_error()) {
event.reply("Failed to create a thread!");
return;
}
event.
reply(
"Created a thread for you!");
});
}
});
bot.global_command_create(
dpp::slashcommand(
"create-thread",
"Create a thread!", bot.me.id));
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
snowflake channel_id
Optional: the channel it was sent from.
Definition appcommand.h:1043
std::string get_command_name() const
Get the command name for a command interaction.
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
@ CHANNEL_PUBLIC_THREAD
A temporary sub-channel within a GUILD_TEXT or GUILD_FORUM channel.
Definition channel.h:85
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
The results of a REST call wrapped in a convenient struct.
Definition restresults.h:261
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...
interaction command
command interaction
Definition dispatcher.h:789
Session ready.
Definition dispatcher.h:1072
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() {
if (callback.is_error()) {
event.reply("Failed to get threads!");
return;
}
for (const auto& _thread : threads) {
thread_id = _thread.first;
break;
}
event.reply("Failed to send a message in a thread.");
return;
}
event.reply("I've sent a message in the specified thread.");
});
});
}
});
bot.global_command_create(
dpp::slashcommand(
"message-thread",
"Message a thread!", bot.me.id));
}
});
return 0;
}
snowflake guild_id
Optional: the guild it was sent from.
Definition appcommand.h:1038
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition snowflake.h:54
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
bool is_error() const
Returns true if the call resulted in an error rather than a legitimate value in the confirmation_call...
T get() const
Get the stored value via std::get.
Definition restresults.h:329
Represents messages sent and received on Discord.
Definition message.h:2350
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() {
if (callback.is_error()) {
event.reply("I failed to get the thread!");
return;
}
thread.metadata.locked = true;
if (callback2.is_error()) {
event.reply("I failed to lock the thread!");
return;
}
event.
reply(
"I have locked the thread!");
});
});
}
});
bot.global_command_create(
dpp::slashcommand(
"lock-thread",
"Lock the thread that you run this command in!", bot.me.id));
}
});
return 0;
}
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!