discord.js guide
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');
4.33
3
/* 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');
Thank you!
3
0
4.56
9
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);
Thank you!
9
0
0
0
// 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.
Thank you!
0
0
3.4
5
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!")
}
})
Thank you!
5
0
4
9
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
Thank you!
9
0
Are there any code examples left?
New code examples in category Javascript