Discord.js guide code example

Example 1: discord.js example

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

client.login('token');

Example 2: discord.js start code

const Discord = require('discord.js') //this says that its using discord.js and classifing it as a bot
const bot = new Discord.Client();

const token = 'put your bots token here!';
//link to the dev portal to make your own discord bot here: https://discord.com/developers/applications

Example 3: Discord.js Basics

const Discord = require("discord.js")
const client = new Discord.Client()
client.on("ready", ()=>{
console.log(`Logged in as ${client.user.tag}`)
})
client.on("message", async function (message) {
  if(message.content === "!test") {
	message.reply("Hello World!")
  }
})

Example 4: discord.js start

const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'YOUR TOKEN HERE'; // For get a token , go here https://discord.com/developers/applications

client.login(token);

Example 5: discord.js download

/* If you are a mac user, do this in TERMINAL
If you are a Window/Linux user, do this in COMMAND PROMPT

npm install discord.js 

DISCORD.JS IS NOW INSTALLED.
I suggest looking on youtube for a tutorial on setting up a project, but here are the basics.


THIS CODE SHOULD BE IN A "index.js" or "main.js" or whatever your main file is.*/

const Discord = require('discord.js');
const client - new Discord.Client();
const /*you can have any prefix you want here*/ prefix = "?"

client.on("ready", () => {
	console.log('literally anything you want goes here')
})

//SUPER BASIC COMMAND: BASICALLY SHOWS THAT YOUR BOT CAN SPEAK
client.on('message', message => {
	if(message.content.startsWith(`${prefix}ping`)){
    	message.channel.send('pong!');
    }
})


//EXTREMELY IMPORTANT: GET YOUR TOKEN FROM THE DISCORD DEVELOPER PORTAL
//NEVER EVER EVER EVER TELL/GIVE ANYONE YOUR TOKEN!
client.login('your token here');

Example 6: discord.js guide

// npm install discord.js
// Download node.js first if you can't install the discord.js depedencies
const Discord = require('discord.js') 
const client = new Discord.client()

// You can store the prefix in a JSON file and access it 
// by destructuring it's object property
const {
  prefix // let's assume it is "!" as the prefix
  token // DO NOT SHARE THAT TO ANYONE IN ANYWAY. You can get that on your Discord bot developer page.
} = require('./<filename>.json') // as an example

client.on("ready", () => {
  console.log('Ready!')
})

client.on("message", message => {
  // if the message DOES NOT (!) starts with the prefix then the bot will not respond
  if (!message.content.startsWith(prefix)) return;
  
	// Here, ping is a template litteral which is an useful way to format declared values with strings
  if (message.content === `${prefix}ping`) { // !ping -> bot respond: Pong
    message.channel.send("Pong")
  }
}

client.login(token) // Your token as stored in a JSON file.