🚂🌈💖🤖 All aboard! Coding Train Tutorial Videos 🚂🌈💖🤖
- Create node project and install discord.js module.
$ npm init
$ npm install discord.js
-
Create an application - optionally set name, description, avatar.
-
Select Bot from left navigation and "Add Bot" - set name and icon.
-
Add bot to the A2Z server with the url:
https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&scope=bot
-
Write the code!
Login to bot account:
const Discord = require('discord.js');
const client = new Discord.Client();
client.login('YOUR BOT TOKEN');
Callback for when the bot is connected and ready:
client.once('ready', () => {
console.log('Ready!');
});
Callback for when a message is posted:
client.on('message', gotMessage);
function gotMessage(msg) {
console.log(msg.content);
}
- Run the bot!
$ node index.js
- Enable developer mode in Discord (Settings->Appearance->Enable Developer Mode).
- Copy server ID.
- Copy channel ID.
const serverID = 'SERVER ID';
const channelID = 'CHANNEL ID';
client.on('message', gotMessage);
function gotMessage(msg) {
// Only for this server and this channel
if (msg.guild.id === serverID && msg.channel.id === channelID) {
// Reply to the message ping!
if (msg.content === 'ping') {
msg.channel.send('pong');
}
}
}
- Install dotenv package.
$ npm install dotenv
- Create
.env
file:
SERVERID=123456789
CHANNELID=123456789
TOKEN=123456789
- Load environment variables using
dotenv
and.env
:
require('dotenv').config();
const serverID = process.env.SERVERID;
const channelID = process.env.CHANNELID;
const TOKEN = process.env.TOKEN;