Basic Discord Bot Example
This is a fully customizable example Discord bot that you can add to your
Discord guild in just a few clicks. You don't need anything other than a Discord
account to get started! After forking and setting up this app, your bot will:
- Confirm installation with a message to your #general channel.
- Respond when it is tagged in a channel with a friendly, informational message.
- Reply to messages starting with the word hey.
with a sample message.
- Respond to a Discord slash command
/member-count with the number of members in the guild.
The slash command requires a bit more setup after forking, but don't worry,
we'll walk through everything here! All behavior is completely customizable by
editing the app code.
Also, for more in-depth information about building Discord bots on Autocode,
click here to check out our official, comprehensive guide!
Bot Mention Handler
You can find the bot mention handler under
functions/events/discord/bot_mention.js. After creating and naming your bot, then linking
it to Autocode, you can tag it as shown below to see a message like the one below:
Message Create Handler
The message create handler will echo messages that start with "hey"
as a reply to the original message. You can find it under
functions/events/discord/message/create/prefix/hey.js. It contains a link where you can
edit your project in the Autocode editor:
The handler responds to the default Discord
which automatically filters events that start with the word
hey based on
the configuration of the webhook handler inside the Autocode editor.
Note: Autocode automatically defines a
context variable containing
information about the incoming request, including the incoming event. To see all the
available fields an event contains, you can view the endpoint's Payload.
For more on this, check out this section of the official guide.
Member Count Command
Discord slash commands
require a little bit more setup than the previous bot mention handler, as you
must register them with Discord before you can use them. Additionally,
Discord treats guild member related events and APIs as privileged, which means for
this specific command to work, you'll need to grant those privileges to your bot.
Registering a Slash Command
After following the instructions to link your newly created Discord bot and
forking this app, you'll need to register the slash command.
Fortunately, you can easily do this with our built-in
Discord Slash Command Builder!
Once on the builder page, link your bot and create a command that looks like this:
For this bot, the
name must be
member-count, and description can be whatever
you'd like. We suggest creating the
command as a guild command
rather than a global one for this bot as global commands can take up to an hour
before they are visible, so you should also select the guild you've installed
your bot in.
Leave the Options empty for this command — these are an advanced feature
that allow you to set parameters for your slash command! For more on options,
we recommend you check out our official, comprehensive Discord guide.
You can also directly create new handlers for your commands here, but since this
app already contains a handler, we can skip this step as well. Just press the
Save All button in the bottom bar, and you're all set with registering
The final step to get this command working is to enable
Privileged Gateway Intents
for your bot. If you try to run your command before doing so, you'll see a message
To fix this, go to the
Discord developer portal and select
your bot out of the list to open your bot's settings. Press the
Bot tab, then
scroll down until you see a section like the one below:
Server Members Intent using the toggle — don't forget to press
Save Changes afterwards! Then, try running your command again and you
should see something like this:
This will also enable you to receive
guild.member events for your bot.
As of August 31st 2022, Discord restricts your bot’s ability to see when messages are sent by server members.
This means that you’ll be unable to use event triggers such as
bot_mention without enabling the Message Content Intent.
To enable this intent, head over to the Discord Developer Portal,
click your bot’s name, then click the Bot button on the sidebar.
Scroll down, and you’ll see a switch to turn on the Message Content Intent.
If you get stuck, check out this guide for screenshots showing what to click.
If your bot is in over one hundred servers,
you’ll need to complete Discord’s intent whitelisting process to enable the Message Content Intent.
If you have any questions or feedback, please join our community Discord server
from the link in the top bar. You can also follow us on Twitter, @Autocode.