basic discord.js bot code example
Example 1: discord.js start
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'TOLKEN'; // Add your token here
client.on('ready', () => {
console.log('The client is ready!')
})
client.login(token)
Example 2: discord.js bot
//first you must install dicord.js by running the command: npm install discord.js
//once u do that copy and paste this into your main file
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () =>{
client.user.setStatus('your status')
console.log('Connected!')
})
//rest of your code
//always remember to never share your token with anyone
client.login('your-token-here')
Example 3: discord js ping command
// If the command sent in the chat is "ping"
if(cmd === `${prefix}ping `) {
// It sends the user "Pinging"
message.channel.send("Pinging...").then(m =>{
// The math thingy to calculate the user's ping
var ping = m.createdTimestamp - message.createdTimestamp;
// Basic embed
var embed = new Discord.MessageEmbed()
.setAuthor(`Your ping is ${ping}`)
.setColor("Your Color")
// Then It Edits the message with the ping variable embed that you created
m.edit(embed)
});
}
Example 4: Bots latency discord js
var yourping = new Date().getTime() - message.createdTimestamp
var botping = Math.round(bot.ws.ping)
message.channel.send(`Your ping: ${yourping} \nBots ping: ${botping}`)