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