Basic Discord bot written in JavaScript
- Make Discord account if you don't have one already.
- Goto to the Discord Developer Portal and click New Application
- NAME your bot, Check the box and click Create
- Click Bot on the left
- PRESENCE INTENT (toggle on)
- SERVERS MEMBERS INTENT (toggle on)
- MESSAGE CONTENT INTENT (toggle on)
- Administrator (check box)
- Save Changes
- Reset Token
- Click Yes, do it!
- in a Terminal window
mkdir MyJavaScriptBot
cd MyJavaScriptBot/
npm init --yes
touch config.js
touch index.js
nano config.json
- In the Discord Developer Portal click Copy to copy your bot's private TOKEN (always keep this private)
- back in the terminal (nano still open)
{
"token": "<paste your copied token here>",
}
- In the Discord Developer Portal
- click OAuth2
- click URL Generator
- click bot
- check the Administrator check box
- click Copy
- back in the terminal (nano still open)
{
"token": "<paste your copied token here>",
"inviteURL": "<paste the copied URL here>",
}
- open a new TAB in your Web-Browser and paste the copied URL in the Address bar of the new TAB (don't press ENTER)
- click back to the Discord Developer Portal TAB
- In the Discord Developer Portal (see image below showing steps in Developer Portal)
- click General Information
- click Copy button under APPLICATION ID
- back in the terminal (nano still open)
{
"token": "<paste your copied token here>",
"inviteURL": "<paste the copied URL here>",
"applicationID": "<paste copied APPICATION ID here>"
}
- In the Discord Developer Portal
- click Copy button under PUBLIC_KEY
- back in the terminal (nano still open)
{
"token": "<paste your copied token here>",
"inviteURL": "<paste the copied URL here>",
"applicationID": "<paste copied APPICATION ID here>",
"publicKey": "<paste your copied PUBLIC_KEY here>"
}
- CTRL+O -> ENTER -> CTRL+X
- Create a Discord Server (also known as a Guild)
- click the new TAB you opened earlier in your Web-Browser and click enter on the Address you pasted in the Address bar
Choose your server from the Drop-Down box then click Continue
Click Authorize
- The bot (which still needs the code written for) should now be OFFLINE on your server, next we will write the code to make the bot perform
- back in the terminal
npm install discord.js
nano index.js
const Discord = require("discord.js");
const { token } = require('./config.json');
const allIntents = Discord.Intents;
const { Client, Events } = require('discord.js');
const client = new Client({ intents: 3276799 });
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.on(Events.MessageCreate, async (interaction) => {
console.log(interaction.content);
if (interaction.content.toLowerCase() === 'hello') {
await interaction.reply({ content: `Hello, ${interaction.author.username}. Have a wonderful day!`, ephemeral: false });
}
});
client.login(token);
> CTRL+O -> ENTER -> CTRL+X
node index.js
- Discord - Developer Docs
- Discord - Developer Server
- discord.js
- Discord Intents Calculator
- Tested on Ubuntu 22.04 on 2023-10-02
- Recommend using VSCode instead of nano
- Visual Studio Code (VSCode)
- This isn't meant to be a JavaScript course, for an introductory JavaScript Course see the link below