Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 2.43 KB

2. Creating The Project.md

File metadata and controls

61 lines (43 loc) · 2.43 KB

Creating the project.

Let's get started on creating the bot itself. For this, you will need to create a project folder, install dependencies, etc.

Let's do that now! (You will need to know how to )

Creating the project

Linux, Mac, WSL (Bash for Windows)

  1. mkdir my-discord-bot && cd my-discord-bot/ - This will create the folder for the project and enter in to it.
  2. touch index.js && npm init --yes - This creates a file called index.js and initializes the project using NPM.
  3. touch Dockerfile - This creates a file for Docker to read from. I will get into this in a later step.
  4. npm install hydrabolt/discord.js --save - This installs the later master branch of discord.js which is recommended.
  5. touch settings.json - This creates a configuration file that stores our bot's token.

Windows

  1. mkdir my-discord-bot && cd my-discord-bot/ - This will create the folder for the project and enter in to it.
  2. type nul > index.js && npm init --yes - This creates a file called index.js and initializes the project using NPM.
  3. type nul > Dockerfile - This creates a file for Docker to read from. I will get into this in a later step.
  4. npm install hydrabolt/discord.js --save - This installs the later master branch of discord.js which is recommended.
  5. type nul > settings.json - This creates a configuration file that stores our bot's token.

Opening the project

You will now open our new project in your favorite IDE/editor. I personally use Visual Studio Code, but use whatever you want to make yourself more comfortable.

Now open your new settings.json. This is the format we will be using:

{
    "token": "--your bots token--"
}

and save that. This will store our bot's information to be used on startup.

Creating the bot itself!

Inside of index.js, we will create the bot. Here is an example I will be using:

const { Client } = require('discord.js');
const { token } = require('./settings');
const client = new Client();

client.on('ready', () => console.log('Ready!'));

client.on('message', (msg) => {
    if (msg.author.bot) return;

    if (msg.content.startsWith('!ping')) {
        msg.channel.send('!gnip');
    }
});

client.login(token);

Now running the bot with node index.js inside our console, the bot should now respond with !gnip when presented with !ping!

Great! Now everything is up and running, let's put this inside a container.