A guide on how to create a simple discord bot. This is a simple introduction to getting your discord bot working, getting advanced commands to work, and connecting databases takes much more effort and experience to consider. Before following this tutorial it is recommended that you have a simple understanding of JavaScript/Node.JS and the documentation on the Discord API. https://discord.com/developers/docs/reference
THIS TUTORIAL WILL ONLY INCLUDE WRITING CODE FOR THE BOT NOT SETTING IT UP IN THE DISCORD DEVELOPER PORTAL
-
Make sure you have Node.js installed on your computer. You can check if you have it installed by running the
node -v
command in your terminal. If you don't have Node.js installed, you can download it from the official website. -- https://nodejs.org/en/ -
Create a new project directory and initialize it as a Node.js project by running the
npm init
command. This will create a package.json file in your project directory, which will be used to store information about your project and its dependencies. -
install the Discord.js library, which will allow you to interact with the Discord API, by running the following command:
npm install discord.js
in your terminal -
Create a new file called index.js in your project directory and add the following code to it:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login('your_bot_token');
-
Replace
your_bot_token
with the token for your bot, which you can get from the Discord developer portal. -
Start your bot by running the
node index.js
command in your terminal.
This part will focus on adding commands to the bot, this will only show you how to add a simple command, to add more function read the discord documentation to add more complex functions in your bot.
- You can now start creating commands for your bot by using the Discord.js library and the Discord API. For example, you can add the following code to your
index.js
file to create a!ping
command that responds with "Pong!" when the user types !ping in a Discord channel:
client.on('message', msg => {
if (msg.content === '!ping') {
msg.reply('Pong!');
}
});
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login('your_bot_token');
client.on('message', msg => {
if (msg.content === '!ping') {
msg.reply('Pong!');
}
});
With this basic setup, you can start creating more advanced commands and features for your Discord bot using Node.js and the Discord API.