Skip to content

Commit 2e6f6b8

Browse files
authored
docs: Added a new page for on_message_create and changed warnings about message content privilege. (#799)
1 parent 7faa78a commit 2e6f6b8

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

docpages/example_programs/interactions_and_components.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
The example programs listed here demonstrate lots of things to do with interactions, application commands (slash commands) and message components. If you're looking to make your bot **modern and user friendly** these examples are what you need.
44

55
* \subpage slashcommands "Using Slash Commands and Interactions"
6-
* \subpage context-menu "Context Menus"
76
* \subpage subcommands "Slash command sub-commands"
87
* \subpage components "Using button components"
8+
* \subpage components2 "Advanced button components"
99
* \subpage components3 "Using select menu components"
10-
* \subpage components2 "Advanced components"
10+
* \subpage detecting-messages "Listening to messages"
11+
* \subpage context-menu "Context Menus"
1112
* \subpage modal-dialog-interactions "Modal Dialogs"
1213
* \subpage commandhandler "Unified message/slash command handler"
1314
* \subpage application-command-autocomplete "Slash command auto completion"

docpages/example_programs/interactions_and_components/commandhandler.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,27 @@ prefixed channel messages) you should consider instantiating a dpp::commandhandl
55
commands and their parameters to functions in your program. A simple example of using this object to route commands is shown below, and will
66
route both the /ping (global slash command) and .ping (prefixed channel message command) to a lambda where a reply can be generated.
77

8-
\note This example automatically hooks the dpp::cluster::on_message_create and dpp::cluster::on_slashcommand events. This can be overridden if needed to allow you to still make use of these functions for your own code, if you need to do this please see the constructor documentation for dpp::commandhandler.
8+
\note This example automatically hooks the dpp::cluster::on_message_create and dpp::cluster::on_slashcommand events. This can be overridden if needed to allow you to still make use of these functions for your own code, if you need to do this please see the constructor documentation for dpp::commandhandler.
99

1010
Note that because the dpp::commandhandler::add_command method accepts a `std::function` as the command handler, you may point a command handler
1111
at a simple lambda (as shown in this example), a function pointer, or an instantiated class method of an object. This is extremely flexible
1212
and allows you to decide how and where commands should be routed, either to an object oriented system or to a lambda based system.
1313

14+
\warning As of August 30th, 2022, you are advised to only be using slash commands, not messages for commands. To prevent the command handler from handling commands with messages, you should only use the "/" prefix. If you wish to still use messages for commands, this tutorial will still cover it but, again, it is discouraged by Discord.
15+
1416
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
1517
#include <dpp/dpp.h>
1618
1719
int main()
1820
{
19-
dpp::cluster bot("token");
21+
/* If your bot only uses the "/" prefix, you can remove the intents here. */
22+
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
2023
2124
bot.on_log(dpp::utility::cout_logger());
2225
2326
/* Create command handler, and specify prefixes */
2427
dpp::commandhandler command_handler(&bot);
25-
/* Specifying a prefix of "/" tells the command handler it should also expect slash commands */
28+
/* Specifying a prefix of "/" tells the command handler it should also expect slash commands. Remove the .add_prefix(".") if you wish to only make it a slash command */
2629
command_handler.add_prefix(".").add_prefix("/");
2730
2831
bot.on_ready([&command_handler](const dpp::ready_t &event) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
\page detecting-messages Listening to messages
2+
3+
Sometimes, you may want to listen out for a message, rather than a command. This could be used for many cases like a spam filter, a bot that would respond to movie quotes with gifs, or a chat bot! However, in this page, we'll be using this to create a moderation filter (detect bad words).
4+
5+
\warning As of August 30th, 2022, Discord made Message Content a privileged intent. Whilst this means you can still use prefixed messages as commands, Discord does not encourage this and heavily suggests you use [slash commands](/slashcommands.html). If you wish to create commands, use [slash commands](/slashcommands.html), not messages.
6+
7+
~~~~~~~~~~{.cpp}
8+
#include <dpp/dpp.h>
9+
10+
int main()
11+
{
12+
/* Create the bot, but with our intents so we can use messages. */
13+
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
14+
15+
bot.on_log(dpp::utility::cout_logger());
16+
17+
/* The event is fired when the bot detects a message in any server and any channel it has access to. */
18+
bot.on_message_create([&bot](const dpp::message_create_t& event) {
19+
20+
/* See if the message contains the phrase we want to check for.
21+
* If there's at least a single match, we reply and say it's not allowed.
22+
*/
23+
if (event.msg.content.find("bad word") != std::string::npos) {
24+
event.reply("That is not allowed here. Please, mind your language!", true);
25+
}
26+
});
27+
28+
bot.start(dpp::st_wait);
29+
30+
return 0;
31+
}
32+
~~~~~~~~~~
33+
34+
If all went well, you should have something like this!
35+
36+
\image html badwordfilter.png

docpages/example_programs/interactions_and_components/slashcommands.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ When a user issues these commands the reply will arrive via the `on_slashcommand
1010

1111
dpp::interaction_create_t::reply has two overloaded versions of the method, one of which accepts simple `std::string` replies, for basic text-only messages (if your message is 'ephemeral' you must use this) and one which accepts a dpp::message for more advanced replies. Please note that at present, Discord only supports a small subset of message and embed features within an interaction response object.
1212

13-
\note You can also use the unified command handler, which lets you combine channel based message commands and slash commands under the same lambda with the same code like they were one and the same. Note that after August of 2022 Discord will be discouraging bots from using commands that are prefixed messages via means of a privileged message intent. It is advised that you exclusively use slash commands, or the unified handler with only a prefix of "/" going forward for any new bots you create and look to migrating existing bots to this setup.
14-
1513
This first example goes over creating a single command globally.
1614

1715
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}

docpages/images/badwordfilter.png

54.6 KB
Loading

0 commit comments

Comments
 (0)