Skip to content

About

Bot Examples

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Discord Bot Examples

This repository provides examples and a step-by-step guide on how to create a simple Discord bot using discord.js.

Steps to Create Your Bot!

1. Set Up Your Project

Create a new Node.js project and install necessary dependencies.

$ npm init
$ npm install discord.js dotenv

2. Create a Discord Application

  • Navigate to the Discord Developer Portal and create a new application.
  • Optionally, set the application name, description, and avatar.
  • Note down the "Application ID" (also known as "Client ID").

3. Create and Configure Your Bot

  • In the Discord Developer Portal, select "Bot" from the left navigation and click "Add Bot".
  • Set a name and icon for your bot.
  • Note down the "Bot Token" (keep this secret).

4. Add Bot to a Server

Use the following URL to add the bot to your Discord server, replacing YOUR_APPLICATION_ID with your actual application ID:

https://discord.com/oauth2/authorize?client_id=YOUR_APPLICATION_ID&scope=bot

5. Enable Developer Mode in Discord

  • Enable "Developer Mode" under the "Advanced" settings tab on your Discord client.
  • Right-click on the server icon, and select "Copy ID" to get the server ID.

6. Configure Environment Variables

Create a .env file in your project root and add your client ID, server ID, and bot token:

CLIENTID=1234
SERVERID=1234
TOKEN=1234

These environment variables are used to keep sensitive data, like your bot token, out of your code. This is especially important if you're sharing your code with others or storing your code publicly on GitHub. (Notice how .env is included in .gitignore.)

7. Add Slash Commands Permission

  • Go to your application page in the Discord developer portal.
  • Navigate to "OAuth" -> "URL Generator".
  • Check "application.commands".
  • Open the URL that populates at the bottom and authorize the bot to access your server.

8. Create the bot code!

Create bot.js (or index.js) and paste this code:

const { Client, Events, GatewayIntentBits } = require('discord.js');
require('dotenv').config();
// Create a new client instance
const client = new Client({
  intents: [GatewayIntentBits.Guilds],
});

// When the client is ready, run this code (only once)
client.once(Events.ClientReady, readyDiscord);

// Login to Discord with your client's token
client.login(process.env.TOKEN);

function readyDiscord() {
  console.log('💖');
}

Run to see if it works! (If you see the 💖 it's working!)

$ node bot.js

9. Create a Command

Each command should be handled in a separate JS file, there are many ways you can manage this, but I suggest putting them in a folder called commands:

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder().setName('choochoo').setDescription('Says choo choo back!'),
  async execute(interaction) {
    await interaction.reply('Choo Choo! 🚂');
  },
};

10. Deploy the commands

Create deploy-commands.js and copy the example code. Then run it!

node deploy-commands.js

You only have to do this once. If you change the command (altering descriptions, changing options, etc.), then you do need to re-run deploy-commands.js.

11. Add the code to handle the command

You also need to handle the command in bot.js, add the equivalent code:

const choochoo = require('./commands/choochoo');

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;
  if (interaction.commandName === 'choochoo') {
    await choochoo.execute(interaction);
  }
});

Then run the bot again!

node bot.js

Recap of the code elements

  • commands/choochoo.js: Defines a simple slash command.
  • index.js: Handles interactions with Discord and executes commands.
  • deploy-commands.js: Script to register slash commands with Discord API.

Additional Resources

About

Bot Examples

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages