link at the discord docs: https://discordjs.guide/#before-you-begin
-
Go to
https://discord.com/developers/applications/
-
Create the new app
-
Use OAuth2 --> URL generator and generate a url to invite your app (bot) to your server as a channel member
-
Issue(reset) the token to use it later in your code and enable the bot
-
Create the project folder and navigate in there
mkdir discord_bot && cd discord_bot
-
Set up the project:
npm init -y
npm install discord.js
npm install -g nodemon
npm install --save-dev eslint
Important
3. Create the config.json, place it in your root directory and use config-example.json as a template
{
"token": "<your token>", // you can issue the token for your bot using https://discord.com/developers/ portal
"apiKey": "<your APIKey>", // you can issue the token for your bot using https://platform.openai.com portal
"guildId": "<your GuildID>", // Discord calls servers as "guilds", so copy-paste your server ID
"clientId": "<your Client ID>" // Client means bot here, so copy-paste your bot ID here
}
-
Modify the script
index.js
const { token } = require('../config.json'); const { Client, IntentsBitField, Partials } = require('discord.js'); const client = new Client ({ intents: [ IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent, IntentsBitField.Flags.GuildMessageTyping, IntentsBitField.Flags.DirectMessages, IntentsBitField.Flags.DirectMessageReactions, IntentsBitField.Flags.DirectMessageTyping, ], partials: [ Partials.Message, Partials.Channel, Partials.Reaction ] }); client.on('ready', (c) => { console.log(`β ${c.user.tag} is online.`); }); client.on('messageCreate', message => { console.log(`Received message from ${message.author.tag} in ${message.guild ? `guild ${message.guild.name}` : 'DM'}. Content: ${message.content}`); if (message.author.bot) return; if (message.content === 'ping') { message.channel.send('Pong!'); } if (message.content === 'hello') { message.channel.send('Hello!'); } }); client.login(token);
-
Result
Note
Slash commands in Discord are special commands that start with a forward slash (/) and allow users to perform various pre-defined actions or invoke specific features
-
Create a folder and modules with slash commands and set up the export
https://github.com/fac30/discord-chatbot--Oleg-Loza/tree/main/commands/utility
-
Create registercommands.js to register the the slash commands and set up the export
https://github.com/fac30/discord-chatbot--Oleg-Loza/blob/main/registercommands.js
-
You will need to copy the
server ID
and thebot ID
. Add them to your config asguildID
andclientID
{ "token": "<your token>", // you can issue the token for your bot using https://discord.com/developers/ portal "apiKey": "<your APIKey>", // you can issue the token for your bot using https://platform.openai.com portal "guildId": "<your GuildID>", // Discord calls servers as "guilds", so copy-paste your server ID "clientId": "<your Client ID>" // Client means bot here, so copy-paste your bot ID here }
-
Modify the index.js to import and register slash commands each time the server launches:
const { commands } = require('../registercommands'); // Import the commands collection from the registercommands module const commands = new Map(); // Create a Map to store commands
-
Test the commands:
-
Modify the script
the link at the final script: index.js
-
Result:
Note
PM2 is a daemon process manager that will help you manage and keep your application online 24/7. So it will help keep our app live and running.
-
Launch the server
-
Clone the repository
git clone
-
Install nodejs and npm
apt-get installl node
-
Install dependencies
npm install discord.js npm install axios
-
Instal PM2:
sudo npm install -g pm2
-
Start PM2
pm2 start index.js
-
Confirm
pm2 list index.js
If all went well and you've added your bot successfully to a guild, you should be able to interact with it by mentioning, followed by a prompt, like so:
bro Hey, can you tell me a fun fact
or:
bro are you a bot?