Skip to content

My first Discord Bot, plain and simple Level System

Notifications You must be signed in to change notification settings

MPZ-00/discord-cancer-med

Repository files navigation

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:

  1. Confirm installation with a message to your #general channel.
  2. Respond when it is tagged in a channel with a friendly, informational message.
  3. Replies to messages containing the words hi, hey, hello, or sup with a sample message.
  4. Respond to a Discord slash command called /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 contain hi, hey, hello, or sup as a reply to the original message. You can find it under functions/events/discord/message/create.js. It contains a link where you can edit your project in the Autocode editor:

The handler responds to the default Discord message.create event, and uses an if statement and a regex to only reply to messages with specific contents. Here's what it looks like:

// Only respond to messages containing the word "hi", "hey", "hello", or "sup"
if (context.params.event.content.match(/\bhi\b|\bhey\b|\bhello\b|\bsup\b/i)) {
  await lib.discord.channels['@0.0.6'].messages.create({
    channel_id: context.params.event.channel_id,
    content: messageContent.join('\n'),
    message_reference: {
      message_id: context.params.event.id
    }
  });
}

It's a useful pattern that you can broadly apply to other handlers as well!

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 your command!

Granting Permissions

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 like this:

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:

Enable the 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.

Useful Links

Thank You!

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, @AutocodeHQ.

About

My first Discord Bot, plain and simple Level System

Topics

Resources

Stars

Watchers

Forks