Message Commands
Wanna use good old message commands instead? We have you covered!
Last updated
Wanna use good old message commands instead? We have you covered!
Last updated
Unless you have a good reason to continue using legacy message contents, you're strongly recommended to migrate to Application Commands (i.e. slash commands) instead. Refer to Registering Commands and Responding to Commands to see how that's accomplished with DiscordKit.
Gateway Intents is Discord's method of filtering gateway events, such that applications only receive the events that are useful to them. Events or fields that might contain sensitive data are gated behind "privileged intents" and require approval to remain enabled if your bot is in more than 100 servers.
Due to a controversial change that was made some time ago, user-created content (including messages) is now gated behind a privileged intent and empty by default. You'll have to enable it in both your bot application and Discord's Developer Portal in order for those fields to be populated.
To do so, go to your application in Discord's Developer Portal and enable the Message Content Intent under the Bot page (you might have to scroll down a bit). Make sure to press Save Changes after making this change!
As stated under the option, you'll have to request for Discord's approval to continue using the privileged message content intent when your bot is in more than 100 servers!
You'll also have to make a change to the intents specified during the creation of your Client
object. You might be instantiating the Client
object like so:
To enable the message content intent, simply append .messageContent
to the bitfield:
That's all the changes you'll need to make in your code to receive message contents.
Now that you're receiving message contents, you'll need to handle them. To do so, place the following example code before the call to .login()
(assuming bot
is an instance of Client
):
Rerun your bot, and you should see something printed out to your console every time a message is sent in any server your bot is in!
To reply to a message, you could add something like the following code in the event handler:
The bot should now reply to every message with the content "?ping" with a "Pong!" message!
However, the code doesn't scale as the number of commands increase. At this point, you could choose to handle commands in any way you wish, but an example is provided below if you'd like something to build off:
Add the following static property in the struct that's annotated with @main
:
And you're all set! Go build something wonderful with message commands (or use slash commands as a better alternative instead)!